Update app.py
Browse files
app.py
CHANGED
|
@@ -8,21 +8,38 @@ app = FastAPI()
|
|
| 8 |
UPLOAD_DIR = "uploads"
|
| 9 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@app.post("/upload")
|
| 12 |
async def upload(file: UploadFile):
|
| 13 |
file_id = str(uuid.uuid4())[:8]
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
```
|
| 17 |
with open(file_path, "wb") as f:
|
| 18 |
f.write(await file.read())
|
| 19 |
|
| 20 |
return {
|
| 21 |
-
"link": f"/file/{file_id}_{
|
| 22 |
}
|
| 23 |
```
|
| 24 |
|
| 25 |
@app.get("/file/{filename}")
|
| 26 |
def get_file(filename: str):
|
| 27 |
file_path = f"{UPLOAD_DIR}/{filename}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return FileResponse(file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
UPLOAD_DIR = "uploads"
|
| 9 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 10 |
|
| 11 |
+
@app.get("/")
|
| 12 |
+
def home():
|
| 13 |
+
return {"message": "QuickDrop API running"}
|
| 14 |
+
|
| 15 |
@app.post("/upload")
|
| 16 |
async def upload(file: UploadFile):
|
| 17 |
file_id = str(uuid.uuid4())[:8]
|
| 18 |
+
filename = file.filename.replace(" ", "*")
|
| 19 |
+
file_path = f"{UPLOAD_DIR}/{file_id}*{filename}"
|
| 20 |
|
| 21 |
```
|
| 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}")
|
| 31 |
def get_file(filename: str):
|
| 32 |
file_path = f"{UPLOAD_DIR}/{filename}"
|
| 33 |
+
|
| 34 |
+
```
|
| 35 |
+
if not os.path.exists(file_path):
|
| 36 |
+
return {"error": "File not found"}
|
| 37 |
+
|
| 38 |
return FileResponse(file_path)
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
# ✅ THIS PART IS IMPORTANT
|
| 42 |
+
|
| 43 |
+
if **name** == "**main**":
|
| 44 |
+
import uvicorn
|
| 45 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)
|