-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.py
74 lines (61 loc) · 2.28 KB
/
upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import settings
class Content(db.Model):
username = db.StringProperty()
uid = db.StringProperty()
filename = db.StringProperty()
contents = db.BlobProperty()
date = db.DateTimeProperty(auto_now_add=True)
class UploadPage(webapp.RequestHandler):
def post(self):
item = Content(
username=self.request.get("username"),
uid =self.request.get("uid"),
filename=self.request.get("filename"),
contents=db.Blob(self.request.get("file"))
)
item.put()
self.response.out.write('Ok. <a href="/list">List</a>')
class ListPage(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
if not user:
self.redirect(users.create_login_url(self.request.uri))
return
if user.email().lower() not in settings.ALLOWED_USERS:
self.response.out.write("Permission Denied")
return
template_data = {
'nickname' : user.nickname(),
'logout_url' : users.create_logout_url("/list"),
'items': Content.all().order("-date")
}
path = os.path.join(os.path.dirname(__file__), 'templates/list.html')
self.response.out.write(template.render(path, template_data ))
class DeletePage(webapp.RequestHandler):
def get(self, key):
item = Content.get(key)
Content.delete(item)
self.redirect('/list', False)
class ShowPage(webapp.RequestHandler):
def get(self, key):
item = Content.get(key)
self.response.headers['Content-Type'] = 'text/plain'
#evaluate the extension, txt and log should be text, otherwise octet-stream
#self.response.headers['Content-Type'] = 'application/octet-stream'
self.response.out.write(item.contents)
application = webapp.WSGIApplication([
('/list', ListPage),
('/show/(.*)', ShowPage),
('/delete/(.*)', DeletePage),
('/upload', UploadPage)
], debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()