Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,10 @@
|
|
| 1 |
-
from fastapi import FastAPI, Request
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
| 4 |
-
from pc_agent import run_command, list_files, cleanup
|
|
|
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
|
@@ -11,7 +14,9 @@ class CommandRequest(BaseModel):
|
|
| 11 |
|
| 12 |
@app.post("/agent/run")
|
| 13 |
async def execute_command(req: CommandRequest):
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
@app.get("/agent/files")
|
| 17 |
async def get_files():
|
|
@@ -22,6 +27,19 @@ async def reset_workspace():
|
|
| 22 |
cleanup()
|
| 23 |
return {"status": "workspace cleared"}
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
@app.get("/health")
|
| 26 |
def health():
|
| 27 |
return {"status": "pc_agent_online"}
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, UploadFile, File, HTTPException
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from pydantic import BaseModel
|
| 5 |
+
from pc_agent import run_command, list_files, cleanup, ensure_workspace
|
| 6 |
+
import aiofiles
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
|
|
|
| 14 |
|
| 15 |
@app.post("/agent/run")
|
| 16 |
async def execute_command(req: CommandRequest):
|
| 17 |
+
res = run_command(req.cmd)
|
| 18 |
+
status = res.pop("code", 200)
|
| 19 |
+
return JSONResponse(status_code=status, content=res)
|
| 20 |
|
| 21 |
@app.get("/agent/files")
|
| 22 |
async def get_files():
|
|
|
|
| 27 |
cleanup()
|
| 28 |
return {"status": "workspace cleared"}
|
| 29 |
|
| 30 |
+
@app.post("/agent/upload")
|
| 31 |
+
async def upload_file(file: UploadFile = File(...)):
|
| 32 |
+
ws = ensure_workspace()
|
| 33 |
+
dest = ws / "uploads" / file.filename
|
| 34 |
+
# منع تجاوز المسار
|
| 35 |
+
if not str(dest.resolve()).startswith(str(ws.resolve())):
|
| 36 |
+
raise HTTPException(400, "Invalid file path")
|
| 37 |
+
|
| 38 |
+
async with aiofiles.open(dest, "wb") as out_file:
|
| 39 |
+
content = await file.read()
|
| 40 |
+
await out_file.write(content)
|
| 41 |
+
return {"status": "uploaded", "path": f"uploads/{file.filename}"}
|
| 42 |
+
|
| 43 |
@app.get("/health")
|
| 44 |
def health():
|
| 45 |
return {"status": "pc_agent_online"}
|