From 0ceb33a8dbb2991e45aefc1d73c6a74578b31356 Mon Sep 17 00:00:00 2001 From: Megan Henning Date: Tue, 19 Sep 2017 11:47:10 -0500 Subject: [PATCH 1/3] Add mimetype field to packfile obj save --- api/placer.py | 1 + 1 file changed, 1 insertion(+) 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'], From d73d430b0d8a434b60f3e3c52a3054d9b050df2e Mon Sep 17 00:00:00 2001 From: Megan Henning Date: Tue, 19 Sep 2017 12:26:20 -0500 Subject: [PATCH 2/3] Add db update and tests --- bin/database.py | 24 ++++++++++++++++++- test/integration_tests/python/test_uploads.py | 6 +++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/bin/database.py b/bin/database.py index 557032e89..0d9bce131 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.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 From fd7d378041a3452cb9202eabcedf87157a445d6f Mon Sep 17 00:00:00 2001 From: Megan Henning Date: Tue, 19 Sep 2017 13:22:48 -0500 Subject: [PATCH 3/3] Only return acqs with files --- bin/database.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/database.py b/bin/database.py index 0d9bce131..e01e6aad6 100755 --- a/bin/database.py +++ b/bin/database.py @@ -1208,7 +1208,7 @@ def upgrade_to_36(): """ scitran/core issue #931 - mimetype not set on packfile uploads """ - cursor = config.db.acquisitions.find({'files.mimetype': None}) + cursor = config.db.acquisitions.find({'files': { '$gt': [] }, 'files.mimetype': None}) process_cursor(cursor, upgrade_to_36_closure)