Merge remote branch 'remotes/jwandborg/cloudfiles_storage_object_wrapper'
This commit is contained in:
commit
9674a9d4f6
@ -142,8 +142,8 @@ def process_image(entry):
|
|||||||
thumb = thumb.convert("RGB")
|
thumb = thumb.convert("RGB")
|
||||||
|
|
||||||
thumb_filepath = create_pub_filepath(entry, 'thumbnail.jpg')
|
thumb_filepath = create_pub_filepath(entry, 'thumbnail.jpg')
|
||||||
|
|
||||||
thumb_file = mgg.public_store.get_file(thumb_filepath, 'w')
|
thumb_file = mgg.public_store.get_file(thumb_filepath, 'w')
|
||||||
|
|
||||||
with closing(thumb_file):
|
with closing(thumb_file):
|
||||||
thumb.save(thumb_file, "JPEG", quality=90)
|
thumb.save(thumb_file, "JPEG", quality=90)
|
||||||
|
|
||||||
@ -160,8 +160,8 @@ def process_image(entry):
|
|||||||
medium = medium.convert("RGB")
|
medium = medium.convert("RGB")
|
||||||
|
|
||||||
medium_filepath = create_pub_filepath(entry, 'medium.jpg')
|
medium_filepath = create_pub_filepath(entry, 'medium.jpg')
|
||||||
|
|
||||||
medium_file = mgg.public_store.get_file(medium_filepath, 'w')
|
medium_file = mgg.public_store.get_file(medium_filepath, 'w')
|
||||||
|
|
||||||
with closing(medium_file):
|
with closing(medium_file):
|
||||||
medium.save(medium_file, "JPEG", quality=90)
|
medium.save(medium_file, "JPEG", quality=90)
|
||||||
medium_processed = True
|
medium_processed = True
|
||||||
|
@ -19,6 +19,8 @@ import shutil
|
|||||||
import urlparse
|
import urlparse
|
||||||
import uuid
|
import uuid
|
||||||
import cloudfiles
|
import cloudfiles
|
||||||
|
import mimetypes
|
||||||
|
import tempfile
|
||||||
|
|
||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
|
|
||||||
@ -227,6 +229,27 @@ class BasicFileStorage(StorageInterface):
|
|||||||
|
|
||||||
|
|
||||||
class CloudFilesStorage(StorageInterface):
|
class CloudFilesStorage(StorageInterface):
|
||||||
|
class StorageObjectWrapper():
|
||||||
|
"""
|
||||||
|
Wrapper for python-cloudfiles's cloudfiles.storage_object.Object
|
||||||
|
used to circumvent the mystic `medium.jpg` corruption issue.
|
||||||
|
|
||||||
|
This wrapper currently meets mediagoblin's needs for a public_store
|
||||||
|
file-like object.
|
||||||
|
"""
|
||||||
|
def __init__(self, storage_object):
|
||||||
|
self.storage_object = storage_object
|
||||||
|
|
||||||
|
def read(self, *args, **kwargs):
|
||||||
|
return self.storage_object.read(*args, **kwargs)
|
||||||
|
|
||||||
|
def write(self, data, *args, **kwargs):
|
||||||
|
if self.storage_object.size and type(data) == str:
|
||||||
|
data = self.read() + data
|
||||||
|
|
||||||
|
self.storage_object.write(data, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
self.param_container = kwargs.get('cloudfiles_container')
|
self.param_container = kwargs.get('cloudfiles_container')
|
||||||
self.param_user = kwargs.get('cloudfiles_user')
|
self.param_user = kwargs.get('cloudfiles_user')
|
||||||
@ -268,7 +291,10 @@ class CloudFilesStorage(StorageInterface):
|
|||||||
except cloudfiles.errors.NoSuchObject:
|
except cloudfiles.errors.NoSuchObject:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def get_file(self, filepath, mode='r'):
|
def get_file(self, filepath, *args):
|
||||||
|
"""
|
||||||
|
- Doesn't care about the "mode" argument
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
obj = self.container.get_object(
|
obj = self.container.get_object(
|
||||||
self._resolve_filepath(filepath))
|
self._resolve_filepath(filepath))
|
||||||
@ -276,7 +302,13 @@ class CloudFilesStorage(StorageInterface):
|
|||||||
obj = self.container.create_object(
|
obj = self.container.create_object(
|
||||||
self._resolve_filepath(filepath))
|
self._resolve_filepath(filepath))
|
||||||
|
|
||||||
return obj
|
mimetype = mimetypes.guess_type(
|
||||||
|
filepath[-1])
|
||||||
|
|
||||||
|
if mimetype:
|
||||||
|
obj.content_type = mimetype[0]
|
||||||
|
|
||||||
|
return self.StorageObjectWrapper(obj)
|
||||||
|
|
||||||
def delete_file(self, filepath):
|
def delete_file(self, filepath):
|
||||||
# TODO: Also delete unused directories if empty (safely, with
|
# TODO: Also delete unused directories if empty (safely, with
|
||||||
|
Loading…
x
Reference in New Issue
Block a user