Skip to content

Commit

Permalink
[#1273] Make sure new resource upload works, also fixes #994
Browse files Browse the repository at this point in the history
  • Loading branch information
kindly committed Nov 12, 2013
1 parent 83a0519 commit 4913975
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
20 changes: 15 additions & 5 deletions ckan/lib/uploader.py
Expand Up @@ -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')
Expand Down Expand Up @@ -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)

Expand Down
11 changes: 8 additions & 3 deletions ckan/logic/action/create.py
Expand Up @@ -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


Expand Down

0 comments on commit 4913975

Please sign in to comment.