Skip to content

Commit c7368f5

Browse files
Joonatan MäkinenRasko Leinonen
andcommitted
Bigpicture requested fixes. (merge commit)
Merge branch 'fix/bigpicture-22-may-rasko' into 'main' * Addressed merge review comments. * Allow image alias to have IMAGE_ prefix when checking image file directory. * Allow image alias to have IMAGE_ prefix when checking image file directory. See merge request https://gitlab.ci.csc.fi/sds-dev/sd-submit/metadata-submitter/-/merge_requests/1159 Approved-by: Joonatan Mäkinen <jmakine@csc.fi> Co-authored-by: Rasko Leinonen <raskolei@csc.fi> Merged by Joonatan Mäkinen <jmakine@csc.fi>
2 parents e004afa + 56f7f24 commit c7368f5

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

metadata_backend/api/services/submission/bigpicture.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,10 @@ def prepare_files(self, submission_id: str) -> list[File]:
510510

511511
# Validate the image file paths and extensions
512512
alias = root.get("alias")
513-
expected_dir = f"IMAGES/IMAGE_{alias}"
514513
for file_elem in xml.xpath("/IMAGE/FILES/FILE"):
515514
filename = file_elem.get("filename")
515+
self.check_image_file_dir(alias, filename)
516516
dir_part, _, file_part = filename.rpartition("/") # "IMAGES/IMAGE_{alias}", "/", "*.dcm.c4gh"
517-
if dir_part != expected_dir:
518-
raise UserException(f"Image file '{filename}' must be in directory '{expected_dir}'.")
519517
if not file_part.removesuffix(".c4gh").endswith(".dcm"):
520518
raise UserException(f"Image file '{filename}' must have a .dcm extension.")
521519

@@ -674,3 +672,18 @@ async def update(
674672
await self._delete_object(datacite_object.objectId)
675673

676674
return submission
675+
676+
@staticmethod
677+
def check_image_file_dir(alias: str, image_file_path: str) -> None:
678+
"""
679+
Check if the image file is in the correct directory.
680+
681+
:param alias: The image metadata object alias.
682+
:param image_file_path: The image file path.
683+
:raises UserException: If the image file is not in the correct directory.
684+
"""
685+
alias = alias.removeprefix("IMAGE_") # Alias may contain IMAGE_ prefix.
686+
expected_dir = f"IMAGES/IMAGE_{alias}"
687+
dir_part, _, _ = image_file_path.rpartition("/")
688+
if dir_part != expected_dir:
689+
raise UserException(f"Image file '{image_file_path}' must be in directory '{expected_dir}'.")

tests/unit/api/services/submission/test_bigpicture.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,23 @@ def test_prepare_files_wrong_extension_raises():
9595

9696
with pytest.raises(UserException, match="Image file 'IMAGES/IMAGE_1/test.tif.c4gh' must have a .dcm extension"):
9797
_prepare_files(objects)
98+
99+
100+
def test_check_image_file_dir_invalid_dir():
101+
alias = "TEST"
102+
path = "IMAGES/INVALID/image"
103+
104+
with pytest.raises(UserException) as exc:
105+
BigpictureObjectSubmissionService.check_image_file_dir(alias, path)
106+
107+
assert f"Image file '{path}' must be in directory 'IMAGES/IMAGE_TEST'" in str(exc.value)
108+
109+
110+
def test_check_image_file_dir_correct_dir():
111+
alias = "TEST"
112+
path = "IMAGES/IMAGE_TEST/image"
113+
BigpictureObjectSubmissionService.check_image_file_dir(alias, path)
114+
115+
alias = "IMAGE_TEST"
116+
path = "IMAGES/IMAGE_TEST/image"
117+
BigpictureObjectSubmissionService.check_image_file_dir(alias, path)

0 commit comments

Comments
 (0)