Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
import asyncio
|
| 3 |
-
import os
|
| 4 |
import psutil
|
| 5 |
import httpx
|
| 6 |
import shutil
|
|
@@ -8,25 +8,29 @@ from fastapi import FastAPI, WebSocket, WebSocketDisconnect, UploadFile, File, B
|
|
| 8 |
from fastapi.responses import HTMLResponse, JSONResponse
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@app.get("/")
|
| 14 |
async def get():
|
| 15 |
with open("index.html", "r", encoding="utf-8") as f:
|
| 16 |
return HTMLResponse(content=f.read())
|
| 17 |
|
| 18 |
-
# --- ফাইল ম্যানেজার
|
| 19 |
@app.get("/api/files")
|
| 20 |
async def list_files(path: str = "."):
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
-
target_path = os.path.abspath(path)
|
| 24 |
files = []
|
| 25 |
-
for entry in os.scandir(
|
| 26 |
files.append({
|
| 27 |
"name": entry.name,
|
| 28 |
"is_dir": entry.is_dir(),
|
| 29 |
-
"path": os.path.relpath(entry.path,
|
| 30 |
"size": f"{entry.stat().st_size / 1024:.1f} KB" if entry.is_file() else "-"
|
| 31 |
})
|
| 32 |
return sorted(files, key=lambda x: not x['is_dir'])
|
|
@@ -35,31 +39,36 @@ async def list_files(path: str = "."):
|
|
| 35 |
|
| 36 |
@app.get("/api/read")
|
| 37 |
async def read_file(path: str):
|
| 38 |
-
|
|
|
|
| 39 |
return {"content": f.read()}
|
| 40 |
|
| 41 |
@app.post("/api/save")
|
| 42 |
async def save_file(path: str = Body(...), content: str = Body(...)):
|
| 43 |
-
|
|
|
|
| 44 |
f.write(content)
|
| 45 |
return {"status": "saved"}
|
| 46 |
|
| 47 |
-
@app.post("/api/
|
| 48 |
-
async def
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
@app.delete("/api/delete")
|
| 55 |
async def delete_file(path: str):
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
else:
|
| 59 |
-
os.remove(
|
| 60 |
return {"message": "Deleted"}
|
| 61 |
|
| 62 |
-
# --- প্র
|
| 63 |
@app.get("/proxy")
|
| 64 |
async def proxy(url: str = "http://localhost:8080"):
|
| 65 |
async with httpx.AsyncClient() as client:
|
|
@@ -69,8 +78,10 @@ async def proxy(url: str = "http://localhost:8080"):
|
|
| 69 |
except Exception as e:
|
| 70 |
return HTMLResponse(content=f"<div style='color:red;padding:20px;'>Error: {str(e)}</div>")
|
| 71 |
|
|
|
|
| 72 |
@app.websocket("/ws")
|
| 73 |
async def websocket_endpoint(websocket: WebSocket):
|
|
|
|
| 74 |
await websocket.accept()
|
| 75 |
try:
|
| 76 |
auth = await websocket.receive_text()
|
|
@@ -79,12 +90,25 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 79 |
await websocket.close()
|
| 80 |
return
|
| 81 |
await websocket.send_text("AUTH_SUCCESS")
|
|
|
|
| 82 |
while True:
|
| 83 |
command = await websocket.receive_text()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
process = await asyncio.create_subprocess_shell(
|
| 85 |
-
command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
|
| 86 |
)
|
| 87 |
stdout, stderr = await process.communicate()
|
| 88 |
-
output = stdout.decode().strip() or stderr.decode().strip() or "
|
| 89 |
await websocket.send_text(output)
|
| 90 |
-
except WebSocketDisconnect:
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import subprocess
|
| 3 |
import asyncio
|
|
|
|
| 4 |
import psutil
|
| 5 |
import httpx
|
| 6 |
import shutil
|
|
|
|
| 8 |
from fastapi.responses import HTMLResponse, JSONResponse
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# Environment variable থেকে পাসওয়ার্ড নেওয়া হয়েছে, ডিফল্ট "admin"
|
| 13 |
+
ACCESS_PASSWORD = os.getenv("ACCESS_PASSWORD", "admin")
|
| 14 |
+
|
| 15 |
+
# সেশন ডিরেক্টরি ট্র্যাকিং
|
| 16 |
+
current_working_dir = os.getcwd()
|
| 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 |
+
abs_path = os.path.abspath(os.path.join(current_working_dir, path))
|
|
|
|
| 28 |
files = []
|
| 29 |
+
for entry in os.scandir(abs_path):
|
| 30 |
files.append({
|
| 31 |
"name": entry.name,
|
| 32 |
"is_dir": entry.is_dir(),
|
| 33 |
+
"path": os.path.relpath(entry.path, current_working_dir),
|
| 34 |
"size": f"{entry.stat().st_size / 1024:.1f} KB" if entry.is_file() else "-"
|
| 35 |
})
|
| 36 |
return sorted(files, key=lambda x: not x['is_dir'])
|
|
|
|
| 39 |
|
| 40 |
@app.get("/api/read")
|
| 41 |
async def read_file(path: str):
|
| 42 |
+
target = os.path.join(current_working_dir, path)
|
| 43 |
+
with open(target, "r", encoding="utf-8") as f:
|
| 44 |
return {"content": f.read()}
|
| 45 |
|
| 46 |
@app.post("/api/save")
|
| 47 |
async def save_file(path: str = Body(...), content: str = Body(...)):
|
| 48 |
+
target = os.path.join(current_working_dir, path)
|
| 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(current_working_dir, path, name)
|
| 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 |
+
target = os.path.join(current_working_dir, path)
|
| 65 |
+
if os.path.isdir(target):
|
| 66 |
+
shutil.rmtree(target)
|
| 67 |
else:
|
| 68 |
+
os.remove(target)
|
| 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 |
except Exception as e:
|
| 79 |
return HTMLResponse(content=f"<div style='color:red;padding:20px;'>Error: {str(e)}</div>")
|
| 80 |
|
| 81 |
+
# --- টার্মিনাল সেশন (WebSocket) ---
|
| 82 |
@app.websocket("/ws")
|
| 83 |
async def websocket_endpoint(websocket: WebSocket):
|
| 84 |
+
global current_working_dir
|
| 85 |
await websocket.accept()
|
| 86 |
try:
|
| 87 |
auth = await websocket.receive_text()
|
|
|
|
| 90 |
await websocket.close()
|
| 91 |
return
|
| 92 |
await websocket.send_text("AUTH_SUCCESS")
|
| 93 |
+
|
| 94 |
while True:
|
| 95 |
command = await websocket.receive_text()
|
| 96 |
+
# 'cd' কমান্ড হ্যান্ডলিং
|
| 97 |
+
if command.startswith("cd "):
|
| 98 |
+
new_path = command[3:].strip()
|
| 99 |
+
try:
|
| 100 |
+
os.chdir(os.path.expanduser(new_path))
|
| 101 |
+
current_working_dir = os.getcwd()
|
| 102 |
+
await websocket.send_text(f"Directory: {current_working_dir}")
|
| 103 |
+
except Exception as e:
|
| 104 |
+
await websocket.send_text(str(e))
|
| 105 |
+
continue
|
| 106 |
+
|
| 107 |
process = await asyncio.create_subprocess_shell(
|
| 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 "Command Executed."
|
| 112 |
await websocket.send_text(output)
|
| 113 |
+
except WebSocketDisconnect:
|
| 114 |
+
pass
|