@@ -107,6 +107,28 @@ async def _get_file(
107107 ) from err
108108
109109
110+ async def _create_domain_file (
111+ webserver_api : AuthSession , client_file : ClientFile | ClientFileInProgramJob
112+ ) -> DomainFile :
113+ if isinstance (client_file , ClientFile ):
114+ file = client_file .to_domain_model ()
115+ elif isinstance (client_file , ClientFileInProgramJob ):
116+ project = await webserver_api .get_project (project_id = client_file .job_id )
117+ if len (project .workbench ) > 1 :
118+ raise HTTPException (
119+ status_code = status .HTTP_422_UNPROCESSABLE_ENTITY ,
120+ detail = f"Job_id { project .uuid } is not a valid program job." ,
121+ )
122+ node_id = next (iter (project .workbench .keys ()))
123+ file = client_file .to_domain_model (
124+ project_id = project .uuid , node_id = NodeID (node_id )
125+ )
126+ else :
127+ err_msg = f"Invalid client_file type passed: { type (client_file )= } "
128+ raise TypeError (err_msg )
129+ return file
130+
131+
110132@router .get ("" , response_model = list [OutputFile ], responses = _FILE_STATUS_CODES )
111133async def list_files (
112134 storage_client : Annotated [StorageApi , Depends (get_api_client (StorageApi ))],
@@ -254,22 +276,7 @@ async def get_upload_links(
254276):
255277 """Get upload links for uploading a file to storage"""
256278 assert request # nosec
257- if isinstance (client_file , ClientFile ):
258- file_meta = client_file .to_domain_model ()
259- elif isinstance (client_file , ClientFileInProgramJob ):
260- project = await webserver_api .get_project (project_id = client_file .job_id )
261- if len (project .workbench ) > 1 :
262- raise HTTPException (
263- status_code = status .HTTP_422_UNPROCESSABLE_ENTITY ,
264- detail = f"Job_id { project .uuid } is not a valid program job." ,
265- )
266- node_id = next (iter (project .workbench .keys ()))
267- file_meta = client_file .to_domain_model (
268- project_id = project .uuid , node_id = NodeID (node_id )
269- )
270- else :
271- err_msg = f"Invalid client_file type passed: { type (client_file )= } "
272- raise TypeError (err_msg )
279+ file_meta = await _create_domain_file (webserver_api , client_file )
273280 _ , upload_links = await get_upload_links_from_s3 (
274281 user_id = user_id ,
275282 store_name = None ,
@@ -384,23 +391,7 @@ async def abort_multipart_upload(
384391 assert request # nosec
385392 assert user_id # nosec
386393
387- if isinstance (client_file , ClientFile ):
388- file = client_file .to_domain_model ()
389- elif isinstance (client_file , ClientFileInProgramJob ):
390- project = await webserver_api .get_project (project_id = client_file .job_id )
391- if len (project .workbench ) > 1 :
392- raise HTTPException (
393- status_code = status .HTTP_422_UNPROCESSABLE_ENTITY ,
394- detail = f"Job_id { project .uuid } is not a valid program job." ,
395- )
396- node_id = next (iter (project .workbench .keys ()))
397- file = client_file .to_domain_model (
398- project_id = project .uuid , node_id = NodeID (node_id )
399- )
400- else :
401- err_msg = f"Invalid client_file type passed: { type (client_file )= } "
402- raise TypeError (err_msg )
403-
394+ file = await _create_domain_file (webserver_api , client_file )
404395 abort_link : URL = await storage_client .create_abort_upload_link (
405396 file = file , query = {"user_id" : str (user_id )}
406397 )
@@ -427,32 +418,17 @@ async def complete_multipart_upload(
427418 assert file_id # nosec
428419 assert request # nosec
429420 assert user_id # nosec
430-
431- if isinstance (client_file , ClientFile ):
432- file = client_file .to_domain_model ()
433- elif isinstance (client_file , ClientFileInProgramJob ):
434- project = await webserver_api .get_project (project_id = client_file .job_id )
435- if len (project .workbench ) > 1 :
436- raise HTTPException (
437- status_code = status .HTTP_422_UNPROCESSABLE_ENTITY ,
438- detail = f"Job_id { project .uuid } is not a valid program job." ,
439- )
440- node_id = next (iter (project .workbench .keys ()))
441- file = client_file .to_domain_model (
442- project_id = project .uuid , node_id = NodeID (node_id )
443- )
444- else :
445- err_msg = f"Invalid client_file type passed: { type (client_file )= } "
446- raise TypeError (err_msg )
421+ file = await _create_domain_file (webserver_api , client_file )
447422 complete_link : URL = await storage_client .create_complete_upload_link (
448423 file = file , query = {"user_id" : str (user_id )}
449424 )
450425
451- e_tag : ETag = await complete_file_upload (
426+ e_tag : ETag | None = await complete_file_upload (
452427 uploaded_parts = uploaded_parts .parts ,
453428 upload_completion_link = TypeAdapter (AnyUrl ).validate_python (f"{ complete_link } " ),
454429 is_directory = False ,
455430 )
431+ assert e_tag is not None # nosec
456432
457433 file .e_tag = e_tag
458434 return file
0 commit comments