Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 16th, 2010  |  syntax: Python  |  size: 3.07 KB  |  hits: 150  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # -*- coding: utf-8 -*-
  2. from twisted.internet import reactor, protocol, utils
  3. from twisted.web2 import server, http, resource, channel, http_headers, responsecode,iweb, stream
  4.  
  5. class UploadFile(resource.PostableResource):
  6.                
  7.         def render(self, ctx):
  8.                
  9.                 request = iweb.IRequest(ctx)
  10.                
  11.                 if request.files:
  12.                
  13.                         # Check the files extensions
  14.                         fileExtension = request.files['filename'][0][1].mediaSubtype
  15.                         # print fileExtension
  16.                         if(fileExtension == "jpeg" or fileExtension == "png"):
  17.  
  18.  
  19.                                 '''
  20.                                 Initial set up
  21.                                 '''
  22.                                 # Create directory for user
  23.                                 path_to_image = 'images/' # function to create directory for user
  24.  
  25.                                 # Read the file
  26.                                 file = request.files['filename'][0][2]
  27.                
  28.                                 # Here the script edits the image
  29.  
  30.                                 cmd = ["blender",
  31.                                              "-b",
  32.                                              path_to_image + "render.blend",
  33.                                                          "-P",
  34.                                                          path_to_image + "script.py"
  35.                                                          ]
  36.                                                
  37.                                 # Catch errors for the subprocess
  38.                                 try:
  39.                                         # Spawn the process
  40.                                         p = subprocess.Popen(cmd)
  41.                                         # Wait for process to complete before continuing
  42.                                         p.wait()
  43.                        
  44.                                 # If there is an error send a 500 response
  45.                                 except subprocess.CalledProcessError:
  46.                                         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>")
  47.                
  48.                                 # Everything went better than expected, return the video?
  49.                                 f = open(path_to_image + '0001_0050.avi', 'r')
  50.                                 video = f.read()
  51.                                 f.close()
  52.                                 # print dir(stream.IByteStream.get)
  53.                                 # fs = stream.FileStream(f)
  54.                                 # f = stream.FileStream.read(fs, True)
  55.                                 return http.Response("200", {'content-type': http_headers.MimeType('video', 'x-msvideo')}, stream=video)
  56.  
  57.                         # If the user tries to upload something other than a jpeg or png
  58.                         else:
  59.                                 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>")
  60.                
  61.                 # No file was uploaded
  62.                 else:
  63.                         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>")
  64.        
  65. class Toplevel(resource.Resource):
  66.        
  67.         addSlash = True
  68.  
  69.         # Root level respond with 404
  70.         def render(self, ctx):
  71.                 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>")
  72.  
  73.         child_i = UploadFile()
  74.  
  75. if __name__ == "__main__":
  76.    
  77.         # This is for Twisted processing protocol
  78.         # pp = MyPP(10)
  79.         site = server.Site(Toplevel())
  80.  
  81.         reactor.listenTCP(8007, channel.HTTPFactory(site))
  82.         try:
  83.                 #NB: should have installSignalHandlers=False flag
  84.           reactor.run()
  85.         except KeyboardInterrupt:
  86.           reactor.stop()