Spaces:
Sleeping
Sleeping
Update bot.py
Browse files
bot.py
CHANGED
|
@@ -4,7 +4,7 @@ import zipfile
|
|
| 4 |
import base64
|
| 5 |
import uuid
|
| 6 |
import tempfile
|
| 7 |
-
from fastapi import FastAPI, Request
|
| 8 |
import aiohttp
|
| 9 |
import uvicorn
|
| 10 |
|
|
@@ -46,22 +46,44 @@ runpy.run_path(os.path.join(t,p))
|
|
| 46 |
unique_name = f"{os.path.splitext(filename)[0]}_{uuid.uuid4().hex}_enc.py"
|
| 47 |
return wrapper.encode(), unique_name
|
| 48 |
|
|
|
|
|
|
|
| 49 |
# -------------------------------
|
| 50 |
-
#
|
| 51 |
# -------------------------------
|
| 52 |
|
| 53 |
@app.post("/api/encode")
|
| 54 |
async def encode_endpoint(request: Request):
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
if not file_url:
|
| 64 |
return {"ok": False, "error": "No file URL provided"}
|
|
|
|
|
|
|
| 65 |
|
| 66 |
async with aiohttp.ClientSession() as session:
|
| 67 |
async with session.get(file_url) as resp:
|
|
@@ -69,21 +91,17 @@ async def encode_endpoint(request: Request):
|
|
| 69 |
return {"ok": False, "error": f"Failed to fetch file ({resp.status})"}
|
| 70 |
file_bytes = await resp.read()
|
| 71 |
|
| 72 |
-
MAX_FILE_SIZE = 3 * 1024 * 1024 # 3MB limit
|
| 73 |
if len(file_bytes) > MAX_FILE_SIZE:
|
| 74 |
return {"ok": False, "error": "File too large (max 3MB)"}
|
| 75 |
|
| 76 |
-
# Encode the file
|
| 77 |
encoded_bytes, out_name = encode_bytes(file_bytes, filename)
|
| 78 |
|
| 79 |
-
# Save temporarily
|
| 80 |
tmp_path = os.path.join(tempfile.gettempdir(), out_name)
|
| 81 |
with open(tmp_path, "wb") as f:
|
| 82 |
f.write(encoded_bytes)
|
| 83 |
|
| 84 |
-
# -------------------------------
|
| 85 |
# Upload to AR Hosting
|
| 86 |
-
# -------------------------------
|
| 87 |
upload_url = "https://ar-hosting.pages.dev/upload"
|
| 88 |
form_data = aiohttp.FormData()
|
| 89 |
form_data.add_field("file", open(tmp_path, "rb"), filename=out_name)
|
|
@@ -94,11 +112,7 @@ async def encode_endpoint(request: Request):
|
|
| 94 |
return {"ok": False, "error": f"Failed to upload to AR Hosting ({resp.status})"}
|
| 95 |
result = await resp.json()
|
| 96 |
|
| 97 |
-
return {
|
| 98 |
-
"ok": True,
|
| 99 |
-
"result_url": result.get("url"),
|
| 100 |
-
"filename": out_name
|
| 101 |
-
}
|
| 102 |
|
| 103 |
# -------------------------------
|
| 104 |
# ๐ Run App
|
|
|
|
| 4 |
import base64
|
| 5 |
import uuid
|
| 6 |
import tempfile
|
| 7 |
+
from fastapi import FastAPI, Request, Query
|
| 8 |
import aiohttp
|
| 9 |
import uvicorn
|
| 10 |
|
|
|
|
| 46 |
unique_name = f"{os.path.splitext(filename)[0]}_{uuid.uuid4().hex}_enc.py"
|
| 47 |
return wrapper.encode(), unique_name
|
| 48 |
|
| 49 |
+
MAX_FILE_SIZE = 3 * 1024 * 1024 # 3MB limit
|
| 50 |
+
|
| 51 |
# -------------------------------
|
| 52 |
+
# POST /api/encode
|
| 53 |
# -------------------------------
|
| 54 |
|
| 55 |
@app.post("/api/encode")
|
| 56 |
async def encode_endpoint(request: Request):
|
| 57 |
+
try:
|
| 58 |
+
data = await request.json()
|
| 59 |
+
file_url = data.get("url")
|
| 60 |
+
filename = data.get("filename", "file.py")
|
| 61 |
|
| 62 |
+
return await process_file(file_url, filename)
|
| 63 |
+
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return {"ok": False, "error": f"Internal server error: {str(e)}"}
|
| 66 |
+
|
| 67 |
+
# -------------------------------
|
| 68 |
+
# GET /encode?url=...&filename=...
|
| 69 |
+
# -------------------------------
|
| 70 |
|
| 71 |
+
@app.get("/encode")
|
| 72 |
+
async def encode_get(url: str = Query(...), filename: str = Query("file.py")):
|
| 73 |
+
try:
|
| 74 |
+
return await process_file(url, filename)
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return {"ok": False, "error": f"Internal server error: {str(e)}"}
|
| 77 |
+
|
| 78 |
+
# -------------------------------
|
| 79 |
+
# File processing function
|
| 80 |
+
# -------------------------------
|
| 81 |
+
|
| 82 |
+
async def process_file(file_url: str, filename: str):
|
| 83 |
if not file_url:
|
| 84 |
return {"ok": False, "error": "No file URL provided"}
|
| 85 |
+
if not filename.endswith(".py"):
|
| 86 |
+
return {"ok": False, "error": "Only Python (.py) files are allowed"}
|
| 87 |
|
| 88 |
async with aiohttp.ClientSession() as session:
|
| 89 |
async with session.get(file_url) as resp:
|
|
|
|
| 91 |
return {"ok": False, "error": f"Failed to fetch file ({resp.status})"}
|
| 92 |
file_bytes = await resp.read()
|
| 93 |
|
|
|
|
| 94 |
if len(file_bytes) > MAX_FILE_SIZE:
|
| 95 |
return {"ok": False, "error": "File too large (max 3MB)"}
|
| 96 |
|
|
|
|
| 97 |
encoded_bytes, out_name = encode_bytes(file_bytes, filename)
|
| 98 |
|
| 99 |
+
# Save temporarily
|
| 100 |
tmp_path = os.path.join(tempfile.gettempdir(), out_name)
|
| 101 |
with open(tmp_path, "wb") as f:
|
| 102 |
f.write(encoded_bytes)
|
| 103 |
|
|
|
|
| 104 |
# Upload to AR Hosting
|
|
|
|
| 105 |
upload_url = "https://ar-hosting.pages.dev/upload"
|
| 106 |
form_data = aiohttp.FormData()
|
| 107 |
form_data.add_field("file", open(tmp_path, "rb"), filename=out_name)
|
|
|
|
| 112 |
return {"ok": False, "error": f"Failed to upload to AR Hosting ({resp.status})"}
|
| 113 |
result = await resp.json()
|
| 114 |
|
| 115 |
+
return {"ok": True, "result_url": result.get("url"), "filename": out_name}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
# -------------------------------
|
| 118 |
# ๐ Run App
|