JerryCoder commited on
Commit
8057028
·
verified ·
1 Parent(s): 66d865b

Update bot.py

Browse files
Files changed (1) hide show
  1. bot.py +8 -8
bot.py CHANGED
@@ -14,22 +14,18 @@ app = FastAPI()
14
  # -------------------------------
15
 
16
  def make_zip_bytes(filename: str, content: bytes) -> bytes:
17
- """Compress file bytes into a ZIP archive (in memory)."""
18
  bio = io.BytesIO()
19
  with zipfile.ZipFile(bio, "w", compression=zipfile.ZIP_DEFLATED) as zf:
20
  zf.writestr(filename, content)
21
  return bio.getvalue()
22
 
23
  def rand_bytes(n):
24
- """Generate random bytes."""
25
  return os.urandom(n)
26
 
27
  def xor_bytes(data: bytes, key: bytes) -> bytes:
28
- """Simple XOR encryption."""
29
  return bytes([b ^ key[i % len(key)] for i, b in enumerate(data)])
30
 
31
  def encode_bytes(file_bytes: bytes, filename: str):
32
- """Encrypt + wrap into Python file."""
33
  zipped = make_zip_bytes(filename, file_bytes)
34
  key = rand_bytes(16)
35
  enc = xor_bytes(zipped, key)
@@ -55,7 +51,6 @@ runpy.run_path(os.path.join(t,p))
55
 
56
  @app.post("/api/encode")
57
  async def encode_endpoint(request: Request):
58
- """Receive file URL from Cloudflare bot, encode, return result."""
59
  data = await request.json()
60
  file_url = data.get("url")
61
  filename = data.get("filename", "file.py")
@@ -75,14 +70,19 @@ async def encode_endpoint(request: Request):
75
 
76
  encoded_bytes, out_name = encode_bytes(file_bytes, filename)
77
 
78
- out_path = os.path.join(tempfile.gettempdir(), out_name)
 
 
 
79
  with open(out_path, "wb") as f:
80
  f.write(encoded_bytes)
81
 
82
- # ⚠️ NOTE: Replace this URL with an actual file hosting if you need public download
 
 
83
  return {
84
  "ok": True,
85
- "result_url": f"https://jerrycoder-kuttuxd.hf.space/tmp/{out_name}",
86
  "filename": out_name
87
  }
88
 
 
14
  # -------------------------------
15
 
16
  def make_zip_bytes(filename: str, content: bytes) -> bytes:
 
17
  bio = io.BytesIO()
18
  with zipfile.ZipFile(bio, "w", compression=zipfile.ZIP_DEFLATED) as zf:
19
  zf.writestr(filename, content)
20
  return bio.getvalue()
21
 
22
  def rand_bytes(n):
 
23
  return os.urandom(n)
24
 
25
  def xor_bytes(data: bytes, key: bytes) -> bytes:
 
26
  return bytes([b ^ key[i % len(key)] for i, b in enumerate(data)])
27
 
28
  def encode_bytes(file_bytes: bytes, filename: str):
 
29
  zipped = make_zip_bytes(filename, file_bytes)
30
  key = rand_bytes(16)
31
  enc = xor_bytes(zipped, key)
 
51
 
52
  @app.post("/api/encode")
53
  async def encode_endpoint(request: Request):
 
54
  data = await request.json()
55
  file_url = data.get("url")
56
  filename = data.get("filename", "file.py")
 
70
 
71
  encoded_bytes, out_name = encode_bytes(file_bytes, filename)
72
 
73
+ # Save to static folder for public download
74
+ static_dir = os.path.join(os.getcwd(), "static")
75
+ os.makedirs(static_dir, exist_ok=True)
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
+ # Public URL
81
+ result_url = f"https://jerrycoder-kuttuxd.hf.space/static/{out_name}"
82
+
83
  return {
84
  "ok": True,
85
+ "result_url": result_url,
86
  "filename": out_name
87
  }
88