# -*- 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')}, "500 Render error

Render error

There was an error running the render. Please try again.") # 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')}, "415 Unsupported Media Type

Unsupported Media Type

The file you uploaded is not accepted by the server. Only jpeg or png files will be accepted.") # No file was uploaded else: return http.Response("400", {'content-type': http_headers.MimeType('text', 'html')}, "400 No file uploaded

No file uploaded

You must upload a file in order to get a response.") 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')}, "404 Not Found

Not Found

The resource / cannot be found.") 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()