Update main.py
Browse files
main.py
CHANGED
|
@@ -18,7 +18,7 @@ from logging.handlers import RotatingFileHandler
|
|
| 18 |
|
| 19 |
from dotenv import load_dotenv
|
| 20 |
from fastapi import FastAPI, Request
|
| 21 |
-
from fastapi.responses import JSONResponse
|
| 22 |
from pyrogram import Client
|
| 23 |
from pyrogram.enums import ParseMode
|
| 24 |
from pyrogram.errors import FloodWait
|
|
@@ -237,7 +237,8 @@ async def handle_media(chat_id: int, reply_to_id: int, file_id: str, filename: s
|
|
| 237 |
os.remove(downloaded)
|
| 238 |
|
| 239 |
entry = fm.save_file(file_data, filename)
|
| 240 |
-
link
|
|
|
|
| 241 |
file_size = len(file_data)
|
| 242 |
size_str = (f"{file_size/1024/1024:.2f} MB"
|
| 243 |
if file_size > 1024 * 1024
|
|
@@ -273,6 +274,29 @@ async def lifespan(app: FastAPI):
|
|
| 273 |
|
| 274 |
app = FastAPI(lifespan=lifespan)
|
| 275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
# ============================================================
|
| 277 |
# Webhook endpoint
|
| 278 |
# ============================================================
|
|
|
|
| 18 |
|
| 19 |
from dotenv import load_dotenv
|
| 20 |
from fastapi import FastAPI, Request
|
| 21 |
+
from fastapi.responses import FileResponse, JSONResponse
|
| 22 |
from pyrogram import Client
|
| 23 |
from pyrogram.enums import ParseMode
|
| 24 |
from pyrogram.errors import FloodWait
|
|
|
|
| 237 |
os.remove(downloaded)
|
| 238 |
|
| 239 |
entry = fm.save_file(file_data, filename)
|
| 240 |
+
# Build direct download link served by our own FastAPI endpoint
|
| 241 |
+
link = f"{SPACE_URL}/download/{entry['id']}"
|
| 242 |
file_size = len(file_data)
|
| 243 |
size_str = (f"{file_size/1024/1024:.2f} MB"
|
| 244 |
if file_size > 1024 * 1024
|
|
|
|
| 274 |
|
| 275 |
app = FastAPI(lifespan=lifespan)
|
| 276 |
|
| 277 |
+
# ============================================================
|
| 278 |
+
# File serving endpoints
|
| 279 |
+
# ============================================================
|
| 280 |
+
@app.get("/")
|
| 281 |
+
async def root():
|
| 282 |
+
return {"status": "ok", "service": "Link Generator Bot"}
|
| 283 |
+
|
| 284 |
+
|
| 285 |
+
@app.get("/download/{file_id}")
|
| 286 |
+
async def download_file(file_id: str):
|
| 287 |
+
"""Serve a stored file by its UUID."""
|
| 288 |
+
entry = fm.get_entry_by_id(file_id)
|
| 289 |
+
if not entry:
|
| 290 |
+
return JSONResponse({"error": "File not found or expired"}, status_code=404)
|
| 291 |
+
path = entry["path"]
|
| 292 |
+
if not os.path.exists(path):
|
| 293 |
+
return JSONResponse({"error": "File not found on disk"}, status_code=404)
|
| 294 |
+
return FileResponse(
|
| 295 |
+
path=path,
|
| 296 |
+
filename=entry["filename"],
|
| 297 |
+
media_type="application/octet-stream",
|
| 298 |
+
)
|
| 299 |
+
|
| 300 |
# ============================================================
|
| 301 |
# Webhook endpoint
|
| 302 |
# ============================================================
|