From 06b0c83c39072aba2cf5b0699d260335233b0295 Mon Sep 17 00:00:00 2001 From: Markus Scheidgen <markus.scheidgen@gmail.com> Date: Thu, 20 Sep 2018 14:25:27 +0200 Subject: [PATCH] Fixed content-encoding on archive api with gzipped archives. --- nomad/api.py | 12 +++++++++++- tests/test_api.py | 9 ++++++++- tests/test_files.py | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/nomad/api.py b/nomad/api.py index 22c5e831a4..c70cb989e2 100644 --- a/nomad/api.py +++ b/nomad/api.py @@ -644,7 +644,17 @@ def get_calc(upload_hash, calc_hash): try: archive = ArchiveFile(archive_id) arhchive_path = archive.os_path - return send_file(arhchive_path, attachment_filename=os.path.basename(arhchive_path)) + + rv = send_file( + arhchive_path, + mimetype='application/json', + as_attachment=True, + attachment_filename=os.path.basename(arhchive_path)) + + if config.files.compress_archive: + rv.headers['Content-Encoding'] = 'gzip' + + return rv except KeyError: abort(404, message='Archive %s does not exist.' % archive_id) except FileNotFoundError: diff --git a/tests/test_api.py b/tests/test_api.py index 580e1e5916..7543c0cc8c 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,6 +1,7 @@ import pytest import time import json +import zlib import re from mongoengine import connect from mongoengine.connection import disconnect @@ -153,7 +154,7 @@ def test_processing(client, file, mode, worker, mocksearch, test_user_auth, no_w upload_id = upload['upload_id'] upload_cmd = upload['upload_command'] - headers = dict(Authorization='Basic %s' % re.search(r'.*-HAuthorization: Basic ([^\s]+).*', upload_cmd).group(1)) + headers = dict(Authorization='Basic %s' % re.search(r'.*Authorization: Basic ([^\s]+).*', upload_cmd).group(1)) upload_endpoint = '/uploads/%s' % upload_id upload_file_endpoint = '%s/file' % upload_endpoint @@ -266,6 +267,12 @@ def test_repo_calcs_user_invisible(client, example_elastic_calc, test_other_user def test_get_archive(client, archive, no_warn): rv = client.get('/archive/%s' % archive.object_id) + + if rv.headers.get('Content-Encoding') == 'gzip': + json.loads(zlib.decompress(rv.data, 16 + zlib.MAX_WBITS)) + else: + json.loads(rv.data) + assert rv.status_code == 200 diff --git a/tests/test_files.py b/tests/test_files.py index 69a1371f6b..3abcebd7ae 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -75,7 +75,7 @@ def archive_config(monkeypatch, request): config.files.uploads_bucket, config.files.repository_bucket, config.files.archive_bucket, - request) + request.param) monkeypatch.setattr(config, 'files', new_config) yield -- GitLab