Small fixes to bugs in the streaming handler for use with magic and radosgw.

This commit is contained in:
Jake Moshenko 2014-09-09 18:30:14 -04:00
parent d2d51d15a2
commit c9e1648781
4 changed files with 25 additions and 13 deletions

View file

@ -89,10 +89,6 @@ class DefaultConfig(object):
# Stripe config
BILLING_TYPE = 'FakeStripe'
# Userfiles
USERFILES_TYPE = 'LocalUserfiles'
USERFILES_PATH = 'test/data/registry/userfiles'
# Analytics
ANALYTICS_TYPE = 'FakeAnalytics'
@ -172,3 +168,7 @@ class DefaultConfig(object):
}
DISTRIBUTED_STORAGE_PREFERENCE = ['local_us']
# Userfiles
USERFILES_LOCATION = 'local_us'
USERFILES_PATH = 'userfiles/'

View file

@ -5,6 +5,8 @@ import magic
from uuid import uuid4
from flask import url_for, request, send_file, make_response, abort
from flask.views import View
from io import BufferedReader
logger = logging.getLogger(__name__)
@ -22,7 +24,9 @@ class UserfilesHandlers(View):
path = self._files.get_file_id_path(file_id)
try:
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:
abort(404)

View file

@ -7,23 +7,19 @@ import boto.gs.connection
import boto.s3.key
import boto.gs.key
from io import UnsupportedOperation, BufferedIOBase
from storage.basestorage import BaseStorage
logger = logging.getLogger(__name__)
class StreamReadKeyAsFile(object):
class StreamReadKeyAsFile(BufferedIOBase):
def __init__(self, key):
self._key = key
self._finished = False
def __enter__(self):
return self
def __exit__(self, type, value, tb):
self._key.close(fast=True)
def read(self, amt=None):
if self._finished:
return None
@ -33,6 +29,16 @@ class StreamReadKeyAsFile(object):
self._finished = True
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):
def __init__(self, connection_class, key_class, connect_kwargs, upload_params, storage_path,

View file

@ -1,5 +1,7 @@
import os
import shutil
import hashlib
import io
from storage.basestorage import BaseStorage
@ -39,7 +41,7 @@ class LocalStorage(BaseStorage):
def stream_read_file(self, 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):
# Size is mandatory