From 4913975a2863e2a4ab12a42daef737c632fe9284 Mon Sep 17 00:00:00 2001 From: kindly Date: Tue, 12 Nov 2013 16:17:04 +0000 Subject: [PATCH] [#1273] Make sure new resource upload works, also fixes #994 --- ckan/lib/uploader.py | 20 +++++++++++++++----- ckan/logic/action/create.py | 11 ++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/ckan/lib/uploader.py b/ckan/lib/uploader.py index 8991f497993..2a348d7150d 100644 --- a/ckan/lib/uploader.py +++ b/ckan/lib/uploader.py @@ -133,14 +133,16 @@ def upload(self, max_size=2): class ResourceUpload(object): def __init__(self, resource): - path = pylons.config.get('ckan.storage_path') + path = get_storage_path() if not path: return self.storage_path = os.path.join(path, 'resources') try: os.makedirs(self.storage_path) except OSError, e: - pass + ## errno 17 is file already exists + if e.errno != 17: + raise self.filename = None url = resource.get('url') @@ -168,23 +170,31 @@ def get_path(self, id): return filepath - def upload(self, resource): - id = resource['id'] + def upload(self, id, max_size=10): directory = self.get_directory(id) filepath = self.get_path(id) if self.filename: try: os.makedirs(directory) except OSError, e: - pass + ## errno 17 is file already exists + if e.errno != 17: + raise tmp_filepath = filepath + '~' output_file = open(tmp_filepath, 'wb+') self.upload_file.seek(0) + current_size = 0 while True: + current_size = current_size + 1 data = self.upload_file.read(2 ** 20) #mb chuncks if not data: break output_file.write(data) + if current_size > max_size: + os.remove(self.tmp_filepath) + raise logic.ValidationError( + {'upload': ['File upload too large']} + ) output_file.close() os.rename(tmp_filepath, filepath) diff --git a/ckan/logic/action/create.py b/ckan/logic/action/create.py index 97f56b689fb..c419cafc0d5 100644 --- a/ckan/logic/action/create.py +++ b/ckan/logic/action/create.py @@ -268,15 +268,20 @@ def resource_create(context, data_dict): try: context['defer_commit'] = True context['use_cache'] = False - pkg_dict = _get_action('package_update')(context, pkg_dict) + _get_action('package_update')(context, pkg_dict) except ValidationError, e: errors = e.error_dict['resources'][-1] raise ValidationError(errors) - resource = pkg_dict['resources'][-1] - upload.upload(resource) + ## Get out resource_id resource from model as it will not appear in + ## package_show until after commit + upload.upload(context['package'].resources[-1].id) model.repo.commit() + ## Run package show again to get out actual last_resource + pkg_dict = _get_action('package_show')(context, {'id': package_id}) + resource = pkg_dict['resources'][-1] + return resource