JerryCoder commited on
Commit
36765d6
·
verified ·
1 Parent(s): 4820cbf

Update bot.py

Browse files
Files changed (1) hide show
  1. bot.py +49 -51
bot.py CHANGED
@@ -4,7 +4,7 @@ import zipfile
4
  import base64
5
  import uuid
6
  import tempfile
7
- from fastapi import FastAPI, Request, Query
8
  import aiohttp
9
  import uvicorn
10
 
@@ -49,72 +49,72 @@ runpy.run_path(os.path.join(t,p))
49
  MAX_FILE_SIZE = 3 * 1024 * 1024 # 3 MB limit
50
 
51
  # -------------------------------
52
- # POST /api/encode
53
  # -------------------------------
54
 
55
  @app.post("/api/encode")
56
- async def encode_post(request: Request):
 
 
 
 
57
  try:
58
- data = await request.json()
59
- file_url = data.get("url")
60
- filename = data.get("filename", "file.py")
61
- return await process_file(file_url, filename)
62
- except Exception as e:
63
- return {"ok": False, "error": f"Internal server error: {str(e)}"}
64
 
65
- # -------------------------------
66
- # GET /encode?url=...&filename=...
67
- # -------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
- @app.get("/encode")
70
- async def encode_get(url: str = Query(...), filename: str = Query("file.py")):
71
- try:
72
- return await process_file(url, filename)
73
  except Exception as e:
74
  return {"ok": False, "error": f"Internal server error: {str(e)}"}
75
 
76
  # -------------------------------
77
- # File processing function
78
  # -------------------------------
79
 
80
- async def process_file(file_url: str, filename: str):
81
- if not file_url:
82
- return {"ok": False, "error": "No file URL provided"}
83
- if not filename.endswith(".py"):
84
- return {"ok": False, "error": "Only Python (.py) files are allowed"}
85
-
86
- # -------------------------------
87
- # Fetch the file
88
- # -------------------------------
89
  try:
90
  async with aiohttp.ClientSession() as session:
91
- async with session.get(file_url) as resp:
92
  if resp.status != 200:
93
  return {"ok": False, "error": f"Failed to fetch file ({resp.status})"}
94
  file_bytes = await resp.read()
95
- except Exception as e:
96
- return {"ok": False, "error": f"Failed to fetch file: {str(e)}"}
97
-
98
- if len(file_bytes) > MAX_FILE_SIZE:
99
- return {"ok": False, "error": "File too large (max 3MB)"}
100
-
101
- # -------------------------------
102
- # Encode the file
103
- # -------------------------------
104
- encoded_bytes, out_name = encode_bytes(file_bytes, filename)
105
 
106
- # Save temporarily
107
- tmp_path = os.path.join(tempfile.gettempdir(), out_name)
108
- with open(tmp_path, "wb") as f:
109
- f.write(encoded_bytes)
110
 
111
- # -------------------------------
112
- # Upload to AR Hosting
113
- # -------------------------------
114
- try:
115
  upload_url = "https://ar-hosting.pages.dev/upload"
116
  form_data = aiohttp.FormData()
117
- f = open(tmp_path, "rb") # Keep file open for async upload
118
  form_data.add_field("file", f, filename=out_name, content_type="application/octet-stream")
119
 
120
  async with aiohttp.ClientSession() as session:
@@ -124,16 +124,14 @@ async def process_file(file_url: str, filename: str):
124
  except Exception:
125
  f.close()
126
  return {"ok": False, "error": f"AR Hosting returned invalid JSON (status {resp.status})"}
127
-
128
  if resp.status != 200 or "url" not in result:
129
  f.close()
130
  return {"ok": False, "error": f"AR Hosting upload failed: {result}"}
 
 
131
 
132
- f.close() # Close after upload
133
  except Exception as e:
134
- return {"ok": False, "error": f"AR Hosting request failed: {str(e)}"}
135
-
136
- return {"ok": True, "result_url": result.get("url"), "filename": out_name}
137
 
138
  # -------------------------------
139
  # 🏁 Run App
 
4
  import base64
5
  import uuid
6
  import tempfile
