Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.staticfiles import StaticFiles
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
UPLOAD_FOLDER = "uploads"
|
| 8 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 9 |
+
|
| 10 |
+
@app.post("/upload/")
|
| 11 |
+
async def upload_file(file: UploadFile = File(...)):
|
| 12 |
+
file_path = os.path.join(UPLOAD_FOLDER, file.filename)
|
| 13 |
+
|
| 14 |
+
with open(file_path, "wb") as f:
|
| 15 |
+
f.write(await file.read())
|
| 16 |
+
|
| 17 |
+
return {"message": "File berhasil diunggah!", "file_url": f"/uploads/{file.filename}"}
|
| 18 |
+
|
| 19 |
+
# Menyediakan file yang diunggah agar bisa diakses publik
|
| 20 |
+
app.mount("/uploads", StaticFiles(directory="uploads"), name="uploads")
|