Mercurial > hg > uploader
view uploader/handlers.py @ 0:827f7577f940
initial commit of file upload widget
author | k0s <k0scist@gmail.com> |
---|---|
date | Sat, 21 Nov 2009 15:29:03 -0500 |
parents | |
children | 0b5fce452087 |
line wrap: on
line source
import os from urlparse import urlparse from webob import Response, exc class Handler(object): def __init__(self, app, request): self.app = app self.request = request self.application_path = urlparse(request.application_url)[2] def link(self, path=(), permanant=False): if isinstance(path, basestring): path = [ path ] path = [ i.strip('/') for i in path ] if permanant: application_url = [ self.request.application_url ] else: application_url = [ self.application_path ] path = application_url + path return '/'.join(path) def redirect(self, location): raise exc.HTTPSeeOther(location=location) class Get(Handler): @classmethod def match(cls, request): return request.method == 'GET' def __call__(self): retval = """<html><body><form name="upload_form" method="post" enctype="multipart/form-data"> <input type="file" name="file"/><input type="submit" value="upload"/></form></body></html>""" return Response(content_type='text/html', body=retval) class Post(Handler): @classmethod def match(cls, request): return request.method == 'POST' def __call__(self): fin = self.request.POST['file'] assert os.sep not in fin.filename assert '..' not in fin.filename fout = file(os.path.join(self.app.directory, fin.filename), 'w') fout.write(fin.file.read()) fout.close() self.redirect(self.link('/'))