Skip to content

Commit

Permalink
added upload directory
Browse files Browse the repository at this point in the history
  • Loading branch information
rlam3 committed Sep 22, 2015
1 parent 17b2d58 commit 81d0660
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
7 changes: 4 additions & 3 deletions depot/fields/interfaces.py
Expand Up @@ -31,7 +31,7 @@ class DepotFileInfo(with_metaclass(ABCMeta, dict)):
systems.
"""
def __init__(self, content, depot_name=None):
def __init__(self, content, depot_name=None, upload_directory=None):
super(DepotFileInfo, self).__init__()
self._thaw()

Expand All @@ -49,12 +49,13 @@ def __init__(self, content, depot_name=None):

self['depot_name'] = depot_name
self['files'] = []
self['upload_directory'] = upload_directory
self.process_content(content)

self._freeze()

@abstractmethod
def process_content(self, content, filename=None, content_type=None): # pragma: no cover
def process_content(self, content, filename=None, content_type=None, upload_directory=None): # pragma: no cover
"""Process content in the given depot.
This is implemented by subclasses to provide some kind of behaviour on the
Expand Down Expand Up @@ -106,4 +107,4 @@ def _freeze(self):
object.__setattr__(self, '_frozen', True)

def _thaw(self):
object.__setattr__(self, '_frozen', False)
object.__setattr__(self, '_frozen', False)
8 changes: 4 additions & 4 deletions depot/fields/sqlalchemy.py
Expand Up @@ -33,11 +33,12 @@ class UploadedFileField(types.TypeDecorator):
"""
impl = types.Unicode

def __init__(self, filters=tuple(), upload_type=UploadedFile, upload_storage=None, *args, **kw):
def __init__(self, filters=tuple(), upload_type=UploadedFile, upload_storage=None, upload_directory=None, *args, **kw):
super(UploadedFileField, self).__init__(*args, **kw)
self._filters = filters
self._upload_type = upload_type
self._upload_storage = upload_storage
self._upload_directory = upload_directory

def load_dialect_impl(self, dialect):
return dialect.type_descriptor(types.VARCHAR(4096))
Expand Down Expand Up @@ -69,9 +70,8 @@ def _field_set(cls, target, value, oldvalue, initiator):
set_property = inspect(target).mapper.get_property(initiator.key)
column_type = set_property.columns[0].type
assert(isinstance(column_type, UploadedFileField))

upload_type = column_type._upload_type
value = upload_type(value, column_type._upload_storage)
value = upload_type(value, column_type._upload_storage, column_type._upload_directory)
value._apply_filters(column_type._filters)

return value
Expand Down Expand Up @@ -157,4 +157,4 @@ def setup(cls):
from ..validators import TW2FileIntentValidator
SAValidatorSelector.default_validators.setdefault(UploadedFileField, TW2FileIntentValidator)
except ImportError: # pragma: no cover
pass
pass
12 changes: 10 additions & 2 deletions depot/fields/upload.py
Expand Up @@ -33,19 +33,27 @@ def process_content(self, content, filename=None, content_type=None):
Subclasses will need to call this method to ensure the standard
set of attributes is provided.
"""
print "Process content"

file_path, file_id = self.store_content(content, filename, content_type)
self['file_id'] = file_id
self['path'] = file_path

# import pdb; pdb.set_trace()
saved_file = self.file
self['filename'] = saved_file.filename
self['content_type'] = saved_file.content_type
self['uploaded_at'] = saved_file.last_modified.strftime('%Y-%m-%d %H:%M:%S')
self['_public_url'] = saved_file.public_url
# self['upload_directory'] = saved_file.

def store_content(self, content, filename=None, content_type=None):
file_id = self.depot.create(content, filename, content_type)
print "Store content"
# print upload_directory
# Add folder name
# import pdb; pdb.set_trace()
upload_directory = self.upload_directory
file_id = self.depot.create(content, filename, content_type, upload_directory)
file_path = '%s/%s' % (self.depot_name, file_id)
self.files.append(file_path)
return file_path, file_id
Expand All @@ -70,4 +78,4 @@ def depot(self):

@property
def file(self):
return self.depot.get(self.file_id)
return self.depot.get(self.file_id)
14 changes: 9 additions & 5 deletions depot/io/awss3.py
Expand Up @@ -122,14 +122,16 @@ def __save_file(self, key, content, filename, content_type=None):
key.set_contents_from_string(content, policy=self._policy,
encrypt_key=self._encrypt_key)

def create(self, content, filename=None, content_type=None):
def create(self, content, filename=None, content_type=None, upload_directory=None):
content, filename, content_type = self.fileinfo(content, filename, content_type)
new_file_id = str(uuid.uuid1())
key = self._bucket.new_key(new_file_id)

new_file_key = "/".join([upload_directory,new_file_id])
key = self._bucket.new_key(new_file_key)
self.__save_file(key, content, filename, content_type)
return new_file_id
return new_file_key

def replace(self, file_or_id, content, filename=None, content_type=None):
def replace(self, file_or_id, content, filename=None, content_type=None, upload_directory=None):
fileid = self.fileid(file_or_id)
_check_file_id(fileid)

Expand Down Expand Up @@ -159,9 +161,11 @@ def exists(self, file_or_id):
return k is not None


def _check_file_id(file_id):
def _check_file_id(unstriped_file_id):
# Check that the given file id is valid, this also
# prevents unsafe paths.
# import pdb; pdb.set_trace()
file_id = unstriped_file_id.split('/')[-1]
try:
uuid.UUID('{%s}' % file_id)
except:
Expand Down

0 comments on commit 81d0660

Please sign in to comment.