From 695b74f7dd398cb09fac552932b25ebfae12fac9 Mon Sep 17 00:00:00 2001
From: Markus Scheidgen <markus.scheidgen@gmail.com>
Date: Fri, 14 Dec 2018 16:32:25 +0100
Subject: [PATCH] Added fix to deal with data/ prefixed mainfile uri based raw
 file paths.

---
 nomad/api/raw.py  | 11 +++++++++--
 tests/test_api.py | 12 ++++++------
 2 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/nomad/api/raw.py b/nomad/api/raw.py
index ce72a4ab44..3f24495939 100644
--- a/nomad/api/raw.py
+++ b/nomad/api/raw.py
@@ -32,6 +32,12 @@ from nomad.utils import get_logger
 from .app import app, base_path
 
 
+def fix_file_paths(path):
+    """ Removed the leading data from file paths that where given in mainfile uris. """
+    # TODO, mainfile URI's should change or this implementation should change
+    return path[5:]
+
+
 @app.route('%s/raw/<string:upload_hash>/<path:upload_filepath>' % base_path, methods=['GET'])
 def get_raw_file(upload_hash, upload_filepath):
     """
@@ -55,6 +61,7 @@ def get_raw_file(upload_hash, upload_filepath):
     :status 404: upload with given hash does not exist or the given file does not exist
     :returns: the gzipped raw data in the body or a zip file when wildcard was used
     """
+    upload_filepath = fix_file_paths(upload_filepath)
 
     repository_file = RepositoryFile(upload_hash)
     if not repository_file.exists():
@@ -123,7 +130,7 @@ def get_raw_files(upload_hash):
 
     if files_str is None:
         abort(400, message="No files argument given.")
-    files = [file.strip() for file in files_str.split(',')]
+    files = [fix_file_paths(file.strip()) for file in files_str.split(',')]
 
     return respond_to_get_raw_files(upload_hash, files, compress)
 
@@ -164,7 +171,7 @@ def get_raw_files_post(upload_hash):
     compress = json_data.get('compress', False)
     if not isinstance(compress, bool):
         abort(400, message='Compress value %s is not a bool.' % str(compress))
-    files = [file.strip() for file in json_data['files']]
+    files = [fix_file_paths(file.strip()) for file in json_data['files']]
 
     return respond_to_get_raw_files(upload_hash, files, compress)
 
diff --git a/tests/test_api.py b/tests/test_api.py
index f1b5b30467..d86004aa8a 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -407,7 +407,7 @@ class TestRaw:
         return upload_hash
 
     def test_raw_file(self, client, example_upload_hash):
-        url = '/raw/%s/%s' % (example_upload_hash, example_file_mainfile)
+        url = '/raw/%s/data/%s' % (example_upload_hash, example_file_mainfile)
         rv = client.get(url)
         assert rv.status_code == 200
         assert len(rv.data) > 0
@@ -420,7 +420,7 @@ class TestRaw:
         assert 'files' not in data
 
     def test_raw_file_listing(self, client, example_upload_hash):
-        url = '/raw/%s/examples' % example_upload_hash
+        url = '/raw/%s/data/examples' % example_upload_hash
         rv = client.get(url)
         assert rv.status_code == 404
         data = json.loads(rv.data)
@@ -428,7 +428,7 @@ class TestRaw:
 
     @pytest.mark.parametrize('compress', [True, False])
     def test_raw_file_wildcard(self, client, example_upload_hash, compress):
-        url = '/raw/%s/examples*' % example_upload_hash
+        url = '/raw/%s/data/examples*' % example_upload_hash
         if compress:
             url = '%s?compress=1' % url
         rv = client.get(url)
@@ -452,7 +452,7 @@ class TestRaw:
     @pytest.mark.parametrize('compress', [True, False])
     def test_raw_files(self, client, example_upload_hash, compress):
         url = '/raw/%s?files=%s' % (
-            example_upload_hash, ','.join(example_file_contents))
+            example_upload_hash, ','.join(['data/%s' % file for file in example_file_contents]))
         if compress:
             url = '%s&compress=1' % url
         rv = client.get(url)
@@ -466,7 +466,7 @@ class TestRaw:
     @pytest.mark.parametrize('compress', [True, False, None])
     def test_raw_files_post(self, client, example_upload_hash, compress):
         url = '/raw/%s' % example_upload_hash
-        data = dict(files=example_file_contents)
+        data = dict(files=['data/%s' % file for file in example_file_contents])
         if compress is not None:
             data.update(compress=compress)
         rv = client.post(url, data=json.dumps(data), content_type='application/json')
@@ -479,7 +479,7 @@ class TestRaw:
 
     @pytest.mark.parametrize('compress', [True, False])
     def test_raw_files_missing_file(self, client, example_upload_hash, compress):
-        url = '/raw/%s?files=%s,missing/file.txt' % (example_upload_hash, example_file_mainfile)
+        url = '/raw/%s?files=data/%s,missing/file.txt' % (example_upload_hash, example_file_mainfile)
         if compress:
             url = '%s&compress=1' % url
         rv = client.get(url)
-- 
GitLab