@@ -469,13 +469,83 @@ def upload_gain_reference(
469469 return {"success" : True }
470470
471471
472- class UpstreamTiffInfo (BaseModel ):
472+ class UpstreamFileDownloadInfo (BaseModel ):
473+ download_dir : Path
474+ upstream_instrument : str
475+ upstream_visit_path : Path
476+
477+
478+ @router .post ("/visits/{visit_name}/sessions/{session_id}/upstream_file_data_request" )
479+ def gather_upstream_files (
480+ visit_name : str ,
481+ session_id : MurfeySessionID ,
482+ upstream_file_download : UpstreamFileDownloadInfo ,
483+ ):
484+ """
485+ Instrument server endpoint that will query the backend for files in the chosen
486+ visit directory
487+ """
488+ # Check for forbidden characters
489+ if any (c in visit_name for c in ("/" , "\\ " , ":" , ";" )):
490+ logger .error (f"Forbidden characters are present in the visit name { visit_name } " )
491+ return {
492+ "succss" : False ,
493+ "detail" : "Forbidden characters present in visit name" ,
494+ }
495+ # Get the list of files to download
496+ murfey_url = urlparse (_get_murfey_url (), allow_fragments = False )
497+ sanitised_visit_name = sanitise_nonpath (visit_name )
498+ url_path = url_path_for (
499+ "session_control.correlative_router" ,
500+ "gather_upstream_files" ,
501+ session_id = session_id ,
502+ visit_name = sanitised_visit_name ,
503+ )
504+ upstream_files : list [str ] = requests .get (
505+ f"{ murfey_url .geturl ()} { url_path } " ,
506+ headers = {"Authorization" : f"Bearer { tokens [session_id ]} " },
507+ json = {
508+ "upstream_instrument" : upstream_file_download .upstream_instrument ,
509+ "upstream_visit_path" : str (upstream_file_download .upstream_visit_path ),
510+ },
511+ ).json ()
512+
513+ # Make the download directory and download gathered files
514+ upstream_file_download .download_dir .mkdir (exist_ok = True )
515+ for upstream_file in upstream_files :
516+ url_path = url_path_for (
517+ "session_control.correlative_router" ,
518+ "get_upstream_file" ,
519+ session_id = session_id ,
520+ visit_name = sanitised_visit_name ,
521+ upstream_file_path = upstream_file ,
522+ )
523+ file_data = requests .get (
524+ f"{ murfey_url .geturl ()} { url_path } " ,
525+ headers = {"Authorization" : f"Bearer { tokens [session_id ]} " },
526+ stream = True ,
527+ )
528+ upstream_file_relative_path = Path (upstream_file ).relative_to (
529+ upstream_file_download .upstream_visit_path
530+ )
531+ save_file = upstream_file_download .download_dir / upstream_file_relative_path
532+ save_file .parent .mkdir (parents = True , exist_ok = True )
533+ with open (save_file , "wb" ) as f :
534+ for chunk in file_data .iter_content (chunk_size = 32 * 1024 ** 2 ):
535+ f .write (chunk )
536+ logger .info (f"Saved file to { str (save_file )!r} " )
537+ return {"success" : True }
538+
539+
540+ class UpstreamTiffDownloadInfo (BaseModel ):
473541 download_dir : Path
474542
475543
476544@router .post ("/visits/{visit_name}/sessions/{session_id}/upstream_tiff_data_request" )
477545def gather_upstream_tiffs (
478- visit_name : str , session_id : MurfeySessionID , upstream_tiff_info : UpstreamTiffInfo
546+ visit_name : str ,
547+ session_id : MurfeySessionID ,
548+ upstream_tiff_info : UpstreamTiffDownloadInfo ,
479549):
480550 sanitised_visit_name = sanitise_nonpath (visit_name )
481551 assert not any (c in visit_name for c in ("/" , "\\ " , ":" , ";" ))
0 commit comments