2013-10-25 05:14:38 +00:00
|
|
|
import boto
|
|
|
|
import os
|
2013-10-25 22:18:06 +00:00
|
|
|
import logging
|
2014-04-03 21:31:46 +00:00
|
|
|
import hashlib
|
2014-04-11 22:34:47 +00:00
|
|
|
import magic
|
2013-10-25 05:14:38 +00:00
|
|
|
|
|
|
|
from boto.s3.key import Key
|
|
|
|
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
|
2013-10-25 05:14:38 +00:00
|
|
|
|
|
|
|
|
2013-10-25 22:18:06 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2014-04-03 21:31:46 +00:00
|
|
|
class FakeUserfiles(object):
|
|
|
|
def prepare_for_drop(self, mime_type):
|
|
|
|
return ('http://fake/url', uuid4())
|
|
|
|
|
|
|
|
def store_file(self, file_like_obj, content_type):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def get_file_url(self, file_id, expires_in=300):
|
|
|
|
return ('http://fake/url')
|
|
|
|
|
|
|
|
def get_file_checksum(self, file_id):
|
|
|
|
return 'abcdefg'
|
|
|
|
|
|
|
|
|
2013-10-25 05:14:38 +00:00
|
|
|
class S3FileWriteException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-04-03 21:31:46 +00:00
|
|
|
class S3Userfiles(object):
|
|
|
|
def __init__(self, path, s3_access_key, s3_secret_key, bucket_name):
|
2013-12-04 00:39:07 +00:00
|
|
|
self._initialized = False
|
2013-10-26 21:20:59 +00:00
|
|
|
self._bucket_name = bucket_name
|
|
|
|
self._access_key = s3_access_key
|
|
|
|
self._secret_key = s3_secret_key
|
2014-04-03 21:31:46 +00:00
|
|
|
self._prefix = path
|
2013-12-04 00:39:07 +00:00
|
|
|
self._s3_conn = None
|
|
|
|
self._bucket = None
|
|
|
|
|
|
|
|
def _initialize_s3(self):
|
|
|
|
if not self._initialized:
|
|
|
|
self._s3_conn = boto.connect_s3(self._access_key, self._secret_key)
|
|
|
|
self._bucket = self._s3_conn.get_bucket(self._bucket_name)
|
|
|
|
self._initialized = True
|
2013-10-25 05:14:38 +00:00
|
|
|
|
2013-10-26 22:37:53 +00:00
|
|
|
def prepare_for_drop(self, mime_type):
|
2013-10-26 21:20:59 +00:00
|
|
|
""" Returns a signed URL to upload a file to our bucket. """
|
2013-12-04 00:39:07 +00:00
|
|
|
self._initialize_s3()
|
2013-10-26 22:37:53 +00:00
|
|
|
logger.debug('Requested upload url with content type: %s' % mime_type)
|
|
|
|
file_id = str(uuid4())
|
|
|
|
full_key = os.path.join(self._prefix, file_id)
|
|
|
|
k = Key(self._bucket, full_key)
|
2013-10-31 15:32:08 +00:00
|
|
|
url = k.generate_url(300, 'PUT', headers={'Content-Type': mime_type},
|
|
|
|
encrypt_key=True)
|
2013-10-26 21:20:59 +00:00
|
|
|
return (url, file_id)
|
|
|
|
|
2014-02-18 23:09:14 +00:00
|
|
|
def store_file(self, file_like_obj, content_type):
|
2013-12-04 00:39:07 +00:00
|
|
|
self._initialize_s3()
|
2013-10-25 05:14:38 +00:00
|
|
|
file_id = str(uuid4())
|
|
|
|
full_key = os.path.join(self._prefix, file_id)
|
2013-10-25 22:18:06 +00:00
|
|
|
k = Key(self._bucket, full_key)
|
2014-02-18 23:09:14 +00:00
|
|
|
logger.debug('Setting s3 content type to: %s' % content_type)
|
|
|
|
k.set_metadata('Content-Type', content_type)
|
|
|
|
bytes_written = k.set_contents_from_file(file_like_obj, encrypt_key=True,
|
|
|
|
rewind=True)
|
2013-10-25 05:14:38 +00:00
|
|
|
|
|
|
|
if bytes_written == 0:
|
|
|
|
raise S3FileWriteException('Unable to write file to S3')
|
|
|
|
|
|
|
|
return file_id
|
|
|
|
|
2014-04-01 04:23:53 +00:00
|
|
|
def get_file_url(self, file_id, expires_in=300, mime_type=None):
|
2013-12-04 00:39:07 +00:00
|
|
|
self._initialize_s3()
|
2013-10-25 05:14:38 +00:00
|
|
|
full_key = os.path.join(self._prefix, file_id)
|
2013-10-25 22:18:06 +00:00
|
|
|
k = Key(self._bucket, full_key)
|
2014-04-01 04:23:53 +00:00
|
|
|
headers = None
|
|
|
|
if mime_type:
|
|
|
|
headers={'Content-Type': mime_type}
|
|
|
|
|
|
|
|
return k.generate_url(expires_in, headers=headers)
|
2014-02-12 18:52:12 +00:00
|
|
|
|
|
|
|
def get_file_checksum(self, file_id):
|
|
|
|
self._initialize_s3()
|
|
|
|
full_key = os.path.join(self._prefix, file_id)
|
|
|
|
k = self._bucket.lookup(full_key)
|
|
|
|
return k.etag[1:-1][:7]
|
2014-04-03 21:31:46 +00:00
|
|
|
|
|
|
|
|
2014-04-11 22:34:47 +00:00
|
|
|
class UserfilesHandlers(View):
|
|
|
|
methods = ['GET', 'PUT']
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2014-04-11 22:34:47 +00:00
|
|
|
def __init__(self, local_userfiles):
|
|
|
|
self._userfiles = local_userfiles
|
|
|
|
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):
|
|
|
|
path = self._userfiles.file_path(file_id)
|
2014-04-17 02:43:57 +00:00
|
|
|
if not os.path.exists(path):
|
|
|
|
abort(404)
|
|
|
|
|
2014-04-11 22:34:47 +00:00
|
|
|
logger.debug('Sending path: %s' % path)
|
|
|
|
return send_file(path, mimetype=self._magic.from_file(path))
|
|
|
|
|
|
|
|
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']
|
|
|
|
|
|
|
|
self._userfiles.store_stream(input_stream, file_id)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
class LocalUserfiles(object):
|
2014-04-11 22:34:47 +00:00
|
|
|
def __init__(self, app, path):
|
2014-04-03 21:31:46 +00:00
|
|
|
self._root_path = path
|
|
|
|
self._buffer_size = 64 * 1024 # 64 KB
|
2014-04-11 22:34:47 +00:00
|
|
|
self._app = app
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def prepare_for_drop(self, mime_type):
|
|
|
|
file_id = str(uuid4())
|
2014-04-11 22:34:47 +00:00
|
|
|
with self._app.app_context() as ctx:
|
|
|
|
ctx.url_adapter = self._build_url_adapter()
|
|
|
|
return (url_for('userfiles_handlers', file_id=file_id, _external=True), file_id)
|
|
|
|
|
|
|
|
def file_path(self, file_id):
|
|
|
|
if '..' in file_id or file_id.startswith('/'):
|
|
|
|
raise RuntimeError('Invalid Filename')
|
|
|
|
return os.path.join(self._root_path, file_id)
|
|
|
|
|
|
|
|
def store_stream(self, stream, file_id):
|
|
|
|
path = self.file_path(file_id)
|
|
|
|
dirname = os.path.dirname(path)
|
|
|
|
if not os.path.exists(dirname):
|
|
|
|
os.makedirs(dirname)
|
2014-04-03 21:31:46 +00:00
|
|
|
|
|
|
|
with open(path, 'w') as to_write:
|
|
|
|
while True:
|
|
|
|
try:
|
2014-04-11 22:34:47 +00:00
|
|
|
buf = stream.read(self._buffer_size)
|
2014-04-03 21:31:46 +00:00
|
|
|
if not buf:
|
|
|
|
break
|
|
|
|
to_write.write(buf)
|
|
|
|
except IOError:
|
|
|
|
break
|
|
|
|
|
2014-04-11 22:34:47 +00:00
|
|
|
def store_file(self, file_like_obj, content_type):
|
|
|
|
file_id = str(uuid4())
|
2014-04-17 02:35:47 +00:00
|
|
|
|
|
|
|
# Rewind the file to match what s3 does
|
|
|
|
file_like_obj.seek(0, os.SEEK_SET)
|
|
|
|
|
|
|
|
self.store_stream(file_like_obj, file_id)
|
2014-04-03 21:31:46 +00:00
|
|
|
return file_id
|
|
|
|
|
|
|
|
def get_file_url(self, file_id, expires_in=300):
|
2014-04-11 22:34:47 +00:00
|
|
|
with self._app.app_context() as ctx:
|
|
|
|
ctx.url_adapter = self._build_url_adapter()
|
|
|
|
return url_for('userfiles_handlers', file_id=file_id, _external=True)
|
2014-04-03 21:31:46 +00:00
|
|
|
|
|
|
|
def get_file_checksum(self, file_id):
|
2014-04-11 22:34:47 +00:00
|
|
|
path = self.file_path(file_id)
|
2014-04-03 21:31:46 +00:00
|
|
|
sha_hash = hashlib.sha256()
|
|
|
|
with open(path, 'r') as to_hash:
|
|
|
|
while True:
|
|
|
|
buf = to_hash.read(self._buffer_size)
|
|
|
|
if not buf:
|
|
|
|
break
|
|
|
|
sha_hash.update(buf)
|
|
|
|
return sha_hash.hexdigest()[:7]
|
|
|
|
|
|
|
|
|
|
|
|
class Userfiles(object):
|
|
|
|
def __init__(self, app=None):
|
|
|
|
self.app = app
|
|
|
|
if app is not None:
|
|
|
|
self.state = self.init_app(app)
|
|
|
|
else:
|
|
|
|
self.state = None
|
|
|
|
|
|
|
|
def init_app(self, app):
|
|
|
|
storage_type = app.config.get('USERFILES_TYPE', 'LocalUserfiles')
|
|
|
|
path = app.config.get('USERFILES_PATH', '')
|
|
|
|
|
|
|
|
if storage_type == 'LocalUserfiles':
|
2014-04-11 22:34:47 +00:00
|
|
|
userfiles = LocalUserfiles(app, path)
|
|
|
|
app.add_url_rule('/userfiles/<file_id>',
|
|
|
|
view_func=UserfilesHandlers.as_view('userfiles_handlers',
|
|
|
|
local_userfiles=userfiles))
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2014-04-10 19:20:16 +00:00
|
|
|
elif storage_type == 'S3Userfiles':
|
2014-04-03 21:31:46 +00:00
|
|
|
access_key = app.config.get('USERFILES_AWS_ACCESS_KEY', '')
|
|
|
|
secret_key = app.config.get('USERFILES_AWS_SECRET_KEY', '')
|
|
|
|
bucket = app.config.get('USERFILES_S3_BUCKET', '')
|
|
|
|
userfiles = S3Userfiles(path, access_key, secret_key, bucket)
|
|
|
|
|
2014-04-10 19:20:16 +00:00
|
|
|
elif storage_type == 'FakeUserfiles':
|
2014-04-03 21:31:46 +00:00
|
|
|
userfiles = FakeUserfiles()
|
|
|
|
|
2014-04-10 19:20:16 +00:00
|
|
|
else:
|
|
|
|
raise RuntimeError('Unknown userfiles type: %s' % storage_type)
|
|
|
|
|
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)
|