Skip to content

Commit

Permalink
Merge pull request #1103 from scitran/download-encoding
Browse files Browse the repository at this point in the history
Require all path characters to be ascii
  • Loading branch information
nagem authored Mar 20, 2018
2 parents 86da184 + b6cbab6 commit 67583c1
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions api/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def _append_targets(self, targets, cont_name, container, prefix, total_size, tot
filepath = os.path.join(data_path, util.path_from_hash(f['hash']))
if os.path.exists(filepath): # silently skip missing files
if cont_name == 'analyses':
targets.append((filepath, '/'.join([prefix, file_group, f['name']]), cont_name, str(container.get('_id')), f['size']))
targets.append((filepath, '{}/{}/{}'.format(prefix, file_group, f['name']), cont_name, str(container.get('_id')), f['size']))
else:
targets.append((filepath, prefix + '/' + f['name'], cont_name, str(container.get('_id')), f['size']))
targets.append((filepath, '{}/{}'.format(prefix, f['name']), cont_name, str(container.get('_id')), f['size']))
total_size += f['size']
total_cnt += 1
else:
Expand Down Expand Up @@ -245,15 +245,14 @@ def _find_new_path(path, ids_of_paths, _id):
# If the id is already associated with a path, use that instead of modifying it
return ids_of_paths[_id]
used_paths = [ids_of_paths[id_] for id_ in ids_of_paths if id_ != _id]
path = str(path)
i = 0
modified_path = path
while modified_path in used_paths:
modified_path = path + '_' + str(i)
i += 1
return modified_path

path = None
path = ''
if not path and container.get('label'):
path = container['label']
if not path and container.get('timestamp'):
Expand All @@ -266,6 +265,9 @@ def _find_new_path(path, ids_of_paths, _id):
path = container['uid']
if not path and container.get('code'):
path = container['code']

path = path.encode('ascii', errors='ignore')

if not path:
path = 'untitled'

Expand Down Expand Up @@ -318,7 +320,7 @@ def download(self):
else:
self.response.app_iter = self.archivestream(ticket)
self.response.headers['Content-Type'] = 'application/octet-stream'
self.response.headers['Content-Disposition'] = 'attachment; filename=' + str(ticket['filename'])
self.response.headers['Content-Disposition'] = 'attachment; filename=' + ticket['filename'].encode('ascii', errors='ignore')
else:

req_spec = self.request.json_body
Expand Down

0 comments on commit 67583c1

Please sign in to comment.