Skip to content
Merged
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
44 changes: 44 additions & 0 deletions src/murfey/server/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,31 @@
raise ValueError(
"No machine configuration set when suggesting destination path"
)

# Construct the full path to where the dataset is to be saved
check_path = machine_config.rsync_basepath / base_path

# Check previous year to account for the year rolling over during data collection
if not check_path.exists():
base_path_parts = base_path.split("/")

Check warning on line 1294 in src/murfey/server/api/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/__init__.py#L1294

Added line #L1294 was not covered by tests
for part in base_path_parts:
# Find the path part corresponding to the year
if len(part) == 4 and part.isdigit():
year_idx = base_path_parts.index(part)
base_path_parts[year_idx] = str(int(part) - 1)
base_path = "/".join(base_path_parts)
check_path_prev = check_path
check_path = machine_config.rsync_basepath / base_path

Check warning on line 1302 in src/murfey/server/api/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/__init__.py#L1298-L1302

Added lines #L1298 - L1302 were not covered by tests

# If it's not in the previous year either, it's a genuine error
if not check_path.exists():
log_message = (

Check warning on line 1306 in src/murfey/server/api/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/__init__.py#L1306

Added line #L1306 was not covered by tests
"Unable to find current visit folder under "
f"{str(check_path_prev)!r} or {str(check_path)!r}"
)
log.error(log_message)
raise FileNotFoundError(log_message)

Check warning on line 1311 in src/murfey/server/api/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/__init__.py#L1310-L1311

Added lines #L1310 - L1311 were not covered by tests

check_path_name = check_path.name
while check_path.exists():
count = count + 1 if count else 2
Expand Down Expand Up @@ -1478,6 +1502,26 @@
/ secure_filename(visit_name)
/ machine_config.gain_directory_name
)

# Check under previous year if the folder doesn't exist
if not filepath.exists():
filepath_prev = filepath
filepath = (

Check warning on line 1509 in src/murfey/server/api/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/__init__.py#L1508-L1509

Added lines #L1508 - L1509 were not covered by tests
Path(machine_config.rsync_basepath)
/ (machine_config.rsync_module or "data")
/ str(datetime.datetime.now().year - 1)
/ secure_filename(visit_name)
/ machine_config.gain_directory_name
)
# If it's not in the previous year, it's a genuine error
if not filepath.exists():
log_message = (

Check warning on line 1518 in src/murfey/server/api/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/__init__.py#L1518

Added line #L1518 was not covered by tests
"Unable to find gain reference directory under "
f"{str(filepath_prev)!r} or {str(filepath)}"
)
log.error(log_message)
raise FileNotFoundError(log_message)

Check warning on line 1523 in src/murfey/server/api/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/__init__.py#L1522-L1523

Added lines #L1522 - L1523 were not covered by tests

if gain_reference_params.eer:
new_gain_ref, new_gain_ref_superres = await prepare_eer_gain(
filepath / safe_path_name,
Expand Down
42 changes: 38 additions & 4 deletions src/murfey/server/demo_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@

settings = Settings()

machine_config: dict = {}
machine_config: dict[str, MachineConfig] = {}

Check warning on line 113 in src/murfey/server/demo_api.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/demo_api.py#L113

Added line #L113 was not covered by tests
if settings.murfey_machine_configuration:
microscope = get_microscope()
machine_config = from_file(Path(settings.murfey_machine_configuration), microscope)
Expand Down Expand Up @@ -1290,12 +1290,46 @@
instrument_name = (
db.exec(select(Session).where(Session.id == session_id)).one().instrument_name
)
check_path = (
machine_config[instrument_name].rsync_basepath / params.base_path
rsync_basepath = (

Check warning on line 1293 in src/murfey/server/demo_api.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/demo_api.py#L1293

Added line #L1293 was not covered by tests
machine_config[instrument_name].rsync_basepath
if machine_config
else Path(f"/dls/{get_microscope()}") / params.base_path
else Path(f"/dls/{get_microscope()}")
)
check_path = rsync_basepath / params.base_path

Check warning on line 1298 in src/murfey/server/demo_api.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/demo_api.py#L1298

Added line #L1298 was not covered by tests
check_path = check_path.parent / f"{check_path.stem}{count}{check_path.suffix}"
check_path = check_path.resolve()

Check warning on line 1300 in src/murfey/server/demo_api.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/demo_api.py#L1300

Added line #L1300 was not covered by tests

# Check for path traversal attempt
if not str(check_path).startswith(str(rsync_basepath)):
raise Exception(f"Path traversal attempt detected: {str(check_path)!r}")

Check warning on line 1304 in src/murfey/server/demo_api.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/demo_api.py#L1304

Added line #L1304 was not covered by tests

# Check previous year to account for the year rolling over during data collection
if not sanitise_path(check_path).exists():
base_path_parts = list(params.base_path.parts)

Check warning on line 1308 in src/murfey/server/demo_api.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/demo_api.py#L1308

Added line #L1308 was not covered by tests
for part in base_path_parts:
# Find the path part corresponding to the year
if len(part) == 4 and part.isdigit():
year_idx = base_path_parts.index(part)
base_path_parts[year_idx] = str(int(part) - 1)
base_path = "/".join(base_path_parts)
check_path_prev = check_path
check_path = rsync_basepath / base_path
check_path = check_path.parent / f"{check_path.stem}{count}{check_path.suffix}"
check_path = check_path.resolve()

Check warning on line 1318 in src/murfey/server/demo_api.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/demo_api.py#L1312-L1318

Added lines #L1312 - L1318 were not covered by tests

# Check for path traversal attempt
if not str(check_path).startswith(str(rsync_basepath)):
raise Exception(f"Path traversal attempt detected: {str(check_path)!r}")

Check warning on line 1322 in src/murfey/server/demo_api.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/demo_api.py#L1322

Added line #L1322 was not covered by tests

# If visit is not in the previous year either, it's a genuine error
if not check_path.exists():
log_message = sanitise(

Check warning on line 1326 in src/murfey/server/demo_api.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/demo_api.py#L1326

Added line #L1326 was not covered by tests
"Unable to find current visit folder under "
f"{str(check_path_prev)!r} or {str(check_path)!r}"
)
log.error(log_message)
raise FileNotFoundError(log_message)

Check warning on line 1331 in src/murfey/server/demo_api.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/demo_api.py#L1330-L1331

Added lines #L1330 - L1331 were not covered by tests

check_path_name = check_path.name
while sanitise_path(check_path).exists():
count = count + 1 if count else 2
Expand Down
Loading