0
|
1 import os
|
|
2 from urlparse import urlparse
|
|
3 from webob import Response, exc
|
|
4
|
|
5 class Handler(object):
|
|
6 def __init__(self, app, request):
|
|
7 self.app = app
|
|
8 self.request = request
|
|
9 self.application_path = urlparse(request.application_url)[2]
|
|
10
|
|
11 def link(self, path=(), permanant=False):
|
|
12 if isinstance(path, basestring):
|
|
13 path = [ path ]
|
|
14 path = [ i.strip('/') for i in path ]
|
|
15 if permanant:
|
|
16 application_url = [ self.request.application_url ]
|
|
17 else:
|
|
18 application_url = [ self.application_path ]
|
|
19 path = application_url + path
|
|
20 return '/'.join(path)
|
|
21
|
|
22 def redirect(self, location):
|
|
23 raise exc.HTTPSeeOther(location=location)
|
|
24
|
|
25 class Get(Handler):
|
|
26
|
|
27 @classmethod
|
|
28 def match(cls, request):
|
|
29 return request.method == 'GET'
|
|
30
|
|
31 def __call__(self):
|
|
32 retval = """<html><body><form name="upload_form" method="post" enctype="multipart/form-data">
|
|
33 <input type="file" name="file"/><input type="submit" value="upload"/></form></body></html>"""
|
|
34 return Response(content_type='text/html', body=retval)
|
|
35
|
|
36 class Post(Handler):
|
|
37
|
|
38 @classmethod
|
|
39 def match(cls, request):
|
|
40 return request.method == 'POST'
|
|
41
|
|
42 def __call__(self):
|
|
43 fin = self.request.POST['file']
|
|
44 assert os.sep not in fin.filename
|
|
45 assert '..' not in fin.filename
|
|
46 fout = file(os.path.join(self.app.directory, fin.filename), 'w')
|
|
47 fout.write(fin.file.read())
|
|
48 fout.close()
|
|
49 self.redirect(self.link('/'))
|
|
50
|