Mercurial > hg > uploader
annotate uploader/handlers.py @ 13:b8c636b0b567
make work for non pastescript frameworks
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 05 Jul 2011 23:37:52 -0700 |
parents | 40b76777a880 |
children | 916d45d4f921 |
rev | line source |
---|---|
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): | |
13
b8c636b0b567
make work for non pastescript frameworks
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
23 return exc.HTTPSeeOther(location=location) |
0 | 24 |
25 class Get(Handler): | |
26 | |
13
b8c636b0b567
make work for non pastescript frameworks
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
27 form = """<form name="upload_form" method="post" enctype="multipart/form-data"> |
2 | 28 <input type="file" name="file"/><input type="submit" value="upload"/></form></body></html>""" |
29 | |
0 | 30 @classmethod |
2 | 31 def match(cls, app, request): |
7
a8c60480fce0
make query_string argument more universal
k0s <k0scist@gmail.com>
parents:
6
diff
changeset
|
32 if app.query_string and (app.query_string not in request.GET): |
a8c60480fce0
make query_string argument more universal
k0s <k0scist@gmail.com>
parents:
6
diff
changeset
|
33 return False |
0 | 34 return request.method == 'GET' |
35 | |
36 def __call__(self): | |
13
b8c636b0b567
make work for non pastescript frameworks
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
37 form = "<html><body>" |
b8c636b0b567
make work for non pastescript frameworks
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
38 if 'uploaded' in self.request.GET: |
b8c636b0b567
make work for non pastescript frameworks
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
39 form += '<div>%s uploaded successfully</div>' % self.request.GET['uploaded'] |
b8c636b0b567
make work for non pastescript frameworks
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
40 form += self.form + '</body></html>' |
b8c636b0b567
make work for non pastescript frameworks
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
41 return Response(content_type='text/html', body=form) |
0 | 42 |
43 class Post(Handler): | |
44 | |
45 @classmethod | |
2 | 46 def match(cls, app, request): |
0 | 47 return request.method == 'POST' |
48 | |
2 | 49 def write(self, fin, path): |
50 assert os.sep not in fin.filename | |
51 assert '..' not in fin.filename | |
52 fout = file(path, 'w') | |
53 fout.write(fin.file.read()) | |
54 fout.close() | |
55 | |
0 | 56 def __call__(self): |
57 fin = self.request.POST['file'] | |
2 | 58 _path = os.path.join(self.app.directory, fin.filename) |
11 | 59 self.write(fin, _path) |
13
b8c636b0b567
make work for non pastescript frameworks
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
60 return self.redirect(self.link('/?uploaded=' + fin.filename)) |
2 | 61 |
62 def path(directory, request): | |
63 if os.sep == '/': | |
64 return os.path.join(directory, request.path_info.strip('/')) | |
65 return os.path.join(directory, *request.path_info.strip('/').split('/')) | |
66 | |
67 class SubpathGet(Get): | |
68 | |
69 @classmethod | |
70 def match(cls, app, request): | |
9 | 71 if not Get.match(app, request): |
2 | 72 return False |
73 _path = path(app.directory, request) | |
74 if os.path.exists(_path) and os.path.isdir(_path): | |
75 return True | |
0 | 76 |
2 | 77 class SubpathPost(Post): |
78 | |
79 @classmethod | |
80 def match(cls, app, request): | |
81 if request.method != 'POST': | |
82 return False | |
83 _path = path(app.directory, request) | |
84 if os.path.exists(_path) and os.path.isdir(_path): | |
85 return True | |
86 | |
87 def __call__(self): | |
88 fin = self.request.POST['file'] | |
10 | 89 _path = path(self.app.directory, self.request) |
90 _path = os.path.join(_path, fin.filename) | |
91 self.write(fin, _path) | |
13
b8c636b0b567
make work for non pastescript frameworks
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
92 return self.redirect(self.link(self.request.path_info)) |
2 | 93 |