Spaces:
Sleeping
Sleeping
Commit ·
ea68cfc
1
Parent(s): 5c59b0f
api: serve pdfs from SFTP for trainer UI
Browse files- backend/api.py +16 -1
backend/api.py
CHANGED
|
@@ -9,6 +9,7 @@ from dotenv import load_dotenv
|
|
| 9 |
from fastapi import FastAPI, HTTPException, Response
|
| 10 |
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
from fastapi.responses import FileResponse
|
|
|
|
| 12 |
from backend.sftp_store import SFTPStore
|
| 13 |
|
| 14 |
|
|
@@ -103,7 +104,21 @@ def root() -> Dict[str, str]:
|
|
| 103 |
|
| 104 |
@app.get("/api/pdf/{pdf_id}")
|
| 105 |
def get_pdf(pdf_id: str):
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
remote_rel = f"pdfs/{pdf_id}.pdf"
|
| 108 |
if not store.exists(remote_rel):
|
| 109 |
raise HTTPException(status_code=404, detail="PDF not found")
|
|
|
|
| 9 |
from fastapi import FastAPI, HTTPException, Response
|
| 10 |
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
from fastapi.responses import FileResponse
|
| 12 |
+
from backend.sftp_store import download_bytes
|
| 13 |
from backend.sftp_store import SFTPStore
|
| 14 |
|
| 15 |
|
|
|
|
| 104 |
|
| 105 |
@app.get("/api/pdf/{pdf_id}")
|
| 106 |
def get_pdf(pdf_id: str):
|
| 107 |
+
# Read PDF bytes from SFTP: /inserio/pdfs/{pdf_id}.pdf
|
| 108 |
+
rel = f"pdfs/{pdf_id}.pdf"
|
| 109 |
+
try:
|
| 110 |
+
data = download_bytes(rel)
|
| 111 |
+
except FileNotFoundError:
|
| 112 |
+
raise HTTPException(status_code=404, detail="pdf not found")
|
| 113 |
+
except Exception as e:
|
| 114 |
+
raise HTTPException(status_code=500, detail=f"sftp read failed: {e}")
|
| 115 |
+
|
| 116 |
+
# Stream bytes back to UI
|
| 117 |
+
resp = PlainTextResponse(content=data)
|
| 118 |
+
resp.media_type = "application/pdf"
|
| 119 |
+
resp.headers["X-PDF-Name"] = f"{pdf_id}.pdf"
|
| 120 |
+
return resp
|
| 121 |
+
|
| 122 |
remote_rel = f"pdfs/{pdf_id}.pdf"
|
| 123 |
if not store.exists(remote_rel):
|
| 124 |
raise HTTPException(status_code=404, detail="PDF not found")
|