2010-06-21 01:21:58 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Camlistore blob server for App Engine.
|
|
|
|
#
|
|
|
|
# Derived from Brad's Brackup-gae utility:
|
|
|
|
# http://github.com/bradfitz/brackup-gae-server
|
|
|
|
#
|
2011-01-28 07:03:46 +00:00
|
|
|
# Copyright 2010 Google Inc.
|
2010-06-21 01:21:58 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
|
|
|
"""Upload server for camlistore.
|
|
|
|
|
|
|
|
To test:
|
|
|
|
|
2011-02-08 16:24:16 +00:00
|
|
|
# Stat -- 200 response
|
2010-08-03 04:05:54 +00:00
|
|
|
curl -v \
|
2010-09-08 04:32:12 +00:00
|
|
|
-d camliversion=1 \
|
2011-02-08 16:24:16 +00:00
|
|
|
http://localhost:8080/camli/stat
|
2010-08-03 04:05:54 +00:00
|
|
|
|
|
|
|
# Upload -- 200 response
|
2010-06-21 01:21:58 +00:00
|
|
|
curl -v -L \
|
2010-08-03 04:05:54 +00:00
|
|
|
-F sha1-126249fd8c18cbb5312a5705746a2af87fba9538=@./test_data.txt \
|
2011-02-08 16:24:16 +00:00
|
|
|
<the url returned by stat>
|
2010-06-21 01:21:58 +00:00
|
|
|
|
|
|
|
# Put with bad blob_ref parameter -- 400 response
|
|
|
|
curl -v -L \
|
2010-08-03 04:05:54 +00:00
|
|
|
-F sha1-22a7fdd575f4c3e7caa3a55cc83db8b8a6714f0f=@./test_data.txt \
|
2011-02-08 16:24:16 +00:00
|
|
|
<the url returned by stat>
|
2010-06-21 01:21:58 +00:00
|
|
|
|
|
|
|
# Get present -- the blob
|
2010-08-03 04:05:54 +00:00
|
|
|
curl -v http://localhost:8080/camli/\
|
2010-06-21 01:32:42 +00:00
|
|
|
sha1-126249fd8c18cbb5312a5705746a2af87fba9538
|
2010-06-21 01:21:58 +00:00
|
|
|
|
|
|
|
# Get missing -- 404
|
2010-08-03 04:05:54 +00:00
|
|
|
curl -v http://localhost:8080/camli/\
|
2010-06-21 01:32:42 +00:00
|
|
|
sha1-22a7fdd575f4c3e7caa3a55cc83db8b8a6714f0f
|
2010-06-21 01:21:58 +00:00
|
|
|
|
2010-08-03 04:05:54 +00:00
|
|
|
# Check present -- 200 with only headers
|
|
|
|
curl -I http://localhost:8080/camli/\
|
2010-06-21 01:32:42 +00:00
|
|
|
sha1-126249fd8c18cbb5312a5705746a2af87fba9538
|
2010-06-21 01:21:58 +00:00
|
|
|
|
|
|
|
# Check missing -- 404 with empty list response
|
2010-08-03 04:05:54 +00:00
|
|
|
curl -I http://localhost:8080/camli/\
|
2010-06-21 01:32:42 +00:00
|
|
|
sha1-22a7fdd575f4c3e7caa3a55cc83db8b8a6714f0f
|
2010-06-21 01:21:58 +00:00
|
|
|
|
|
|
|
# List -- 200 with list of blobs (just one)
|
2010-08-03 04:05:54 +00:00
|
|
|
curl -v http://localhost:8080/camli/enumerate-blobs&limit=1
|
2010-06-21 01:21:58 +00:00
|
|
|
|
|
|
|
# List offset -- 200 with list of no blobs
|
2010-08-03 04:05:54 +00:00
|
|
|
curl -v http://localhost:8080/camli/enumerate-blobs?after=\
|
2010-06-21 01:32:42 +00:00
|
|
|
sha1-126249fd8c18cbb5312a5705746a2af87fba9538
|
2010-06-21 01:21:58 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2010-08-03 04:05:54 +00:00
|
|
|
import cgi
|
2010-06-21 01:21:58 +00:00
|
|
|
import hashlib
|
2010-09-08 04:32:12 +00:00
|
|
|
import logging
|
2010-06-21 01:21:58 +00:00
|
|
|
import urllib
|
|
|
|
import wsgiref.handlers
|
|
|
|
|
|
|
|
from google.appengine.ext import blobstore
|
|
|
|
from google.appengine.ext import db
|
|
|
|
from google.appengine.ext import webapp
|
|
|
|
from google.appengine.ext.webapp import blobstore_handlers
|
|
|
|
|
|
|
|
import config
|
|
|
|
|
|
|
|
|
|
|
|
class Blob(db.Model):
|
|
|
|
"""Some content-addressable blob.
|
|
|
|
|
|
|
|
The key is the algorithm, dash, and the lowercase hex digest:
|
|
|
|
"sha1-f1d2d2f924e986ac86fdf7b36c94bcdf32beec15"
|
|
|
|
"""
|
|
|
|
|
|
|
|
# The actual bytes.
|
|
|
|
blob = blobstore.BlobReferenceProperty(indexed=False)
|
|
|
|
|
2010-08-03 04:05:54 +00:00
|
|
|
# Size. (already in the blobinfo, but denormalized for speed)
|
2010-06-21 01:21:58 +00:00
|
|
|
size = db.IntegerProperty(indexed=False)
|
|
|
|
|
|
|
|
|
2010-09-08 05:09:13 +00:00
|
|
|
class HelloHandler(webapp.RequestHandler):
|
|
|
|
"""Present ourselves to the world."""
|
|
|
|
|
|
|
|
def get(self):
|
2010-11-05 06:06:43 +00:00
|
|
|
self.response.out.write('Hello! This is an AppEngine Camlistore '
|
|
|
|
'blob server.<p>')
|
|
|
|
self.response.out.write('<a href=js/index.html>js frontend</a>')
|
2010-09-08 05:09:13 +00:00
|
|
|
|
|
|
|
|
2010-06-21 01:21:58 +00:00
|
|
|
class ListHandler(webapp.RequestHandler):
|
|
|
|
"""Return chunks that the server has."""
|
|
|
|
|
2010-08-03 04:05:54 +00:00
|
|
|
def get(self):
|
|
|
|
after_blob_ref = self.request.get('after')
|
|
|
|
limit = max(1, min(1000, int(self.request.get('limit') or 1000)))
|
2010-06-21 01:21:58 +00:00
|
|
|
query = Blob.all().order('__key__')
|
|
|
|
if after_blob_ref:
|
|
|
|
query.filter('__key__ >', db.Key.from_path(Blob.kind(), after_blob_ref))
|
2010-08-03 04:05:54 +00:00
|
|
|
blob_ref_list = query.fetch(limit)
|
|
|
|
|
|
|
|
self.response.headers['Content-Type'] = 'text/javascript'
|
|
|
|
out = [
|
|
|
|
'{\n'
|
|
|
|
' "blobs": ['
|
|
|
|
]
|
|
|
|
if blob_ref_list:
|
|
|
|
out.extend([
|
|
|
|
'\n ',
|
|
|
|
',\n '.join(
|
|
|
|
'{"blobRef": "%s", "size": %d}' %
|
|
|
|
(b.key().name(), b.size) for b in blob_ref_list),
|
|
|
|
'\n ',
|
|
|
|
])
|
|
|
|
if blob_ref_list and len(blob_ref_list) == limit:
|
|
|
|
out.append(
|
|
|
|
'],'
|
2011-02-04 01:06:01 +00:00
|
|
|
'\n "continueAfter": "%s"\n'
|
2010-08-03 04:05:54 +00:00
|
|
|
'}' % blob_ref_list[-1].key().name())
|
|
|
|
else:
|
|
|
|
out.append(
|
|
|
|
']\n'
|
|
|
|
'}'
|
|
|
|
)
|
|
|
|
self.response.out.write(''.join(out))
|
2010-06-21 01:21:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GetHandler(blobstore_handlers.BlobstoreDownloadHandler):
|
|
|
|
"""Gets a blob with the given ref."""
|
|
|
|
|
2010-08-03 04:05:54 +00:00
|
|
|
def head(self, blob_ref):
|
|
|
|
self.get(blob_ref)
|
|
|
|
|
2010-06-21 01:32:42 +00:00
|
|
|
def get(self, blob_ref):
|
2010-06-21 01:21:58 +00:00
|
|
|
blob = Blob.get_by_key_name(blob_ref)
|
|
|
|
if not blob:
|
|
|
|
self.error(404)
|
|
|
|
return
|
|
|
|
self.send_blob(blob.blob, 'application/octet-stream')
|
|
|
|
|
|
|
|
|
2011-02-08 16:24:16 +00:00
|
|
|
class StatHandler(webapp.RequestHandler):
|
2010-08-03 04:05:54 +00:00
|
|
|
"""Handler to return a URL for a script to get an upload URL."""
|
2010-06-21 01:21:58 +00:00
|
|
|
|
2011-02-08 16:24:16 +00:00
|
|
|
def stat_key(self):
|
|
|
|
return "stat"
|
|
|
|
|
2010-08-03 04:05:54 +00:00
|
|
|
def get(self):
|
2011-02-08 16:24:16 +00:00
|
|
|
self.handle()
|
2010-06-21 01:21:58 +00:00
|
|
|
|
2010-08-03 04:05:54 +00:00
|
|
|
def post(self):
|
2011-02-08 16:24:16 +00:00
|
|
|
self.handle()
|
2010-06-21 01:21:58 +00:00
|
|
|
|
2011-02-08 16:24:16 +00:00
|
|
|
def handle(self):
|
2010-08-03 04:05:54 +00:00
|
|
|
if self.request.get('camliversion') != '1':
|
|
|
|
self.response.headers['Content-Type'] = 'text/plain'
|
|
|
|
self.response.out.write('Bad parameter: "camliversion"')
|
|
|
|
self.response.set_status(400)
|
|
|
|
return
|
2010-06-21 01:21:58 +00:00
|
|
|
|
2010-08-03 04:05:54 +00:00
|
|
|
blob_ref_list = []
|
|
|
|
for key, value in self.request.params.items():
|
|
|
|
if not key.startswith('blob'):
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
int(key[len('blob'):])
|
|
|
|
except ValueError:
|
2010-09-08 04:32:12 +00:00
|
|
|
logging.exception('Bad parameter: %s', key)
|
2010-08-03 04:05:54 +00:00
|
|
|
self.response.headers['Content-Type'] = 'text/plain'
|
|
|
|
self.response.out.write('Bad parameter: "%s"' % key)
|
|
|
|
self.response.set_status(400)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
blob_ref_list.append(value)
|
|
|
|
|
2011-02-08 16:24:16 +00:00
|
|
|
key_name = self.stat_key()
|
2010-08-03 04:05:54 +00:00
|
|
|
|
|
|
|
self.response.headers['Content-Type'] = 'text/javascript'
|
|
|
|
out = [
|
|
|
|
'{\n'
|
|
|
|
' "maxUploadSize": %d,\n'
|
|
|
|
' "uploadUrl": "%s",\n'
|
|
|
|
' "uploadUrlExpirationSeconds": 600,\n'
|
|
|
|
' "%s": [\n'
|
|
|
|
% (config.MAX_UPLOAD_SIZE,
|
|
|
|
blobstore.create_upload_url('/upload_complete'),
|
2011-02-08 16:24:16 +00:00
|
|
|
key_name)
|
2010-08-03 04:05:54 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
already_have = db.get([
|
|
|
|
db.Key.from_path(Blob.kind(), b) for b in blob_ref_list])
|
|
|
|
if already_have:
|
|
|
|
out.extend([
|
|
|
|
'\n ',
|
|
|
|
',\n '.join(
|
|
|
|
'{"blobRef": "%s", "size": %d}' %
|
2010-09-08 04:32:12 +00:00
|
|
|
(b.key().name(), b.size) for b in already_have if b is not None),
|
2010-08-03 04:05:54 +00:00
|
|
|
'\n ',
|
|
|
|
])
|
|
|
|
out.append(
|
|
|
|
']\n'
|
|
|
|
'}'
|
|
|
|
)
|
|
|
|
self.response.out.write(''.join(out))
|
2010-06-21 01:21:58 +00:00
|
|
|
|
|
|
|
|
2011-02-08 16:24:16 +00:00
|
|
|
class PostUploadHandler(StatHandler):
|
|
|
|
|
|
|
|
def stat_key(self):
|
|
|
|
return "received"
|
|
|
|
|
|
|
|
|
2010-06-21 01:21:58 +00:00
|
|
|
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
|
|
|
|
"""Handle blobstore post, as forwarded by notification agent."""
|
|
|
|
|
|
|
|
def compute_blob_ref(self, hash_func, blob_key):
|
|
|
|
"""Computes the blob ref for a blob stored using the given hash function.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
hash_func: The name of the hash function (sha1, md5)
|
|
|
|
blob_key: The BlobKey of the App Engine blob containing the blob's data.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A newly computed blob_ref for the data.
|
|
|
|
"""
|
|
|
|
hasher = hashlib.new(hash_func)
|
|
|
|
last_index = 0
|
|
|
|
while True:
|
|
|
|
data = blobstore.fetch_data(
|
|
|
|
blob_key, last_index, last_index + blobstore.MAX_BLOB_FETCH_SIZE - 1)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
hasher.update(data)
|
|
|
|
last_index += len(data)
|
|
|
|
|
|
|
|
return '%s-%s' % (hash_func, hasher.hexdigest())
|
|
|
|
|
2010-08-03 04:05:54 +00:00
|
|
|
def store_blob(self, blob_ref, blob_info, error_messages):
|
2010-06-21 01:21:58 +00:00
|
|
|
"""Store blob information.
|
|
|
|
|
|
|
|
Writes a Blob to the datastore for the uploaded file.
|
|
|
|
|
|
|
|
Args:
|
2010-08-03 04:05:54 +00:00
|
|
|
blob_ref: The file that was uploaded.
|
|
|
|
upload_file: List of BlobInfo records representing the uploads.
|
2010-06-21 01:21:58 +00:00
|
|
|
error_messages: Empty list for storing error messages to report to user.
|
|
|
|
"""
|
|
|
|
if not blob_ref.startswith('sha1-'):
|
|
|
|
error_messages.append('Only sha1 supported for now.')
|
|
|
|
return
|
|
|
|
|
|
|
|
if len(blob_ref) != (len('sha1-') + 40):
|
2010-08-03 04:05:54 +00:00
|
|
|
error_messages.append('Bogus blobRef.')
|
2010-06-21 01:21:58 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
found_blob_ref = self.compute_blob_ref('sha1', blob_info.key())
|
|
|
|
if blob_ref != found_blob_ref:
|
|
|
|
error_messages.append('Found blob ref %s, expected %s' %
|
|
|
|
(found_blob_ref, blob_ref))
|
|
|
|
return
|
|
|
|
|
|
|
|
def txn():
|
2010-09-08 04:32:12 +00:00
|
|
|
logging.info('Saving blob "%s" with size %d', blob_ref, blob_info.size)
|
2010-08-03 04:05:54 +00:00
|
|
|
blob = Blob(key_name=blob_ref, blob=blob_info.key(), size=blob_info.size)
|
2010-06-21 01:21:58 +00:00
|
|
|
blob.put()
|
|
|
|
db.run_in_transaction(txn)
|
|
|
|
|
2010-08-03 04:05:54 +00:00
|
|
|
def post(self):
|
2010-06-21 01:21:58 +00:00
|
|
|
"""Do upload post."""
|
|
|
|
error_messages = []
|
2010-08-03 04:05:54 +00:00
|
|
|
blob_info_dict = {}
|
2010-06-21 01:21:58 +00:00
|
|
|
|
2010-08-03 04:05:54 +00:00
|
|
|
for key, value in self.request.params.items():
|
|
|
|
if isinstance(value, cgi.FieldStorage):
|
|
|
|
if 'blob-key' in value.type_options:
|
|
|
|
blob_info = blobstore.parse_blob_info(value)
|
|
|
|
blob_info_dict[value.name] = blob_info
|
2011-01-13 18:00:35 +00:00
|
|
|
logging.info("got blob: %s" % value.name)
|
2010-08-03 04:05:54 +00:00
|
|
|
self.store_blob(value.name, blob_info, error_messages)
|
2010-06-21 01:21:58 +00:00
|
|
|
|
|
|
|
if error_messages:
|
2010-09-08 04:32:12 +00:00
|
|
|
logging.error('Upload errors: %r', error_messages)
|
2010-08-03 04:05:54 +00:00
|
|
|
blobstore.delete(blob_info_dict.values())
|
2011-01-13 18:00:35 +00:00
|
|
|
self.response.set_status(303)
|
|
|
|
# TODO: fix up this format
|
|
|
|
self.response.headers.add_header("Location", '/error?%s' % '&'.join(
|
2010-06-21 01:21:58 +00:00
|
|
|
'error_message=%s' % urllib.quote(m) for m in error_messages))
|
|
|
|
else:
|
2010-09-08 04:32:12 +00:00
|
|
|
query = ['/nonstandard/upload_complete?camliversion=1']
|
|
|
|
query.extend('blob%d=%s' % (i + 1, k)
|
|
|
|
for i, k in enumerate(blob_info_dict.iterkeys()))
|
2010-12-16 19:13:29 +00:00
|
|
|
self.response.set_status(303)
|
2010-12-17 02:54:36 +00:00
|
|
|
self.response.headers.add_header("Location", str('&'.join(query)))
|
2010-06-21 01:21:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ErrorHandler(webapp.RequestHandler):
|
|
|
|
"""The blob put failed."""
|
|
|
|
|
|
|
|
def get(self):
|
|
|
|
self.response.headers['Content-Type'] = 'text/plain'
|
|
|
|
self.response.out.write('\n'.join(self.request.get_all('error_message')))
|
|
|
|
self.response.set_status(400)
|
|
|
|
|
|
|
|
|
2011-01-13 18:00:35 +00:00
|
|
|
class DebugUploadForm(webapp.RequestHandler):
|
|
|
|
def get(self):
|
|
|
|
self.response.headers['Content-Type'] = 'text/html'
|
|
|
|
uploadurl = blobstore.create_upload_url('/upload_complete')
|
|
|
|
self.response.out.write('<body><form method="post" enctype="multipart/form-data" action="%s">' % uploadurl)
|
|
|
|
self.response.out.write('<input type="file" name="sha1-f628050e63819347a095645ad9ae697415664f0">')
|
|
|
|
self.response.out.write('<input type="submit"></form></body>')
|
|
|
|
|
|
|
|
|
2010-06-21 01:21:58 +00:00
|
|
|
APP = webapp.WSGIApplication(
|
|
|
|
[
|
2010-09-08 05:09:13 +00:00
|
|
|
('/', HelloHandler),
|
2011-01-13 18:00:35 +00:00
|
|
|
('/debug/upform', DebugUploadForm),
|
2010-08-03 04:05:54 +00:00
|
|
|
('/camli/enumerate-blobs', ListHandler),
|
2011-02-08 16:24:16 +00:00
|
|
|
('/camli/stat', StatHandler),
|
2010-08-03 04:05:54 +00:00
|
|
|
('/camli/([^/]+)', GetHandler),
|
2011-02-08 16:24:16 +00:00
|
|
|
('/nonstandard/upload_complete', PostUploadHandler),
|
2010-08-03 04:05:54 +00:00
|
|
|
('/upload_complete', UploadHandler), # Admin only.
|
2010-06-21 01:21:58 +00:00
|
|
|
('/error', ErrorHandler),
|
|
|
|
],
|
|
|
|
debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
wsgiref.handlers.CGIHandler().run(APP)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|