Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,29 @@
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, Request
|
| 2 |
-
from fastapi.responses import HTMLResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
import shutil, os, json
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 12 |
os.makedirs(STATIC_DIR, exist_ok=True)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
| 16 |
app.mount("/uploads", StaticFiles(directory=UPLOAD_DIR), name="uploads")
|
| 17 |
|
| 18 |
-
# Serve halaman utama
|
| 19 |
@app.get("/", response_class=HTMLResponse)
|
| 20 |
async def root():
|
| 21 |
with open("index.html", encoding="utf-8") as f:
|
| 22 |
return f.read()
|
| 23 |
|
| 24 |
-
# Upload file
|
| 25 |
@app.post("/upload")
|
| 26 |
async def upload(file: UploadFile = File(...)):
|
| 27 |
dest = os.path.join(UPLOAD_DIR, file.filename)
|
|
@@ -29,12 +31,10 @@ async def upload(file: UploadFile = File(...)):
|
|
| 29 |
shutil.copyfileobj(file.file, f)
|
| 30 |
return {"filename": file.filename}
|
| 31 |
|
| 32 |
-
# Ambil list file/folder dari uploads
|
| 33 |
@app.get("/files")
|
| 34 |
async def files():
|
| 35 |
return os.listdir(UPLOAD_DIR)
|
| 36 |
|
| 37 |
-
# Rename file
|
| 38 |
@app.post("/rename")
|
| 39 |
async def rename_file(request: Request):
|
| 40 |
data = await request.json()
|
|
@@ -45,7 +45,6 @@ async def rename_file(request: Request):
|
|
| 45 |
return {"status": "renamed"}
|
| 46 |
return {"status": "error", "detail": "file not found"}
|
| 47 |
|
| 48 |
-
# Delete file
|
| 49 |
@app.post("/delete")
|
| 50 |
async def delete_file(request: Request):
|
| 51 |
data = await request.json()
|
|
@@ -58,7 +57,6 @@ async def delete_file(request: Request):
|
|
| 58 |
return {"status": "deleted"}
|
| 59 |
return {"status": "error", "detail": "file not found"}
|
| 60 |
|
| 61 |
-
# Buat file kosong
|
| 62 |
@app.post("/create_file")
|
| 63 |
async def create_file(request: Request):
|
| 64 |
data = await request.json()
|
|
@@ -67,7 +65,6 @@ async def create_file(request: Request):
|
|
| 67 |
f.write("")
|
| 68 |
return {"status": "created"}
|
| 69 |
|
| 70 |
-
# Buat folder
|
| 71 |
@app.post("/create_folder")
|
| 72 |
async def create_folder(request: Request):
|
| 73 |
data = await request.json()
|
|
@@ -75,18 +72,16 @@ async def create_folder(request: Request):
|
|
| 75 |
os.makedirs(path, exist_ok=True)
|
| 76 |
return {"status": "created"}
|
| 77 |
|
| 78 |
-
# Simpan setup
|
| 79 |
@app.post("/setup")
|
| 80 |
async def setup_config(request: Request):
|
| 81 |
data = await request.json()
|
| 82 |
-
with open(
|
| 83 |
json.dump(data, f, indent=2)
|
| 84 |
return {"status": "ok", "received": data}
|
| 85 |
|
| 86 |
-
# Ambil setup
|
| 87 |
@app.get("/setup")
|
| 88 |
async def get_setup():
|
| 89 |
-
if os.path.exists(
|
| 90 |
-
with open(
|
| 91 |
return json.load(f)
|
| 92 |
return {"username": "", "password": "", "start_cmd": ""}
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, Request
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
import shutil, os, json
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
# Pindah semua direktori ke /tmp biar gak permission denied
|
| 9 |
+
BASE_DIR = "/tmp"
|
| 10 |
+
UPLOAD_DIR = os.path.join(BASE_DIR, "uploads")
|
| 11 |
+
STATIC_DIR = os.path.join(BASE_DIR, "static")
|
| 12 |
+
SETUP_FILE = os.path.join(BASE_DIR, "setup.json")
|
| 13 |
+
|
| 14 |
+
# Bikin direktori kalau belum ada
|
| 15 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 16 |
os.makedirs(STATIC_DIR, exist_ok=True)
|
| 17 |
|
| 18 |
+
# Mount direktori statis
|
| 19 |
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
| 20 |
app.mount("/uploads", StaticFiles(directory=UPLOAD_DIR), name="uploads")
|
| 21 |
|
|
|
|
| 22 |
@app.get("/", response_class=HTMLResponse)
|
| 23 |
async def root():
|
| 24 |
with open("index.html", encoding="utf-8") as f:
|
| 25 |
return f.read()
|
| 26 |
|
|
|
|
| 27 |
@app.post("/upload")
|
| 28 |
async def upload(file: UploadFile = File(...)):
|
| 29 |
dest = os.path.join(UPLOAD_DIR, file.filename)
|
|
|
|
| 31 |
shutil.copyfileobj(file.file, f)
|
| 32 |
return {"filename": file.filename}
|
| 33 |
|
|
|
|
| 34 |
@app.get("/files")
|
| 35 |
async def files():
|
| 36 |
return os.listdir(UPLOAD_DIR)
|
| 37 |
|
|
|
|
| 38 |
@app.post("/rename")
|
| 39 |
async def rename_file(request: Request):
|
| 40 |
data = await request.json()
|
|
|
|
| 45 |
return {"status": "renamed"}
|
| 46 |
return {"status": "error", "detail": "file not found"}
|
| 47 |
|
|
|
|
| 48 |
@app.post("/delete")
|
| 49 |
async def delete_file(request: Request):
|
| 50 |
data = await request.json()
|
|
|
|
| 57 |
return {"status": "deleted"}
|
| 58 |
return {"status": "error", "detail": "file not found"}
|
| 59 |
|
|
|
|
| 60 |
@app.post("/create_file")
|
| 61 |
async def create_file(request: Request):
|
| 62 |
data = await request.json()
|
|
|
|
| 65 |
f.write("")
|
| 66 |
return {"status": "created"}
|
| 67 |
|
|
|
|
| 68 |
@app.post("/create_folder")
|
| 69 |
async def create_folder(request: Request):
|
| 70 |
data = await request.json()
|
|
|
|
| 72 |
os.makedirs(path, exist_ok=True)
|
| 73 |
return {"status": "created"}
|
| 74 |
|
|
|
|
| 75 |
@app.post("/setup")
|
| 76 |
async def setup_config(request: Request):
|
| 77 |
data = await request.json()
|
| 78 |
+
with open(SETUP_FILE, "w") as f:
|
| 79 |
json.dump(data, f, indent=2)
|
| 80 |
return {"status": "ok", "received": data}
|
| 81 |
|
|
|
|
| 82 |
@app.get("/setup")
|
| 83 |
async def get_setup():
|
| 84 |
+
if os.path.exists(SETUP_FILE):
|
| 85 |
+
with open(SETUP_FILE, "r") as f:
|
| 86 |
return json.load(f)
|
| 87 |
return {"username": "", "password": "", "start_cmd": ""}
|