-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.py
101 lines (84 loc) · 3.16 KB
/
store.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# CirruxCache provides dynamic HTTP caching on AppEngine (CDN like)
# Copyright (C) 2009 Samuel Alba <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
import re
import web
from google.appengine.ext import blobstore
from google.appengine.ext import db
from google.appengine.api import users
BLOB_KEY = re.compile('blob-key=[\'"]*([^\'";\s\r\n]+)', re.M)
class _StoreMeta(db.Model):
blobKey = db.StringProperty()
class Store(object):
# This IP list is authorized to upload and remove files
allowFrom = ['127.0.0.1']
def GET(self, request):
req = web.ctx.path.split('/')
cmd = 'cmd' + req.pop().capitalize()
if hasattr(self, cmd):
attr = getattr(self, cmd)
if callable(attr):
return attr('/'.join(req))
return self.serve(web.ctx.path)
def DELETE(self, request):
return self.cmdDelete(request)
def POST(self, request):
return self.PUT(request)
def PUT(self, request):
request = web.ctx.path
data = web.data()
data = BLOB_KEY.search(data)
if not data:
raise web.BadRequest()
data = data.group(1)
if not blobstore.BlobInfo.get(data):
# The blobKey does not exist
raise web.NotFound()
meta = _StoreMeta.get_by_key_name(request)
if meta:
# Delete the existing blob
blobstore.delete(meta.blobKey)
meta = _StoreMeta(key_name=request)
meta.blobKey = data
meta.put()
# This redirection is empty and useless,
# required from the appengine SDK...
raise web.HTTPError(status='302 Found')
def serve(self, request):
meta = _StoreMeta.get_by_key_name(request)
if not meta:
raise web.NotFound()
print 'X-AppEngine-BlobKey: %s' % meta.blobKey
def checkAuth(self):
if not web.ctx.env['REMOTE_ADDR'] in self.allowFrom and not users.is_current_user_admin():
raise web.Forbidden()
def cmdNew(self, request):
self.checkAuth()
try:
url = blobstore.create_upload_url(request).split('/')[3:]
except Exception:
raise web.HTTPError(status='501 Not Implemented')
url = '/' + '/'.join(url)
return '%s' % url
def cmdDelete(self, request):
self.checkAuth()
meta = _StoreMeta.get_by_key_name(request)
if not meta:
raise web.NotFound()
meta.delete()
blobstore.delete(meta.blobKey)
return 'OK'