Advertisement
Guest User

Untitled

a guest
Jul 27th, 2012
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. import settings, utils, uwsgi, cgi, os
  2. from cStringIO import StringIO
  3. from uuid import uuid4
  4.  
  5.  
  6. class WSGIHandler:
  7.    
  8.     def __init__(self):
  9.         pass
  10.    
  11.     def __call__(self, env, start_response):
  12.         #
  13.         # Nginx should handle this!
  14.         #
  15.         #if env['REQUEST_METHOD'] != 'POST':
  16.         #   start_response('405 Method Not Allowed', [])
  17.         #   raise StopIteration
  18.        
  19.         #
  20.         # Nginx should handle this!
  21.         #
  22.         #if env['QUERY_STRING']:
  23.         #   start_response('404 Not Found', [])
  24.         #   raise StopIteration
  25.        
  26.         boundary = cgi.parse_header(env['CONTENT_TYPE'])[1]
  27.         print 'boundary: ', boundary
  28.         try:
  29.             body_fd = StringIO(utils.getShortBody(env))
  30.             meta = cgi.parse_multipart(body_fd, boundary)
  31.         finally:
  32.             body_fd.close()
  33.  
  34.         print 'meta: ', meta
  35.         mime, src = tuple(meta['file'])
  36.        
  37.         try:
  38.             ext = {
  39.                 'image/jpeg' : '.jpeg',
  40.                 'image/gif'  : '.gif',
  41.                 'image/png'  : '.png',
  42.             }[mime]
  43.         except KeyError:
  44.             start_response('415 Unsupported Media Type', [('Content-Length', '0')])
  45.             raise StopIteration
  46.         name = str(uuid4()) + ext
  47.         dst = os.path.join(settings.AVATARS_DIR, name)
  48.         os.rename(src, dst)
  49.         print src, ' ', dst
  50.        
  51.         response = '{"img":"' + name + '"}'
  52.         headers = [
  53.             ('Content-Type', 'text/html'),
  54.             ('Content-Length', str(len(response))),
  55.         ]
  56.         start_response('200 OK', headers)
  57.         yield response
  58.  
  59. run = WSGIHandler()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement