-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
28 lines (22 loc) · 859 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
import os
app = FastAPI()
# تحديد مسار المجلد الذي يحتوي على ملفات PDF
PDF_FOLDER = "folder"
@app.get("/")
def read_root():
return {"message": "Welcome to the PDF viewer"}
@app.get("/pdfs/")
def list_pdfs():
try:
pdf_files = [f for f in os.listdir(PDF_FOLDER) if f.endswith(".pdf")]
return {"pdf_files": pdf_files}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/pdfs/{filename}")
def get_pdf(filename: str):
file_path = os.path.join(PDF_FOLDER, filename)
if not os.path.exists(file_path):
raise HTTPException(status_code=404, detail="File not found")
return FileResponse(file_path, media_type="application/pdf")