diff --git a/ckan/config/middleware.py b/ckan/config/middleware.py index 7699596138d..efde540054a 100644 --- a/ckan/config/middleware.py +++ b/ckan/config/middleware.py @@ -23,6 +23,7 @@ from ckan.plugins import PluginImplementations from ckan.plugins.interfaces import IMiddleware from ckan.lib.i18n import get_locales_from_config +import ckan.lib.uploader as uploader from ckan.config.environment import load_environment import ckan.lib.app_globals as app_globals @@ -148,7 +149,7 @@ def make_app(conf, full_stack=True, static_files=True, **app_conf): cache_max_age=static_max_age) static_parsers = [static_app, app] - storage_directory = config.get('ckan.storage_path') + storage_directory = uploader.get_storage_path() if storage_directory: path = os.path.join(storage_directory, 'storage') try: diff --git a/ckan/lib/uploader.py b/ckan/lib/uploader.py index 880488b8161..867e28cfcb0 100644 --- a/ckan/lib/uploader.py +++ b/ckan/lib/uploader.py @@ -10,6 +10,7 @@ _storage_path = None + def get_storage_path(): '''Function to cache storage path''' global _storage_path @@ -27,21 +28,24 @@ def get_storage_path(): _storage_path = ofs_storage_dir return _storage_path elif ofs_impl: - log.critical('''We only support local file storage form version 2.2 of ckan - please specify ckan.storage_path in your config for your uploads''') + log.critical('''We only support local file storage form version 2.2 + of ckan please specify ckan.storage_path in your + config for your uploads''') _storage_path = False else: - log.critical('''Please specify a ckan.storage_path in your config for your uploads''') + log.critical('''Please specify a ckan.storage_path in your config + for your uploads''') _storage_path = False return _storage_path - class Upload(object): def __init__(self, object_type, old_filename=None): - ''' Setup upload by creating a subdirectory of the storage directory of name - object_type. old_filename is the name of the file in the url field last time''' + ''' Setup upload by creating a subdirectory of the storage directory + of name object_type. old_filename is the name of the file in the url + field last time''' + self.storage_path = None self.filename = None self.filepath = None @@ -60,20 +64,21 @@ def __init__(self, object_type, old_filename=None): self.old_filepath = os.path.join(self.storage_path, old_filename) def update_data_dict(self, data_dict, url_field, file_field, clear_field): - ''' Manipulate data from the data_dict. url_field is the name of the field - where the upload is going to be. file_field is name of the key where the - FieldStorage is kept (i.e the field where the file data actually is). clear_field - is the name of a boolean field which requests the upload to be deleted. This needs - to be called before it reaches any validators''' - - if not self.storage_path: - return + ''' Manipulate data from the data_dict. url_field is the name of the + field where the upload is going to be. file_field is name of the key + where the FieldStorage is kept (i.e the field where the file data + actually is). clear_field is the name of a boolean field which + requests the upload to be deleted. This needs to be called before + it reaches any validators''' self.url = data_dict.get(url_field, '') self.clear = data_dict.pop(clear_field, None) self.file_field = file_field self.upload_field_storage = data_dict.pop(file_field, None) + if not self.storage_path: + return + if isinstance(self.upload_field_storage, cgi.FieldStorage): self.filename = self.upload_field_storage.filename self.filename = str(datetime.datetime.utcnow()) + self.filename @@ -90,9 +95,11 @@ def update_data_dict(self, data_dict, url_field, file_field, clear_field): data_dict[url_field] = '' def upload(self, max_size=2): - ''' Actually upload the file. This should happen just before a commit but after - the data has been validated and flushed to the db. This is so we do not store anything - unless the request is actually good. Max_size is size in MB maximum of the file''' + ''' Actually upload the file. + This should happen just before a commit but after the data has + been validated and flushed to the db. This is so we do not store + anything unless the request is actually good. + max_size is size in MB maximum of the file''' if self.filename: output_file = open(self.tmp_filepath, 'wb')