Spaces:
Sleeping
Sleeping
Update bot.py
Browse files
bot.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import io
|
| 3 |
import zipfile
|
| 4 |
import base64
|
|
|
|
| 5 |
import tempfile
|
| 6 |
from fastapi import FastAPI, Request
|
| 7 |
import aiohttp
|
|
@@ -42,8 +43,8 @@ zipfile.ZipFile(io.BytesIO(O)).extractall(t)
|
|
| 42 |
p=[f for f in os.listdir(t) if f.endswith('.py')][0]
|
| 43 |
runpy.run_path(os.path.join(t,p))
|
| 44 |
"""
|
| 45 |
-
|
| 46 |
-
return wrapper.encode(),
|
| 47 |
|
| 48 |
# -------------------------------
|
| 49 |
# 🌐 FastAPI Endpoint
|
|
@@ -55,7 +56,7 @@ async def encode_endpoint(request: Request):
|
|
| 55 |
file_url = data.get("url")
|
| 56 |
filename = data.get("filename", "file.py")
|
| 57 |
|
| 58 |
-
#
|
| 59 |
if not filename.endswith(".py"):
|
| 60 |
return {"ok": False, "error": "Only Python (.py) files are allowed"}
|
| 61 |
|
|
@@ -68,21 +69,34 @@ async def encode_endpoint(request: Request):
|
|
| 68 |
return {"ok": False, "error": f"Failed to fetch file ({resp.status})"}
|
| 69 |
file_bytes = await resp.read()
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
encoded_bytes, out_name = encode_bytes(file_bytes, filename)
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
out_path = os.path.join(static_dir, out_name)
|
| 77 |
-
with open(out_path, "wb") as f:
|
| 78 |
f.write(encoded_bytes)
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
return {
|
| 84 |
"ok": True,
|
| 85 |
-
"result_url":
|
| 86 |
"filename": out_name
|
| 87 |
}
|
| 88 |
|
|
|
|
| 2 |
import io
|
| 3 |
import zipfile
|
| 4 |
import base64
|
| 5 |
+
import uuid
|
| 6 |
import tempfile
|
| 7 |
from fastapi import FastAPI, Request
|
| 8 |
import aiohttp
|
|
|
|
| 43 |
p=[f for f in os.listdir(t) if f.endswith('.py')][0]
|
| 44 |
runpy.run_path(os.path.join(t,p))
|
| 45 |
"""
|
| 46 |
+
unique_name = f"{os.path.splitext(filename)[0]}_{uuid.uuid4().hex}_enc.py"
|
| 47 |
+
return wrapper.encode(), unique_name
|
| 48 |
|
| 49 |
# -------------------------------
|
| 50 |
# 🌐 FastAPI Endpoint
|
|
|
|
| 56 |
file_url = data.get("url")
|
| 57 |
filename = data.get("filename", "file.py")
|
| 58 |
|
| 59 |
+
# Only allow Python files
|
| 60 |
if not filename.endswith(".py"):
|
| 61 |
return {"ok": False, "error": "Only Python (.py) files are allowed"}
|
| 62 |
|
|
|
|
| 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 before upload
|
| 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)
|
| 90 |
+
|
| 91 |
+
async with aiohttp.ClientSession() as session:
|
| 92 |
+
async with session.post(upload_url, data=form_data) as resp:
|
| 93 |
+
if resp.status != 200:
|
| 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 |
|