Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,40 @@
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, Request
|
| 2 |
-
from fastapi.responses import HTMLResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
-
|
| 5 |
-
from pydantic import BaseModel
|
| 6 |
-
import shutil, os, json, io, sys
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
)
|
| 17 |
|
| 18 |
-
|
| 19 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
|
|
|
| 20 |
|
| 21 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
@app.get("/", response_class=HTMLResponse)
|
| 23 |
async def root():
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
# Upload file ke folder uploads/
|
| 28 |
@app.post("/upload")
|
| 29 |
async def upload(file: UploadFile = File(...)):
|
| 30 |
dest = os.path.join(UPLOAD_DIR, file.filename)
|
|
@@ -32,40 +42,60 @@ async def upload(file: UploadFile = File(...)):
|
|
| 32 |
shutil.copyfileobj(file.file, f)
|
| 33 |
return {"filename": file.filename}
|
| 34 |
|
| 35 |
-
# List semua file yang sudah di-upload
|
| 36 |
@app.get("/files")
|
| 37 |
-
async def
|
| 38 |
return os.listdir(UPLOAD_DIR)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
async def setup_config(request: Request):
|
| 43 |
data = await request.json()
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
@app.
|
| 60 |
-
def
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
exec(code, exec_env)
|
| 66 |
-
output = buffer.getvalue()
|
| 67 |
-
except Exception as e:
|
| 68 |
-
output = str(e)
|
| 69 |
-
finally:
|
| 70 |
-
sys.stdout = sys.__stdout__
|
| 71 |
-
return {"output": output}
|
|
|
|
| 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 |
+
# 📂 PATHS & DIRECTORY SETUP
|
| 10 |
+
# =============================
|
| 11 |
+
BASE_DIR = "/tmp" # WAJIB pake /tmp biar gak kena PermissionError
|
| 12 |
+
UPLOAD_DIR = os.path.join(BASE_DIR, "uploads")
|
| 13 |
+
STATIC_DIR = os.path.join(BASE_DIR, "static")
|
| 14 |
+
SETUP_FILE = os.path.join(BASE_DIR, "setup.json")
|
| 15 |
|
| 16 |
+
# Bikin folder kalau belum ada
|
| 17 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 18 |
+
os.makedirs(STATIC_DIR, exist_ok=True)
|
| 19 |
|
| 20 |
+
# Mount file statis
|
| 21 |
+
app.mount("/uploads", StaticFiles(directory=UPLOAD_DIR), name="uploads")
|
| 22 |
+
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
| 23 |
+
|
| 24 |
+
# =============================
|
| 25 |
+
# 🏠 MAIN PAGE
|
| 26 |
+
# =============================
|
| 27 |
@app.get("/", response_class=HTMLResponse)
|
| 28 |
async def root():
|
| 29 |
+
if os.path.exists("index.html"):
|
| 30 |
+
with open("index.html", encoding="utf-8") as f:
|
| 31 |
+
return f.read()
|
| 32 |
+
return HTMLResponse("<h1>index.html not found</h1>", status_code=404)
|
| 33 |
+
|
| 34 |
+
# =============================
|
| 35 |
+
# 📁 FILE MANAGER
|
| 36 |
+
# =============================
|
| 37 |
|
|
|
|
| 38 |
@app.post("/upload")
|
| 39 |
async def upload(file: UploadFile = File(...)):
|
| 40 |
dest = os.path.join(UPLOAD_DIR, file.filename)
|
|
|
|
| 42 |
shutil.copyfileobj(file.file, f)
|
| 43 |
return {"filename": file.filename}
|
| 44 |
|
|
|
|
| 45 |
@app.get("/files")
|
| 46 |
+
async def list_files():
|
| 47 |
return os.listdir(UPLOAD_DIR)
|
| 48 |
|
| 49 |
+
@app.post("/rename")
|
| 50 |
+
async def rename_file(request: Request):
|
|
|
|
| 51 |
data = await request.json()
|
| 52 |
+
src = os.path.join(UPLOAD_DIR, data["old"])
|
| 53 |
+
dst = os.path.join(UPLOAD_DIR, data["new"])
|
| 54 |
+
if os.path.exists(src):
|
| 55 |
+
os.rename(src, dst)
|
| 56 |
+
return {"status": "renamed"}
|
| 57 |
+
return {"status": "error", "detail": "file not found"}
|
| 58 |
|
| 59 |
+
@app.post("/delete")
|
| 60 |
+
async def delete_file(request: Request):
|
| 61 |
+
data = await request.json()
|
| 62 |
+
path = os.path.join(UPLOAD_DIR, data["filename"])
|
| 63 |
+
if os.path.exists(path):
|
| 64 |
+
if os.path.isfile(path):
|
| 65 |
+
os.remove(path)
|
| 66 |
+
elif os.path.isdir(path):
|
| 67 |
+
shutil.rmtree(path)
|
| 68 |
+
return {"status": "deleted"}
|
| 69 |
+
return {"status": "error", "detail": "file not found"}
|
| 70 |
|
| 71 |
+
@app.post("/create_file")
|
| 72 |
+
async def create_file(request: Request):
|
| 73 |
+
data = await request.json()
|
| 74 |
+
path = os.path.join(UPLOAD_DIR, data["filename"])
|
| 75 |
+
with open(path, "w") as f:
|
| 76 |
+
f.write("")
|
| 77 |
+
return {"status": "created"}
|
| 78 |
|
| 79 |
+
@app.post("/create_folder")
|
| 80 |
+
async def create_folder(request: Request):
|
| 81 |
+
data = await request.json()
|
| 82 |
+
path = os.path.join(UPLOAD_DIR, data["foldername"])
|
| 83 |
+
os.makedirs(path, exist_ok=True)
|
| 84 |
+
return {"status": "created"}
|
| 85 |
|
| 86 |
+
# =============================
|
| 87 |
+
# ⚙️ SETUP ENDPOINT
|
| 88 |
+
# =============================
|
| 89 |
+
@app.post("/setup")
|
| 90 |
+
async def save_setup(request: Request):
|
| 91 |
+
data = await request.json()
|
| 92 |
+
with open(SETUP_FILE, "w") as f:
|
| 93 |
+
json.dump(data, f, indent=2)
|
| 94 |
+
return {"status": "ok", "received": data}
|
| 95 |
|
| 96 |
+
@app.get("/setup")
|
| 97 |
+
async def load_setup():
|
| 98 |
+
if os.path.exists(SETUP_FILE):
|
| 99 |
+
with open(SETUP_FILE, "r") as f:
|
| 100 |
+
return json.load(f)
|
| 101 |
+
return {"username": "", "password": "", "start_cmd": ""}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|