2013-10-25 05:14:38 +00:00
|
|
|
import os
|
2013-10-25 22:18:06 +00:00
|
|
|
import logging
|
2014-04-11 22:34:47 +00:00
|
|
|
import magic
|
2013-10-25 05:14:38 +00:00
|
|
|
|
|
|
|
from uuid import uuid4
|
2014-04-17 02:43:57 +00:00
|
|
|
from flask import url_for, request, send_file, make_response, abort
|
2014-04-11 22:34:47 +00:00
|
|
|
from flask.views import View
|
2014-09-10 02:28:25 +00:00
|
|
|
from _pyio import BufferedReader
|
2013-10-25 05:14:38 +00:00
|
|
|
|
|
|
|
|
2013-10-25 22:18:06 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2014-04-11 22:34:47 +00:00
|
|
|
class UserfilesHandlers(View):
|
|
|
|
methods = ['GET', 'PUT']
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
def __init__(self, distributed_storage, location, files):
|
|
|
|
self._storage = distributed_storage
|
|
|
|
self._files = files
|
|
|
|
self._locations = {location}
|
2014-04-11 22:34:47 +00:00
|
|
|
self._magic = magic.Magic(mime=True)
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2014-04-11 22:34:47 +00:00
|
|
|
def get(self, file_id):
|
2014-09-09 19:54:03 +00:00
|
|
|
path = self._files.get_file_id_path(file_id)
|
|
|
|
try:
|
|
|
|
file_stream = self._storage.stream_read_file(self._locations, path)
|
2014-09-09 22:30:14 +00:00
|
|
|
buffered = BufferedReader(file_stream)
|
|
|
|
file_header_bytes = buffered.peek(1024)
|
|
|
|
return send_file(buffered, mimetype=self._magic.from_buffer(file_header_bytes))
|
2014-09-09 19:54:03 +00:00
|
|
|
except IOError:
|
2014-04-17 02:43:57 +00:00
|
|
|
abort(404)
|
|
|
|
|
2014-04-11 22:34:47 +00:00
|
|
|
def put(self, file_id):
|
|
|
|
input_stream = request.stream
|
|
|
|
if request.headers.get('transfer-encoding') == 'chunked':
|
|
|
|
# Careful, might work only with WSGI servers supporting chunked
|
|
|
|
# encoding (Gunicorn)
|
|
|
|
input_stream = request.environ['wsgi.input']
|
|
|
|
|
2014-09-09 20:52:53 +00:00
|
|
|
c_type = request.headers.get('Content-Type', None)
|
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
path = self._files.get_file_id_path(file_id)
|
2014-09-09 20:52:53 +00:00
|
|
|
self._storage.stream_write(self._locations, path, input_stream, c_type)
|
2014-04-11 22:34:47 +00:00
|
|
|
|
2014-04-17 02:35:47 +00:00
|
|
|
return make_response('Okay')
|
|
|
|
|
2014-04-11 22:34:47 +00:00
|
|
|
def dispatch_request(self, file_id):
|
|
|
|
if request.method == 'GET':
|
|
|
|
return self.get(file_id)
|
|
|
|
elif request.method == 'PUT':
|
|
|
|
return self.put(file_id)
|
2014-04-03 21:31:46 +00:00
|
|
|
|
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
class DelegateUserfiles(object):
|
|
|
|
def __init__(self, app, distributed_storage, location, path, handler_name):
|
2014-04-11 22:34:47 +00:00
|
|
|
self._app = app
|
2014-09-09 19:54:03 +00:00
|
|
|
self._storage = distributed_storage
|
|
|
|
self._locations = {location}
|
|
|
|
self._prefix = path
|
|
|
|
self._handler_name = handler_name
|
2014-04-11 22:34:47 +00:00
|
|
|
|
|
|
|
def _build_url_adapter(self):
|
|
|
|
return self._app.url_map.bind(self._app.config['SERVER_HOSTNAME'],
|
|
|
|
script_name=self._app.config['APPLICATION_ROOT'] or '/',
|
|
|
|
url_scheme=self._app.config['PREFERRED_URL_SCHEME'])
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
def get_file_id_path(self, file_id):
|
|
|
|
return os.path.join(self._prefix, file_id)
|
|
|
|
|
|
|
|
def prepare_for_drop(self, mime_type, requires_cors=True):
|
|
|
|
""" Returns a signed URL to upload a file to our bucket. """
|
|
|
|
logger.debug('Requested upload url with content type: %s' % mime_type)
|
2014-04-03 21:31:46 +00:00
|
|
|
file_id = str(uuid4())
|
2014-09-09 19:54:03 +00:00
|
|
|
path = self.get_file_id_path(file_id)
|
|
|
|
url = self._storage.get_direct_upload_url(self._locations, path, mime_type, requires_cors)
|
|
|
|
|
|
|
|
if url is None:
|
|
|
|
with self._app.app_context() as ctx:
|
|
|
|
ctx.url_adapter = self._build_url_adapter()
|
|
|
|
return (url_for(self._handler_name, file_id=file_id, _external=True), file_id)
|
|
|
|
|
|
|
|
return (url, file_id)
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2014-09-11 19:33:10 +00:00
|
|
|
def store_file(self, file_like_obj, content_type, content_encoding=None, file_id=None):
|
2014-09-08 20:43:17 +00:00
|
|
|
if file_id is None:
|
|
|
|
file_id = str(uuid4())
|
2014-04-17 02:35:47 +00:00
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
path = self.get_file_id_path(file_id)
|
2014-09-11 19:33:10 +00:00
|
|
|
self._storage.stream_write(self._locations, path, file_like_obj, content_type,
|
|
|
|
content_encoding)
|
2014-04-03 21:31:46 +00:00
|
|
|
return file_id
|
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
def get_file_url(self, file_id, expires_in=300, requires_cors=False):
|
|
|
|
path = self.get_file_id_path(file_id)
|
|
|
|
url = self._storage.get_direct_download_url(self._locations, path, expires_in, requires_cors)
|
2014-04-17 02:35:47 +00:00
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
if url is None:
|
|
|
|
with self._app.app_context() as ctx:
|
|
|
|
ctx.url_adapter = self._build_url_adapter()
|
|
|
|
return url_for(self._handler_name, file_id=file_id, _external=True)
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
return url
|
2014-04-03 21:31:46 +00:00
|
|
|
|
|
|
|
def get_file_checksum(self, file_id):
|
2014-09-09 19:54:03 +00:00
|
|
|
path = self.get_file_id_path(file_id)
|
|
|
|
return self._storage.get_checksum(self._locations, path)
|
2014-04-03 21:31:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Userfiles(object):
|
2014-09-09 19:54:03 +00:00
|
|
|
def __init__(self, app=None, distributed_storage=None):
|
2014-04-03 21:31:46 +00:00
|
|
|
self.app = app
|
|
|
|
if app is not None:
|
2014-09-09 19:54:03 +00:00
|
|
|
self.state = self.init_app(app, distributed_storage)
|
2014-04-03 21:31:46 +00:00
|
|
|
else:
|
|
|
|
self.state = None
|
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
def init_app(self, app, distributed_storage):
|
|
|
|
location = app.config.get('USERFILES_LOCATION')
|
|
|
|
path = app.config.get('USERFILES_PATH', None)
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
handler_name = 'userfiles_handlers'
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
userfiles = DelegateUserfiles(app, distributed_storage, location, path, handler_name)
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
app.add_url_rule('/userfiles/<file_id>',
|
|
|
|
view_func=UserfilesHandlers.as_view(handler_name,
|
|
|
|
distributed_storage=distributed_storage,
|
|
|
|
location=location,
|
|
|
|
files=userfiles))
|
2014-04-10 19:20:16 +00:00
|
|
|
|
2014-04-03 21:31:46 +00:00
|
|
|
# register extension with app
|
|
|
|
app.extensions = getattr(app, 'extensions', {})
|
|
|
|
app.extensions['userfiles'] = userfiles
|
|
|
|
return userfiles
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
return getattr(self.state, name, None)
|