Small fixes to bugs in the streaming handler for use with magic and radosgw.
This commit is contained in:
parent
d2d51d15a2
commit
c9e1648781
4 changed files with 25 additions and 13 deletions
|
@ -89,10 +89,6 @@ class DefaultConfig(object):
|
||||||
# Stripe config
|
# Stripe config
|
||||||
BILLING_TYPE = 'FakeStripe'
|
BILLING_TYPE = 'FakeStripe'
|
||||||
|
|
||||||
# Userfiles
|
|
||||||
USERFILES_TYPE = 'LocalUserfiles'
|
|
||||||
USERFILES_PATH = 'test/data/registry/userfiles'
|
|
||||||
|
|
||||||
# Analytics
|
# Analytics
|
||||||
ANALYTICS_TYPE = 'FakeAnalytics'
|
ANALYTICS_TYPE = 'FakeAnalytics'
|
||||||
|
|
||||||
|
@ -172,3 +168,7 @@ class DefaultConfig(object):
|
||||||
}
|
}
|
||||||
|
|
||||||
DISTRIBUTED_STORAGE_PREFERENCE = ['local_us']
|
DISTRIBUTED_STORAGE_PREFERENCE = ['local_us']
|
||||||
|
|
||||||
|
# Userfiles
|
||||||
|
USERFILES_LOCATION = 'local_us'
|
||||||
|
USERFILES_PATH = 'userfiles/'
|
||||||
|
|
|
@ -5,6 +5,8 @@ import magic
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
from flask import url_for, request, send_file, make_response, abort
|
from flask import url_for, request, send_file, make_response, abort
|
||||||
from flask.views import View
|
from flask.views import View
|
||||||
|
from io import BufferedReader
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -22,7 +24,9 @@ class UserfilesHandlers(View):
|
||||||
path = self._files.get_file_id_path(file_id)
|
path = self._files.get_file_id_path(file_id)
|
||||||
try:
|
try:
|
||||||
file_stream = self._storage.stream_read_file(self._locations, path)
|
file_stream = self._storage.stream_read_file(self._locations, path)
|
||||||
return send_file(file_stream)
|
buffered = BufferedReader(file_stream)
|
||||||
|
file_header_bytes = buffered.peek(1024)
|
||||||
|
return send_file(buffered, mimetype=self._magic.from_buffer(file_header_bytes))
|
||||||
except IOError:
|
except IOError:
|
||||||
abort(404)
|
abort(404)
|
||||||
|
|
||||||
|
|
|
@ -7,23 +7,19 @@ import boto.gs.connection
|
||||||
import boto.s3.key
|
import boto.s3.key
|
||||||
import boto.gs.key
|
import boto.gs.key
|
||||||
|
|
||||||
|
from io import UnsupportedOperation, BufferedIOBase
|
||||||
|
|
||||||
from storage.basestorage import BaseStorage
|
from storage.basestorage import BaseStorage
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class StreamReadKeyAsFile(object):
|
class StreamReadKeyAsFile(BufferedIOBase):
|
||||||
def __init__(self, key):
|
def __init__(self, key):
|
||||||
self._key = key
|
self._key = key
|
||||||
self._finished = False
|
self._finished = False
|
||||||
|
|
||||||
def __enter__(self):
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __exit__(self, type, value, tb):
|
|
||||||
self._key.close(fast=True)
|
|
||||||
|
|
||||||
def read(self, amt=None):
|
def read(self, amt=None):
|
||||||
if self._finished:
|
if self._finished:
|
||||||
return None
|
return None
|
||||||
|
@ -33,6 +29,16 @@ class StreamReadKeyAsFile(object):
|
||||||
self._finished = True
|
self._finished = True
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
|
def readable(self):
|
||||||
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def closed(self):
|
||||||
|
return self._key.closed
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self._key.close(fast=True)
|
||||||
|
|
||||||
|
|
||||||
class _CloudStorage(BaseStorage):
|
class _CloudStorage(BaseStorage):
|
||||||
def __init__(self, connection_class, key_class, connect_kwargs, upload_params, storage_path,
|
def __init__(self, connection_class, key_class, connect_kwargs, upload_params, storage_path,
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
import hashlib
|
||||||
|
import io
|
||||||
|
|
||||||
from storage.basestorage import BaseStorage
|
from storage.basestorage import BaseStorage
|
||||||
|
|
||||||
|
@ -39,7 +41,7 @@ class LocalStorage(BaseStorage):
|
||||||
|
|
||||||
def stream_read_file(self, path):
|
def stream_read_file(self, path):
|
||||||
path = self._init_path(path)
|
path = self._init_path(path)
|
||||||
return open(path, mode='rb')
|
return io.open(path, mode='rb')
|
||||||
|
|
||||||
def stream_write(self, path, fp, content_type=None):
|
def stream_write(self, path, fp, content_type=None):
|
||||||
# Size is mandatory
|
# Size is mandatory
|
||||||
|
|
Reference in a new issue