7
+ from fastapi import FastAPI, Request, UploadFile, File
8
  import aiohttp
9
  import uvicorn
10
 
 
49
  MAX_FILE_SIZE = 3 * 1024 * 1024 # 3 MB limit
50
 
51
  # -------------------------------
52
+ # POST /api/encode - receive raw file bytes
53
  # -------------------------------
54
 
55
  @app.post("/api/encode")
56
+ async def encode_post(file: UploadFile = File(...)):
57
+ filename = file.filename
58
+ if not filename.endswith(".py"):
59
+ return {"ok": False, "error": "Only Python (.py) files are allowed"}
60
+
61
  try:
62
+ file_bytes = await file.read()
63
+ if len(file_bytes) > MAX_FILE_SIZE:
64
+ return {"ok": False, "error": "File too large (max 3MB)"}
 
 
 
65
 
66
+ encoded_bytes, out_name = encode_bytes(file_bytes, filename)
67
+
68
+ tmp_path = os.path.join(tempfile.gettempdir(), out_name)
69
+ with open(tmp_path, "wb") as f:
70
+ f.write(encoded_bytes)
71
+
72
+ # Upload to AR Hosting
73
+ upload_url = "https://ar-hosting.pages.dev/upload"
74
+ form_data = aiohttp.FormData()
75
+ f = open(tmp_path, "rb")
76
+ form_data.add_field("file", f, filename=out_name, content_type="application/octet-stream")
77
+
78
+ async with aiohttp.ClientSession() as session:
79
+ async with session.post(upload_url, data=form_data) as resp:
80
+ try:
81
+ result = await resp.json()
82
+ except Exception:
83
+ f.close()
84
+ return {"ok": False, "error": f"AR Hosting returned invalid JSON (status {resp.status})"}
85
+
86
+ if resp.status != 200 or "url" not in result:
87
+ f.close()
88
+ return {"ok": False, "error": f"AR Hosting upload failed: {result}"}
89
+ f.close()
90
+ return {"ok": True, "result_url": result.get("url"), "filename": out_name}
91
 
 
 
 
 
92
  except Exception as e:
93
  return {"ok": False, "error": f"Internal server error: {str(e)}"}
94
 
95
  # -------------------------------
96
+ # GET /encode?url=...&filename=... (optional)
97
  # -------------------------------
98
 
99
+ @app.get("/encode")
100
+ async def encode_get(url: str, filename: str = "file.py"):
101
+ # Keep GET for backward compatibility if URL is public
 
 
 
 
 
 
102
  try:
103
  async with aiohttp.ClientSession() as session:
104
+ async with session.get(url) as resp:
105
  if resp.status != 200:
106
  return {"ok": False, "error": f"Failed to fetch file ({resp.status})"}
107
  file_bytes = await resp.read()
108
+ encoded_bytes, out_name = encode_bytes(file_bytes, filename)
 
 
 
 
 
 
 
 
 
109
 
110
+ tmp_path = os.path.join(tempfile.gettempdir(), out_name)
111
+ with open(tmp_path, "wb") as f:
112
+ f.write(encoded_bytes)
 
113
 
114
+ # Upload to AR Hosting
 
 
 
115
  upload_url = "https://ar-hosting.pages.dev/upload"
116
  form_data = aiohttp.FormData()
117
+ f = open(tmp_path, "rb")
118
  form_data.add_field("file", f, filename=out_name, content_type="application/octet-stream")
119
 
120
  async with aiohttp.ClientSession() as session:
 
124
  except Exception:
125
  f.close()
126
  return {"ok": False, "error": f"AR Hosting returned invalid JSON (status {resp.status})"}
 
127
  if resp.status != 200 or "url" not in result:
128
  f.close()
129
  return {"ok": False, "error": f"AR Hosting upload failed: {result}"}
130
+ f.close()
131
+ return {"ok": True, "result_url": result.get("url"), "filename": out_name}
132
 
 
133
  except Exception as e:
134
+ return {"ok": False, "error": f"Internal server error: {str(e)}"}
 
 
135
 
136
  # -------------------------------
137
  # 🏁 Run App