Update app.py
Browse files
app.py
CHANGED
|
@@ -4,10 +4,12 @@ from fastapi.templating import Jinja2Templates
|
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
import os
|
| 6 |
import uuid
|
|
|
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
# CORS
|
| 11 |
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
|
@@ -17,20 +19,22 @@ allow_methods=["*"],
|
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# Templates
|
| 21 |
-
|
| 22 |
templates = Jinja2Templates(directory="templates")
|
| 23 |
|
| 24 |
UPLOAD_DIR = "uploads"
|
| 25 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Serve frontend
|
| 28 |
|
| 29 |
@app.get("/", response_class=HTMLResponse)
|
| 30 |
def home(request: Request):
|
| 31 |
return templates.TemplateResponse("index.html", {"request": request})
|
| 32 |
|
| 33 |
-
# Upload
|
| 34 |
|
| 35 |
@app.post("/upload")
|
| 36 |
async def upload(file: UploadFile):
|
|
@@ -42,10 +46,13 @@ file_path = f"{UPLOAD_DIR}/{file_id}*{filename}"
|
|
| 42 |
with open(file_path, "wb") as f:
|
| 43 |
f.write(await file.read())
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
return {"link": f"/file/{file_id}_{filename}"}
|
| 46 |
```
|
| 47 |
|
| 48 |
-
# Download
|
| 49 |
|
| 50 |
@app.get("/file/{filename}")
|
| 51 |
def get_file(filename: str):
|
|
@@ -53,12 +60,32 @@ file_path = f"{UPLOAD_DIR}/{filename}"
|
|
| 53 |
|
| 54 |
```
|
| 55 |
if not os.path.exists(file_path):
|
| 56 |
-
return {"error": "File not found"}
|
| 57 |
|
| 58 |
return FileResponse(file_path)
|
| 59 |
```
|
| 60 |
|
| 61 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if **name** == "**main**":
|
| 64 |
import uvicorn
|
|
|
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
import os
|
| 6 |
import uuid
|
| 7 |
+
import time
|
| 8 |
+
import threading
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
+
# CORS
|
| 13 |
|
| 14 |
app.add_middleware(
|
| 15 |
CORSMiddleware,
|
|
|
|
| 19 |
allow_headers=["*"],
|
| 20 |
)
|
| 21 |
|
|
|
|
|
|
|
| 22 |
templates = Jinja2Templates(directory="templates")
|
| 23 |
|
| 24 |
UPLOAD_DIR = "uploads"
|
| 25 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 26 |
|
| 27 |
+
# Store file timestamps
|
| 28 |
+
|
| 29 |
+
file_times = {}
|
| 30 |
+
|
| 31 |
# Serve frontend
|
| 32 |
|
| 33 |
@app.get("/", response_class=HTMLResponse)
|
| 34 |
def home(request: Request):
|
| 35 |
return templates.TemplateResponse("index.html", {"request": request})
|
| 36 |
|
| 37 |
+
# Upload
|
| 38 |
|
| 39 |
@app.post("/upload")
|
| 40 |
async def upload(file: UploadFile):
|
|
|
|
| 46 |
with open(file_path, "wb") as f:
|
| 47 |
f.write(await file.read())
|
| 48 |
|
| 49 |
+
# Save upload time
|
| 50 |
+
file_times[file_path] = time.time()
|
| 51 |
+
|
| 52 |
return {"link": f"/file/{file_id}_{filename}"}
|
| 53 |
```
|
| 54 |
|
| 55 |
+
# Download
|
| 56 |
|
| 57 |
@app.get("/file/{filename}")
|
| 58 |
def get_file(filename: str):
|
|
|
|
| 60 |
|
| 61 |
```
|
| 62 |
if not os.path.exists(file_path):
|
| 63 |
+
return {"error": "File expired or not found"}
|
| 64 |
|
| 65 |
return FileResponse(file_path)
|
| 66 |
```
|
| 67 |
|
| 68 |
+
# 🧹 AUTO DELETE THREAD
|
| 69 |
+
|
| 70 |
+
def cleanup_files():
|
| 71 |
+
while True:
|
| 72 |
+
now = time.time()
|
| 73 |
+
|
| 74 |
+
```
|
| 75 |
+
for path in list(file_times.keys()):
|
| 76 |
+
if now - file_times[path] > 300: # 5 minutes = 300 sec
|
| 77 |
+
if os.path.exists(path):
|
| 78 |
+
os.remove(path)
|
| 79 |
+
del file_times[path]
|
| 80 |
+
|
| 81 |
+
time.sleep(60) # check every 1 minute
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
+
# Start cleanup thread
|
| 85 |
+
|
| 86 |
+
threading.Thread(target=cleanup_files, daemon=True).start()
|
| 87 |
+
|
| 88 |
+
# Run
|
| 89 |
|
| 90 |
if **name** == "**main**":
|
| 91 |
import uvicorn
|