Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from twisted.internet import reactor, protocol, utils
- from twisted.web2 import server, http, resource, channel, http_headers, responsecode,iweb, stream
- class UploadFile(resource.PostableResource):
- def render(self, ctx):
- request = iweb.IRequest(ctx)
- if request.files:
- # Check the files extensions
- fileExtension = request.files['filename'][0][1].mediaSubtype
- # print fileExtension
- if(fileExtension == "jpeg" or fileExtension == "png"):
- '''
- Initial set up
- '''
- # Create directory for user
- path_to_image = 'images/' # function to create directory for user
- # Read the file
- file = request.files['filename'][0][2]
- # Here the script edits the image
- cmd = ["blender",
- "-b",
- path_to_image + "render.blend",
- "-P",
- path_to_image + "script.py"
- ]
- # Catch errors for the subprocess
- try:
- # Spawn the process
- p = subprocess.Popen(cmd)
- # Wait for process to complete before continuing
- p.wait()
- # If there is an error send a 500 response
- except subprocess.CalledProcessError:
- return http.Response("500", {'content-type': http_headers.MimeType('text', 'html')}, "<html><head><title>500 Render error</title></head><body><h1>Render error</h1>There was an error running the render. Please try again.</body></html>")
- # Everything went better than expected, return the video?
- f = open(path_to_image + '0001_0050.avi', 'r')
- video = f.read()
- f.close()
- # print dir(stream.IByteStream.get)
- # fs = stream.FileStream(f)
- # f = stream.FileStream.read(fs, True)
- return http.Response("200", {'content-type': http_headers.MimeType('video', 'x-msvideo')}, stream=video)
- # If the user tries to upload something other than a jpeg or png
- else:
- return http.Response("415", {'content-type': http_headers.MimeType('text', 'html')}, "<html><head><title>415 Unsupported Media Type</title></head><body><h1>Unsupported Media Type</h1>The file you uploaded is not accepted by the server. Only jpeg or png files will be accepted.</body></html>")
- # No file was uploaded
- else:
- return http.Response("400", {'content-type': http_headers.MimeType('text', 'html')}, "<html><head><title>400 No file uploaded</title></head><body><h1>No file uploaded</h1>You must upload a file in order to get a response.</body></html>")
- class Toplevel(resource.Resource):
- addSlash = True
- # Root level respond with 404
- def render(self, ctx):
- return http.Response("404", {'content-type': http_headers.MimeType('text', 'html')}, "<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1>The resource / cannot be found.</body></html>")
- child_i = UploadFile()
- if __name__ == "__main__":
- # This is for Twisted processing protocol
- # pp = MyPP(10)
- site = server.Site(Toplevel())
- reactor.listenTCP(8007, channel.HTTPFactory(site))
- try:
- #NB: should have installSignalHandlers=False flag
- reactor.run()
- except KeyboardInterrupt:
- reactor.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement