From 707b626fc83d858f7e1841a87c83abbcb3b2c6ff Mon Sep 17 00:00:00 2001 From: Federico Negri Date: Fri, 1 Dec 2023 15:22:28 +0100 Subject: [PATCH] Fix download of file when evaluation path contains a subdir --- ansys/rep/client/jms/api/project_api.py | 2 +- tests/jms/test_files.py | 28 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/ansys/rep/client/jms/api/project_api.py b/ansys/rep/client/jms/api/project_api.py index 078e93a09..e789a01c6 100644 --- a/ansys/rep/client/jms/api/project_api.py +++ b/ansys/rep/client/jms/api/project_api.py @@ -577,9 +577,9 @@ def _download_file( if getattr(file, "hash", None) is None: log.warning(f"No hash found for file {file.name}.") - Path(target_path).mkdir(parents=True, exist_ok=True) download_link = f"{project_api.fs_bucket_url}/{file.storage_id}" download_path = os.path.join(target_path, file.evaluation_path) + Path(download_path).parent.mkdir(parents=True, exist_ok=True) with project_api.client.session.get(download_link, stream=stream) as r, open( download_path, "wb" diff --git a/tests/jms/test_files.py b/tests/jms/test_files.py index 402eed618..251bb6abd 100644 --- a/tests/jms/test_files.py +++ b/tests/jms/test_files.py @@ -119,6 +119,34 @@ def test_files(self): # Delete project again jms_api.delete_project(proj) + def test_download_file_in_subdir(self): + + client = self.client + jms_api = JmsApi(client) + proj = jms_api.create_project( + Project(name=f"rep_test_download_file_in_subdir", active=False) + ) + project_api = ProjectApi(client, proj.id) + + files = [ + File( + name="file", + evaluation_path="subdir/file.txt", + type="text/plain", + src=io.BytesIO(b"This is my file"), + ) + ] + + file = project_api.create_files(files)[0] + + with tempfile.TemporaryDirectory() as tpath: + fpath = project_api.download_file(file, tpath) + with open(fpath, "r") as sf: + self.assertEqual("This is my file", sf.read()) + + # Delete project again + jms_api.delete_project(proj) + if __name__ == "__main__": unittest.main()