Skip to content

Commit

Permalink
added unzip option to the upload_folder_return_details method
Browse files Browse the repository at this point in the history
  • Loading branch information
bcrickboom committed Jun 28, 2024
1 parent cfa3eb0 commit 2bfd697
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
30 changes: 22 additions & 8 deletions orthanc_api_client/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import logging
import typing
import datetime
import zipfile
import tempfile
from typing import List, Optional, Dict
from urllib.parse import urlunsplit, urlencode

Expand Down Expand Up @@ -192,13 +194,15 @@ def upload_folder(self,

return instances_ids

def upload_folder_return_details(self, folder_path: str) -> (typing.Set, typing.Set, typing.List):
def upload_folder_return_details(self, folder_path: str, unzip_before_upload: bool = False) -> (typing.Set, typing.Set, typing.List):
'''
Uploads all the files contained in the folder, including the ones in the sub-folders.
Returns some details
Parameters
----------
folder_path: the folder to upload
unzip_before_upload: if True, a zip file will be unzipped and all resulting files will be uploaded
(if False, the zip file will be uploaded as it is)
Returns
-------
Expand All @@ -213,13 +217,23 @@ def upload_folder_return_details(self, folder_path: str) -> (typing.Set, typing.
for path in os.listdir(folder_path):
full_path = os.path.join(folder_path, path)
if os.path.isfile(full_path):
try:
instance_orthanc_ids = self.upload_file(full_path, ignore_errors=False)
for id in instance_orthanc_ids:
dicom_ids_set.add(self.instances.get_tags(id)["StudyInstanceUID"])
orthanc_ids_set.add(self.instances.get_parent_study_id(id))
except Exception as e:
rejected_files_list.append([str(full_path), e.args[0]])

if unzip_before_upload and zipfile.is_zipfile(full_path):
with tempfile.TemporaryDirectory() as tempDir:
with zipfile.ZipFile(full_path, 'r') as z:
z.extractall(tempDir)
zip_dicom_ids_set, zip_orthanc_ids_set, zip_rejected_files_list = self.upload_folder_return_details(folder_path=tempDir)
dicom_ids_set.update(zip_dicom_ids_set)
orthanc_ids_set.update(zip_orthanc_ids_set)
rejected_files_list.extend(zip_rejected_files_list)
else:
try:
instance_orthanc_ids = self.upload_file(full_path, ignore_errors=False)
for id in instance_orthanc_ids:
dicom_ids_set.add(self.instances.get_tags(id)["StudyInstanceUID"])
orthanc_ids_set.add(self.instances.get_parent_study_id(id))
except Exception as e:
rejected_files_list.append([str(full_path), e.args[0]])
elif os.path.isdir(full_path):
sub_dicom_ids_set, sub_orthanc_ids_set, sub_rejected_files_list = self.upload_folder_return_details(full_path)
dicom_ids_set.update(sub_dicom_ids_set)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ def test_upload_folder_return_details(self):
self.assertEqual(0, len(self.oa.studies.get_all_ids()))
self.assertEqual(2, len(rejected_files_list))

def test_upload_folder_return_details_unzip_before_upload(self):
self.oa.delete_all_content()
dicom_ids_set, orthanc_ids_set, rejected_files_list = self.oa.upload_folder_return_details(here / "stimuli", unzip_before_upload=True)

self.assertLessEqual(4, len(dicom_ids_set))
self.oa.studies.delete(orthanc_ids=orthanc_ids_set)
self.assertEqual(0, len(self.oa.studies.get_all_ids()))
self.assertEqual(2, len(rejected_files_list))

def test_upload_file_dicom_web(self):
self.oa.delete_all_content()

Expand Down

0 comments on commit 2bfd697

Please sign in to comment.