Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class DICOM2NiFTIConverter:
"""

@staticmethod
def convert(dcm_files, nifti_file=None, converted_files_location="/tmp/upload"):
def convert(dcm_files, nifti_file=None, converted_files_location=None):

"""
Convert DICOM files to NIfTI format.
Expand All @@ -22,6 +22,8 @@ def convert(dcm_files, nifti_file=None, converted_files_location="/tmp/upload"):
nifti_file_assigned = nifti_file
processed_series = set()
series_repetitions = {}
if converted_files_location is None:
converted_files_location = os.path.join(tempfile.gettempdir(), "upload")

with tempfile.TemporaryDirectory() as temp_dir:
for dcm_file in dcm_files:
Expand Down
16 changes: 15 additions & 1 deletion package/src/pyaslreport/core/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import tempfile

from pyaslreport.io.readers.yaml_reader import YamlReader

Expand Down Expand Up @@ -51,13 +52,26 @@ def load(self) -> dict:
"""
allowed_file_types = YamlReader.read(self.allowed_file_types_path)
schemas = self._load_schemas()
paths = self._normalize_temp_paths(allowed_file_types['paths'])

return {
'allowed_file_type': allowed_file_types['allowed_file_types'],
'paths': allowed_file_types['paths'],
'paths': paths,
'schemas': schemas
}

def _normalize_temp_paths(self, paths: dict) -> dict:
normalized_paths = {}
temp_dir = tempfile.gettempdir()

for key, path in paths.items():
if isinstance(path, str) and path.startswith("/tmp/"):
normalized_paths[key] = os.path.join(temp_dir, path.removeprefix("/tmp/"))
else:
normalized_paths[key] = path

return normalized_paths


# Global config instance
CONFIG_DIR = os.path.join(os.path.dirname(__file__), 'config')
Expand Down
25 changes: 25 additions & 0 deletions package/src/pyaslreport/tests/test_temp_path_normalization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
import tempfile

from pyaslreport.core.config import Config
from pyaslreport.converters.dicom_to_nifti_converter import DICOM2NiFTIConverter


def test_config_normalizes_tmp_paths_to_platform_temp_dir():
config_loader = Config("/tmp/does-not-matter")
paths = {
"upload_folder": "/tmp/upload",
"basic_report": "/tmp/basic_report.txt",
"json_report": "backend/tests/test_data/expected_response.json",
}

normalized = config_loader._normalize_temp_paths(paths)

assert normalized["upload_folder"] == os.path.join(tempfile.gettempdir(), "upload")
assert normalized["basic_report"] == os.path.join(tempfile.gettempdir(), "basic_report.txt")
assert normalized["json_report"] == "backend/tests/test_data/expected_response.json"


def test_converter_uses_platform_temp_dir_default():
converted_files_location = DICOM2NiFTIConverter.convert.__defaults__[0]
assert converted_files_location is None