Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
+
import os
|
| 4 |
+
import shutil
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# Directory to store uploaded files
|
| 9 |
+
UPLOAD_DIR = "/app/uploads"
|
| 10 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
@app.post("/upload/")
|
| 13 |
+
async def upload_file(file: UploadFile = File(...)):
|
| 14 |
+
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 15 |
+
with open(file_path, "wb") as buffer:
|
| 16 |
+
shutil.copyfileobj(file.file, buffer)
|
| 17 |
+
return {"filename": file.filename}
|
| 18 |
+
|
| 19 |
+
@app.get("/download/{filename}")
|
| 20 |
+
async def download_file(filename: str):
|
| 21 |
+
file_path = os.path.join(UPLOAD_DIR, filename)
|
| 22 |
+
if os.path.exists(file_path):
|
| 23 |
+
return FileResponse(file_path, media_type='application/octet-stream', filename=filename)
|
| 24 |
+
return {"error": "File not found"}
|