Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,21 @@
|
|
| 1 |
from fastapi import FastAPI, UploadFile
|
| 2 |
from fastapi.responses import FileResponse
|
|
|
|
| 3 |
import os
|
| 4 |
import uuid
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
UPLOAD_DIR = "uploads"
|
| 9 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 10 |
|
|
@@ -22,9 +33,7 @@ file_path = f"{UPLOAD_DIR}/{file_id}*{filename}"
|
|
| 22 |
with open(file_path, "wb") as f:
|
| 23 |
f.write(await file.read())
|
| 24 |
|
| 25 |
-
return {
|
| 26 |
-
"link": f"/file/{file_id}_{filename}"
|
| 27 |
-
}
|
| 28 |
```
|
| 29 |
|
| 30 |
@app.get("/file/{filename}")
|
|
@@ -38,7 +47,7 @@ if not os.path.exists(file_path):
|
|
| 38 |
return FileResponse(file_path)
|
| 39 |
```
|
| 40 |
|
| 41 |
-
#
|
| 42 |
|
| 43 |
if **name** == "**main**":
|
| 44 |
import uvicorn
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import os
|
| 5 |
import uuid
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# ✅ Enable CORS
|
| 10 |
+
|
| 11 |
+
app.add_middleware(
|
| 12 |
+
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"],
|
| 14 |
+
allow_credentials=True,
|
| 15 |
+
allow_methods=["*"],
|
| 16 |
+
allow_headers=["*"],
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
UPLOAD_DIR = "uploads"
|
| 20 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 21 |
|
|
|
|
| 33 |
with open(file_path, "wb") as f:
|
| 34 |
f.write(await file.read())
|
| 35 |
|
| 36 |
+
return {"link": f"/file/{file_id}_{filename}"}
|
|
|
|
|
|
|
| 37 |
```
|
| 38 |
|
| 39 |
@app.get("/file/{filename}")
|
|
|
|
| 47 |
return FileResponse(file_path)
|
| 48 |
```
|
| 49 |
|
| 50 |
+
# Run with python app.py
|
| 51 |
|
| 52 |
if **name** == "**main**":
|
| 53 |
import uvicorn
|