Avinashnalla7 commited on
Commit
c509456
·
1 Parent(s): c02018d

Fix get_pdf: return bytes Response (avoid hanging stream)

Browse files
Files changed (1) hide show
  1. backend/api.py +8 -11
backend/api.py CHANGED
@@ -1,5 +1,7 @@
1
  from __future__ import annotations
2
 
 
 
3
 
4
  import os
5
  SENDCFG_FAST_OK=(os.getenv("SENDCFG_FAST_OK","0")=="1")
@@ -96,18 +98,13 @@ def health():
96
 
97
  @app.get("/api/pdf/{pdf_id}")
98
  def get_pdf(pdf_id: str):
99
- path = UPLOADS_DIR / f"{pdf_id}.pdf"
100
- if not path.exists():
101
- raise HTTPException(status_code=404, detail="PDF not found")
102
-
103
- name_path = UPLOADS_DIR / f"{pdf_id}.name.txt"
104
- pdf_name = name_path.read_text(encoding="utf-8").strip() if name_path.exists() else f"{pdf_id}.pdf"
105
-
106
- resp = FileResponse(path, media_type="application/pdf", filename=pdf_name)
107
- resp.headers["X-PDF-Name"] = pdf_name
108
- return resp
109
 
110
- @app.post("/api/pdf/{pdf_id}")
111
  async def put_pdf(pdf_id: str, file: UploadFile = File(...), pdf_name: str = Form("")):
112
  UPLOADS_DIR.mkdir(parents=True, exist_ok=True)
113
  data = await file.read()
 
1
  from __future__ import annotations
2
 
3
+ from fastapi.responses import Response
4
+
5
 
6
  import os
7
  SENDCFG_FAST_OK=(os.getenv("SENDCFG_FAST_OK","0")=="1")
 
98
 
99
  @app.get("/api/pdf/{pdf_id}")
100
  def get_pdf(pdf_id: str):
101
+ pdf_path = _pdf_path(pdf_id)
102
+ try:
103
+ b = pdf_path.read_bytes()
104
+ except FileNotFoundError:
105
+ raise HTTPException(status_code=404, detail="pdf_not_found")
106
+ return Response(content=b, media_type="application/pdf")
 
 
 
 
107
 
 
108
  async def put_pdf(pdf_id: str, file: UploadFile = File(...), pdf_name: str = Form("")):
109
  UPLOADS_DIR.mkdir(parents=True, exist_ok=True)
110
  data = await file.read()