Hamza4100 commited on
Commit
0b8ae89
·
verified ·
1 Parent(s): 0511f80

Update app/routes/upload.py

Browse files
Files changed (1) hide show
  1. app/routes/upload.py +21 -0
app/routes/upload.py CHANGED
@@ -254,6 +254,27 @@ async def update_report(
254
  }
255
 
256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
  @router.delete("/upload/document/{file_id}")
258
  async def delete_document(
259
  file_id: str,
 
254
  }
255
 
256
 
257
+ @router.get("/upload/file/{file_id}")
258
+ async def get_raw_file(file_id: str, db: Session = Depends(get_db)):
259
+ """Stream raw file from PostgreSQL vault or disk storage."""
260
+ from fastapi.responses import Response
261
+ doc = db.query(Document).filter(Document.file_id == file_id).first()
262
+ if doc and doc.file_data:
263
+ ftype = (doc.file_type or "").lower()
264
+ content_type = "image/jpeg" if ftype in ["jpg", "jpeg", "image/jpeg", "image/jpg"] else "application/pdf" if ftype in ["pdf", "application/pdf"] else "image/png" if ftype in ["png", "image/png"] else "application/octet-stream"
265
+ return Response(content=doc.file_data, media_type=content_type)
266
+
267
+ if os.path.exists(UPLOAD_FOLDER):
268
+ for fname in os.listdir(UPLOAD_FOLDER):
269
+ if fname.startswith(file_id):
270
+ file_path = os.path.join(UPLOAD_FOLDER, fname)
271
+ with open(file_path, "rb") as f:
272
+ data = f.read()
273
+ return Response(content=data, media_type="image/jpeg")
274
+
275
+ raise HTTPException(status_code=404, detail="Document file not found in EMR vault")
276
+
277
+
278
  @router.delete("/upload/document/{file_id}")
279
  async def delete_document(
280
  file_id: str,