Upload download_api.py
Browse files- download_api.py +20 -0
download_api.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
ANNOTATIONS_DIR = Path("annotations").resolve()
|
| 9 |
+
|
| 10 |
+
@app.get("/download/{filename}")
|
| 11 |
+
def download_file(filename: str):
|
| 12 |
+
file_path = ANNOTATIONS_DIR / filename
|
| 13 |
+
if not file_path.exists() or not file_path.is_file():
|
| 14 |
+
raise HTTPException(status_code=404, detail="File not found")
|
| 15 |
+
return FileResponse(str(file_path), filename=filename)
|
| 16 |
+
|
| 17 |
+
@app.get("/")
|
| 18 |
+
def root():
|
| 19 |
+
files = [f.name for f in ANNOTATIONS_DIR.glob("*.json")]
|
| 20 |
+
return {"message": "Use /download/{filename} to download a file.", "available_files": files}
|