Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,70 +5,85 @@ import psutil
|
|
| 5 |
import httpx
|
| 6 |
import shutil
|
| 7 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, UploadFile, File, Body
|
| 8 |
-
from fastapi.responses import HTMLResponse, JSONResponse
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
-
# Environment variable থেকে পাসওয়ার্ড নেওয়া
|
| 13 |
ACCESS_PASSWORD = os.getenv("ACCESS_PASSWORD", "admin")
|
| 14 |
|
| 15 |
-
# সে
|
| 16 |
-
current_working_dir =
|
| 17 |
|
| 18 |
@app.get("/")
|
| 19 |
async def get():
|
| 20 |
with open("index.html", "r", encoding="utf-8") as f:
|
| 21 |
return HTMLResponse(content=f.read())
|
| 22 |
|
| 23 |
-
# --- ফাইল ম্যানেজার API ---
|
| 24 |
@app.get("/api/files")
|
| 25 |
-
async def list_files(path: str = "
|
| 26 |
try:
|
| 27 |
-
|
|
|
|
| 28 |
files = []
|
| 29 |
-
for entry in os.scandir(
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
return sorted(files, key=lambda x: not x['is_dir'])
|
| 37 |
except Exception as e:
|
| 38 |
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 39 |
|
| 40 |
@app.get("/api/read")
|
| 41 |
async def read_file(path: str):
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
@app.post("/api/save")
|
| 47 |
async def save_file(path: str = Body(...), content: str = Body(...)):
|
| 48 |
-
|
| 49 |
-
with open(target, "w", encoding="utf-8") as f:
|
| 50 |
f.write(content)
|
| 51 |
return {"status": "saved"}
|
| 52 |
|
| 53 |
@app.post("/api/create")
|
| 54 |
async def create_item(name: str = Body(...), is_dir: bool = Body(...), path: str = Body(...)):
|
| 55 |
-
target = os.path.join(
|
| 56 |
if is_dir:
|
| 57 |
os.makedirs(target, exist_ok=True)
|
| 58 |
else:
|
| 59 |
with open(target, "w") as f: f.write("")
|
| 60 |
return {"status": "created"}
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
@app.delete("/api/delete")
|
| 63 |
async def delete_file(path: str):
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
shutil.rmtree(target)
|
| 67 |
else:
|
| 68 |
-
os.remove(
|
| 69 |
return {"message": "Deleted"}
|
| 70 |
|
| 71 |
-
# ---
|
| 72 |
@app.get("/proxy")
|
| 73 |
async def proxy(url: str = "http://localhost:8080"):
|
| 74 |
async with httpx.AsyncClient() as client:
|
|
@@ -78,7 +93,7 @@ async def proxy(url: str = "http://localhost:8080"):
|
|
| 78 |
except Exception as e:
|
| 79 |
return HTMLResponse(content=f"<div style='color:red;padding:20px;'>Error: {str(e)}</div>")
|
| 80 |
|
| 81 |
-
# --- টার্মিনাল সেশন
|
| 82 |
@app.websocket("/ws")
|
| 83 |
async def websocket_endpoint(websocket: WebSocket):
|
| 84 |
global current_working_dir
|
|
@@ -93,13 +108,12 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 93 |
|
| 94 |
while True:
|
| 95 |
command = await websocket.receive_text()
|
| 96 |
-
# 'cd' কমান��ড হ্যান্ডলিং
|
| 97 |
if command.startswith("cd "):
|
| 98 |
-
|
| 99 |
try:
|
| 100 |
-
os.chdir(os.path.expanduser(
|
| 101 |
current_working_dir = os.getcwd()
|
| 102 |
-
await websocket.send_text(f"
|
| 103 |
except Exception as e:
|
| 104 |
await websocket.send_text(str(e))
|
| 105 |
continue
|
|
@@ -108,7 +122,7 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 108 |
command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, cwd=current_working_dir
|
| 109 |
)
|
| 110 |
stdout, stderr = await process.communicate()
|
| 111 |
-
output = stdout.decode().strip() or stderr.decode().strip() or "
|
| 112 |
await websocket.send_text(output)
|
| 113 |
except WebSocketDisconnect:
|
| 114 |
pass
|
|
|
|
| 5 |
import httpx
|
| 6 |
import shutil
|
| 7 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, UploadFile, File, Body
|
| 8 |
+
from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
+
# Environment variable থেকে পাসওয়ার্ড নেওয়া
|
| 13 |
ACCESS_PASSWORD = os.getenv("ACCESS_PASSWORD", "admin")
|
| 14 |
|
| 15 |
+
# সিস্টেম রুট থেকে শুরু করার অনুমতি দেওয়া হয়েছে
|
| 16 |
+
current_working_dir = "/"
|
| 17 |
|
| 18 |
@app.get("/")
|
| 19 |
async def get():
|
| 20 |
with open("index.html", "r", encoding="utf-8") as f:
|
| 21 |
return HTMLResponse(content=f.read())
|
| 22 |
|
| 23 |
+
# --- ফাইল ম্যানেজার API (Root Access Enabled) ---
|
| 24 |
@app.get("/api/files")
|
| 25 |
+
async def list_files(path: str = "/"):
|
| 26 |
try:
|
| 27 |
+
# পাথ যদি খালি থাকে তবে রুটে নিয়ে যাবে
|
| 28 |
+
target_path = path if path.startswith("/") else "/"
|
| 29 |
files = []
|
| 30 |
+
for entry in os.scandir(target_path):
|
| 31 |
+
try:
|
| 32 |
+
files.append({
|
| 33 |
+
"name": entry.name,
|
| 34 |
+
"is_dir": entry.is_dir(),
|
| 35 |
+
"path": os.path.abspath(entry.path),
|
| 36 |
+
"size": f"{entry.stat().st_size / 1024:.1f} KB" if entry.is_file() else "-"
|
| 37 |
+
})
|
| 38 |
+
except PermissionError:
|
| 39 |
+
continue # পারমিশন নেই এমন ফাইল স্কিপ করবে
|
| 40 |
return sorted(files, key=lambda x: not x['is_dir'])
|
| 41 |
except Exception as e:
|
| 42 |
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 43 |
|
| 44 |
@app.get("/api/read")
|
| 45 |
async def read_file(path: str):
|
| 46 |
+
try:
|
| 47 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 48 |
+
return {"content": f.read()}
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return {"content": f"Error: {str(e)}"}
|
| 51 |
|
| 52 |
@app.post("/api/save")
|
| 53 |
async def save_file(path: str = Body(...), content: str = Body(...)):
|
| 54 |
+
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
| 55 |
f.write(content)
|
| 56 |
return {"status": "saved"}
|
| 57 |
|
| 58 |
@app.post("/api/create")
|
| 59 |
async def create_item(name: str = Body(...), is_dir: bool = Body(...), path: str = Body(...)):
|
| 60 |
+
target = os.path.join(path, name)
|
| 61 |
if is_dir:
|
| 62 |
os.makedirs(target, exist_ok=True)
|
| 63 |
else:
|
| 64 |
with open(target, "w") as f: f.write("")
|
| 65 |
return {"status": "created"}
|
| 66 |
|
| 67 |
+
@app.post("/api/upload")
|
| 68 |
+
async def upload_file(file: UploadFile = File(...), path: str = "/"):
|
| 69 |
+
target_path = os.path.join(path, file.filename)
|
| 70 |
+
with open(target_path, "wb") as buffer:
|
| 71 |
+
shutil.copyfileobj(file.file, buffer)
|
| 72 |
+
return {"message": "Uploaded"}
|
| 73 |
+
|
| 74 |
+
@app.get("/api/download")
|
| 75 |
+
async def download_file(path: str):
|
| 76 |
+
return FileResponse(path)
|
| 77 |
+
|
| 78 |
@app.delete("/api/delete")
|
| 79 |
async def delete_file(path: str):
|
| 80 |
+
if os.path.isdir(path):
|
| 81 |
+
shutil.rmtree(path)
|
|
|
|
| 82 |
else:
|
| 83 |
+
os.remove(path)
|
| 84 |
return {"message": "Deleted"}
|
| 85 |
|
| 86 |
+
# --- প্রক্সি ---
|
| 87 |
@app.get("/proxy")
|
| 88 |
async def proxy(url: str = "http://localhost:8080"):
|
| 89 |
async with httpx.AsyncClient() as client:
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
return HTMLResponse(content=f"<div style='color:red;padding:20px;'>Error: {str(e)}</div>")
|
| 95 |
|
| 96 |
+
# --- টার্মিনাল সেশন ---
|
| 97 |
@app.websocket("/ws")
|
| 98 |
async def websocket_endpoint(websocket: WebSocket):
|
| 99 |
global current_working_dir
|
|
|
|
| 108 |
|
| 109 |
while True:
|
| 110 |
command = await websocket.receive_text()
|
|
|
|
| 111 |
if command.startswith("cd "):
|
| 112 |
+
new_p = command[3:].strip()
|
| 113 |
try:
|
| 114 |
+
os.chdir(os.path.expanduser(new_p))
|
| 115 |
current_working_dir = os.getcwd()
|
| 116 |
+
await websocket.send_text(f"CWD: {current_working_dir}")
|
| 117 |
except Exception as e:
|
| 118 |
await websocket.send_text(str(e))
|
| 119 |
continue
|
|
|
|
| 122 |
command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, cwd=current_working_dir
|
| 123 |
)
|
| 124 |
stdout, stderr = await process.communicate()
|
| 125 |
+
output = stdout.decode().strip() or stderr.decode().strip() or "Done."
|
| 126 |
await websocket.send_text(output)
|
| 127 |
except WebSocketDisconnect:
|
| 128 |
pass
|