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
11 changes: 8 additions & 3 deletions src/murfey/client/analyser.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,15 @@ def _find_context(self, file_path: Path) -> bool:
)
return True

if "Metadata" in file_path.parts or file_path.name == "EpuSession.dm":
if (
"Metadata" in file_path.parts
or "gridsquares" in file_path.parts
or file_path.name == "EpuSession.dm"
):
if (context := _get_context("SPAMetadataContext")) is None:
return False
self._context = context.load()(
"epu",
"serialem" if self._serialem else "epu",
self._basepath,
self._murfey_config,
self._token,
Expand Down Expand Up @@ -357,12 +361,13 @@ def _analyse(self, transferred_file: Path):
if self._limited:
if (
"Metadata" in transferred_file.parts
or "gridsquares" in transferred_file.parts
or transferred_file.name == "EpuSession.dm"
) and not self._context:
if not (context := _get_context("SPAMetadataContext")):
return None
self._context = context.load()(
"epu",
"serialem" if self._serialem else "epu",
self._basepath,
self._murfey_config,
self._token,
Expand Down
8 changes: 8 additions & 0 deletions src/murfey/client/contexts/spa_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ def post_transfer_serialem(
visitless_source = _get_visitless_source(
source, environment, skip_search=True
)
destination_file = _file_transferred_to(
environment,
source,
transferred_file,
Path(self._machine_config.get("rsync_basepath", "")),
)
capture_post(
base_url=str(environment.url.geturl()),
router_name="session_control.spa_router",
Expand All @@ -148,7 +154,9 @@ def post_transfer_serialem(
gsid=transferred_file.stem,
data={
"tag": visitless_source,
"image_path": str(destination_file),
"count": len(self._registered_squares_serialem) + 1,
"serialem": self._acquisition_software == "serialem",
},
)
self._registered_squares_serialem.add(transferred_file.stem)
Expand Down
34 changes: 25 additions & 9 deletions src/murfey/server/api/session_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path
from typing import Dict, List, Optional

import requests
from fastapi import APIRouter, Depends
from fastapi.responses import FileResponse
from ispyb.sqlalchemy import AutoProcProgram as ISPyBAutoProcProgram
Expand Down Expand Up @@ -417,24 +418,31 @@

class SquareRegistration(BaseModel):
tag: str
image_path: str | None = None
count: int | None = None
serialem: bool = False


@spa_router.post("/sessions/{session_id}/register_square/{gsid}")
def register_square(
session_id: MurfeySessionID,
gsid: int,
gsid: int | str,
square_registration_data: SquareRegistration,
db=murfey_db,
):
if SMARTEM_ACTIVE:
gs = db.exec(
select(GridSquare)
.where(GridSquare.name == gsid)
.where(GridSquare.tag == square_registration_data.tag)
.where(GridSquare.session_id == session_id)
).one_or_none()
if gs and gs.smartem_uuid:
logger.info(f"smartem active for square {sanitise(str(gsid))}")
if square_registration_data.serialem:
smartem_uuid: int | str | None = gsid
else:
gs = db.exec(
select(GridSquare)
.where(GridSquare.name == gsid)
.where(GridSquare.tag == square_registration_data.tag)
.where(GridSquare.session_id == session_id)
).one_or_none()
smartem_uuid = gs.smartem_uuid if gs else None
if smartem_uuid is not None:
session = db.exec(select(Session).where(Session.id == session_id)).one()
machine_config = get_machine_config(session.instrument_name)[
session.instrument_name
Expand All @@ -444,8 +452,16 @@
base_url=machine_config.smartem_api_url, logger=logger
)
smartem_client.gridsquare_registered(
gs.smartem_uuid, count=square_registration_data.count
smartem_uuid, count=square_registration_data.count
)
result = requests.put(
f"{machine_config.smartem_api_url}/gridsquares/{smartem_uuid}",
json={"image_path": square_registration_data.image_path},
)

Check failure

Code scanning / CodeQL

Partial server-side request forgery Critical

Part of the URL of this request depends on a
user-provided value
.
if not result.status_code == 200:
logger.warning(
f"Post to smartem gridsquares returned {result.status_code}"
)
else:
logger.info("smartem deactivated so did not register square")

Expand Down
2 changes: 1 addition & 1 deletion src/murfey/util/route_manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ murfey.server.api.session_control.spa_router:
function: register_square
path_params:
- name: gsid
type: int
type: int | str
- name: session_id
type: int
methods:
Expand Down