@@ -573,14 +573,37 @@ def upload_files_for_chat(
573573 }
574574
575575
576- @router .get ("/file/{file_id}" )
576+ @router .get ("/file/{file_id:path }" )
577577def fetch_chat_file (
578578 file_id : str ,
579579 db_session : Session = Depends (get_session ),
580580 _ : User | None = Depends (current_user ),
581581) -> Response :
582582 file_store = get_default_file_store (db_session )
583- file_io = file_store .read_file (file_id , mode = "b" )
584- # NOTE: specifying "image/jpeg" here, but it still works for pngs
585- # TODO: do this properly
586- return Response (content = file_io .read (), media_type = "image/jpeg" )
583+
584+ try :
585+ file_io = file_store .read_file (file_id , mode = "b" )
586+
587+ # Determine content type based on file extension if it's a path
588+ content_type = "application/octet-stream"
589+ if "/" in file_id :
590+ # It's a path like uuid/filename, extract filename
591+ filename = file_id .split ("/" )[- 1 ].lower ()
592+ if filename .endswith (".pdf" ):
593+ content_type = "application/pdf"
594+ elif filename .endswith ((".jpg" , ".jpeg" )):
595+ content_type = "image/jpeg"
596+ elif filename .endswith (".png" ):
597+ content_type = "image/png"
598+ elif filename .endswith (".txt" ):
599+ content_type = "text/plain"
600+ elif filename .endswith ((".doc" , ".docx" )):
601+ content_type = "application/msword"
602+ else :
603+ # For simple IDs (chat uploads)
604+ content_type = "image/jpeg"
605+
606+ return Response (content = file_io .read (), media_type = content_type )
607+ except Exception as e :
608+ logger .error (f"Error fetching file { file_id } : { str (e )} " )
609+ raise HTTPException (status_code = 404 , detail = "File not found" )
0 commit comments