Commit d3b96e98 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Added support for .xz file preview.

parent a4661185
......@@ -25,6 +25,7 @@ import magic
import fnmatch
import json
import gzip
import lzma
import urllib.parse
from nomad import search, utils, config
......@@ -140,10 +141,15 @@ def get_raw_file_from_upload_path(
raw_file = upload_files.raw_file(upload_filepath, 'br')
if request.args.get('decompress') is not None and upload_filepath.endswith('.gz'):
filename = upload_filepath[:3]
upload_filepath = filename
raw_file = gzip.GzipFile(filename=filename, mode='rb', fileobj=raw_file)
if request.args.get('decompress') is not None:
if upload_filepath.endswith('.gz'):
filename = upload_filepath[:3]
upload_filepath = filename
raw_file = gzip.GzipFile(filename=filename, mode='rb', fileobj=raw_file)
if upload_filepath.endswith('.xz'):
filename = upload_filepath[:3]
upload_filepath = filename
raw_file = lzma.open(filename=raw_file, mode='rb')
buffer = raw_file.read(2048)
raw_file.seek(0)
......
......@@ -479,7 +479,8 @@ class TestUploads:
@pytest.mark.parametrize('upload_file, ending', [
('examples_potcar.zip', ''),
('examples_potcar_gz.tgz', '.gz')])
('examples_potcar_gz.tgz', '.gz'),
('examples_potcar_xz.tgz', '.xz')])
def test_potcar(self, api, proc_infra, test_user_auth, upload_file, ending):
# only the owner, shared with people are supposed to download the original potcar file
example_file = 'tests/data/proc/%s' % upload_file
......@@ -1388,9 +1389,13 @@ class TestRaw(UploadFilesBasedTests):
upload = files.ArchiveBasedStagingUploadFiles(
'upload_id', upload_path='tests/data/api/example_with_compressed.zip', create=True)
upload.extract()
rv = api.get('raw/upload_id/example_with_compressed/mainfile.gz?decompress=true&offset=5&length=3', headers=admin_user_auth)
assert rv.status_code == 200
assert rv.data == b'con'
for compression in ['gz', 'xz']:
rv = api.get(
'raw/upload_id/example_with_compressed/mainfile.%s?'
'decompress=true&offset=5&length=3' % compression,
headers=admin_user_auth)
assert rv.status_code == 200
assert rv.data == b'con'
@UploadFilesBasedTests.ignore_authorization
def test_raw_file_signed(self, api, upload, _, test_user_signature_token):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment