Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile
|
| 2 |
-
from fastapi.responses import HTMLResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
import shutil
|
| 5 |
import os
|
|
@@ -9,6 +9,8 @@ app = FastAPI()
|
|
| 9 |
UPLOAD_FOLDER = "/tmp/uploads"
|
| 10 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 11 |
|
|
|
|
|
|
|
| 12 |
# Menyediakan folder statis untuk menyimpan file yang diunggah
|
| 13 |
app.mount("/uploads", StaticFiles(directory=UPLOAD_FOLDER), name="uploads")
|
| 14 |
|
|
@@ -42,9 +44,13 @@ async def upload_file(file: UploadFile):
|
|
| 42 |
file_path = os.path.join(UPLOAD_FOLDER, file.filename)
|
| 43 |
with open(file_path, "wb") as buffer:
|
| 44 |
shutil.copyfileobj(file.file, buffer)
|
| 45 |
-
return {"filename": file.filename, "url": f"/uploads/{file.filename}"}
|
| 46 |
|
| 47 |
@app.get("/files")
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
import shutil
|
| 5 |
import os
|
|
|
|
| 9 |
UPLOAD_FOLDER = "/tmp/uploads"
|
| 10 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 11 |
|
| 12 |
+
BASE_URL = "https://rayhanzuck-cloud.hf.space"
|
| 13 |
+
|
| 14 |
# Menyediakan folder statis untuk menyimpan file yang diunggah
|
| 15 |
app.mount("/uploads", StaticFiles(directory=UPLOAD_FOLDER), name="uploads")
|
| 16 |
|
|
|
|
| 44 |
file_path = os.path.join(UPLOAD_FOLDER, file.filename)
|
| 45 |
with open(file_path, "wb") as buffer:
|
| 46 |
shutil.copyfileobj(file.file, buffer)
|
| 47 |
+
return {"filename": file.filename, "url": f"{BASE_URL}/uploads/{file.filename}"}
|
| 48 |
|
| 49 |
@app.get("/files")
|
| 50 |
+
def list_files():
|
| 51 |
+
try:
|
| 52 |
+
files = os.listdir(UPLOAD_FOLDER)
|
| 53 |
+
file_urls = [f"{BASE_URL}/uploads/{file}" for file in files]
|
| 54 |
+
return JSONResponse(content={"files": file_urls})
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|