diff --git a/api/placer.py b/api/placer.py index 325558e90..72a4be8cf 100644 --- a/api/placer.py +++ b/api/placer.py @@ -507,6 +507,7 @@ def finalize(self): 'modified': cgi_field.modified, 'size': cgi_field.size, 'hash': cgi_field.hash, + 'mimetype': cgi_field.mimetype, 'type': self.metadata['packfile']['type'], diff --git a/bin/database.py b/bin/database.py index 557032e89..e01e6aad6 100755 --- a/bin/database.py +++ b/bin/database.py @@ -14,6 +14,7 @@ import time from api import config +from api import util from api.dao import containerutil from api.dao.containerstorage import ProjectStorage from api.jobs.jobs import Job @@ -21,7 +22,7 @@ from api.types import Origin from api.jobs import batch -CURRENT_DATABASE_VERSION = 35 # An int that is bumped when a new schema change is made +CURRENT_DATABASE_VERSION = 36 # An int that is bumped when a new schema change is made def get_db_version(): @@ -1189,6 +1190,27 @@ def upgrade_to_35(): process_cursor(cursor, upgrade_to_35_closure) +def upgrade_to_36_closure(acquisition): + + for f in acquisition['files']: + if not f.get('mimetype'): + logging.debug('file with name {} did not have mimetype'.format(f['name'])) + f['mimetype'] = util.guess_mimetype(f['name']) + + result = config.db.acquisitions.update_one({'_id': acquisition['_id']}, {'$set': {'files': acquisition['files']}}) + if result.modified_count != 1: + raise Exception('Acquisition file not updated') + + return True + + +def upgrade_to_36(): + """ + scitran/core issue #931 - mimetype not set on packfile uploads + """ + cursor = config.db.acquisitions.find({'files': { '$gt': [] }, 'files.mimetype': None}) + process_cursor(cursor, upgrade_to_36_closure) + ### diff --git a/test/integration_tests/python/test_uploads.py b/test/integration_tests/python/test_uploads.py index 394644165..eaacb866b 100644 --- a/test/integration_tests/python/test_uploads.py +++ b/test/integration_tests/python/test_uploads.py @@ -932,6 +932,12 @@ def test_packfile_upload(data_builder, file_form, as_admin, as_root, api_db): params={'token': token, 'metadata': metadata_json}) assert r.ok + # make sure file was uploaded and mimetype and type are properly set + packfile = as_admin.get('/acquisitions').json()[0]['files'][0] + assert packfile['mimetype'] == 'application/zip' + assert packfile['type'] == 'test' + + # get another token (start packfile-upload) r = as_admin.post('/projects/' + project + '/packfile-start') assert r.ok