2013-10-25 05:14:38 +00:00
|
|
|
import os
|
2013-10-25 22:18:06 +00:00
|
|
|
import logging
|
2015-09-08 14:29:28 +00:00
|
|
|
import urlparse
|
2013-10-25 05:14:38 +00:00
|
|
|
|
|
|
|
from uuid import uuid4
|
2016-04-15 17:51:54 +00:00
|
|
|
from _pyio import BufferedReader
|
|
|
|
|
|
|
|
import magic
|
|
|
|
|
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
|
2015-09-08 14:29:28 +00:00
|
|
|
from util import get_app_url
|
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:
|
2017-04-21 19:54:15 +00:00
|
|
|
logger.exception('Error reading user file')
|
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
|
|
|
|
|
|
|
|
2016-04-15 17:51:54 +00:00
|
|
|
class MissingHandlerException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
class DelegateUserfiles(object):
|
2016-04-15 17:51:54 +00:00
|
|
|
def __init__(self, app, distributed_storage, location, path, handler_name=None):
|
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):
|
2017-04-21 19:54:15 +00:00
|
|
|
# Note: We use basename here to prevent paths with ..'s and absolute paths.
|
2017-04-21 20:13:18 +00:00
|
|
|
return os.path.join(self._prefix or '', os.path.basename(file_id))
|
2014-09-09 19:54:03 +00:00
|
|
|
|
|
|
|
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:
|
2016-04-15 17:51:54 +00:00
|
|
|
if self._handler_name is None:
|
|
|
|
raise MissingHandlerException()
|
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
with self._app.app_context() as ctx:
|
|
|
|
ctx.url_adapter = self._build_url_adapter()
|
2015-09-08 14:29:28 +00:00
|
|
|
file_relative_url = url_for(self._handler_name, file_id=file_id)
|
|
|
|
file_url = urlparse.urljoin(get_app_url(self._app.config), file_relative_url)
|
|
|
|
return (file_url, file_id)
|
2014-09-09 19:54:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-10-05 19:20:16 +00:00
|
|
|
def get_file_url(self, file_id, remote_ip, expires_in=300, requires_cors=False):
|
2014-09-09 19:54:03 +00:00
|
|
|
path = self.get_file_id_path(file_id)
|
2017-10-05 19:20:16 +00:00
|
|
|
url = self._storage.get_direct_download_url(self._locations, path, remote_ip, expires_in,
|
2017-09-25 21:14:28 +00:00
|
|
|
requires_cors)
|
2014-04-17 02:35:47 +00:00
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
if url is None:
|
2016-04-15 17:51:54 +00:00
|
|
|
if self._handler_name is None:
|
|
|
|
raise MissingHandlerException()
|
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
with self._app.app_context() as ctx:
|
|
|
|
ctx.url_adapter = self._build_url_adapter()
|
2015-09-08 14:29:28 +00:00
|
|
|
file_relative_url = url_for(self._handler_name, file_id=file_id)
|
|
|
|
return urlparse.urljoin(get_app_url(self._app.config), file_relative_url)
|
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
|
|
|
|
2017-04-21 20:13:18 +00:00
|
|
|
if path is not None:
|
|
|
|
handler_name = 'userfiles_handlers'
|
|
|
|
userfiles = DelegateUserfiles(app, distributed_storage, location, path,
|
|
|
|
handler_name=handler_name)
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2017-04-21 20:13:18 +00:00
|
|
|
app.add_url_rule('/userfiles/<regex("[0-9a-zA-Z-]+"):file_id>',
|
|
|
|
view_func=UserfilesHandlers.as_view(handler_name,
|
|
|
|
distributed_storage=distributed_storage,
|
|
|
|
location=location,
|
|
|
|
files=userfiles))
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2017-04-21 20:13:18 +00:00
|
|
|
# register extension with app
|
|
|
|
app.extensions = getattr(app, 'extensions', {})
|
|
|
|
app.extensions['userfiles'] = userfiles
|
2014-04-10 19:20:16 +00:00
|
|
|
|
2014-04-03 21:31:46 +00:00
|
|
|
return userfiles
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
return getattr(self.state, name, None)
|