Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import settings, utils, uwsgi, cgi, os
- from cStringIO import StringIO
- from uuid import uuid4
- class WSGIHandler:
- def __init__(self):
- pass
- def __call__(self, env, start_response):
- #
- # Nginx should handle this!
- #
- #if env['REQUEST_METHOD'] != 'POST':
- # start_response('405 Method Not Allowed', [])
- # raise StopIteration
- #
- # Nginx should handle this!
- #
- #if env['QUERY_STRING']:
- # start_response('404 Not Found', [])
- # raise StopIteration
- boundary = cgi.parse_header(env['CONTENT_TYPE'])[1]
- print 'boundary: ', boundary
- try:
- body_fd = StringIO(utils.getShortBody(env))
- meta = cgi.parse_multipart(body_fd, boundary)
- finally:
- body_fd.close()
- print 'meta: ', meta
- mime, src = tuple(meta['file'])
- try:
- ext = {
- 'image/jpeg' : '.jpeg',
- 'image/gif' : '.gif',
- 'image/png' : '.png',
- }[mime]
- except KeyError:
- start_response('415 Unsupported Media Type', [('Content-Length', '0')])
- raise StopIteration
- name = str(uuid4()) + ext
- dst = os.path.join(settings.AVATARS_DIR, name)
- os.rename(src, dst)
- print src, ' ', dst
- response = '{"img":"' + name + '"}'
- headers = [
- ('Content-Type', 'text/html'),
- ('Content-Length', str(len(response))),
- ]
- start_response('200 OK', headers)
- yield response
- run = WSGIHandler()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement