Heretic Webdl //top\\ Page
# Stream the remote file directly back to the caller # We preserve the original content‑type and disposition when possible async def generator(): async for chunk in stream_remote_file(payload.url): yield chunk
except httpx.RequestError as exc: raise HTTPException( status_code=status.HTTP_504_GATEWAY_TIMEOUT, detail=f"Error contacting remote host: exc", ) # app/main.py import time from collections import defaultdict from datetime import datetime, timedelta from typing import Dict
# --------------------------------------------------- # # Optional: expose OpenAPI UI (only in dev) # --------------------------------------------------- # if __name__ == "__main__": uvicorn.run("app.main:app", host="0.0.0.0", port=8000, log_level="info") 5.1 Pure‑Python (no Docker) # 1️⃣ Clone repo & cd into it git clone https://github.com/your‑user/heroku‑webdl.git cd heroku-webdl heretic webdl
# 5️⃣ Run the dev server uvicorn app.main:app --reload Open http://127.0.0.1:8000/docs – you’ll see the interactive Swagger UI. # Dockerfile FROM python:3.11-slim
# --------------------------------------------------------------------------- # # Core: async generator that streams the remote response to the caller. # --------------------------------------------------------------------------- # async def stream_remote_file(url: str) -> AsyncGenerator[bytes, None]: """ Yield chunks from the remote URL, respecting the global size limit. """ parsed = _validate_url(url) # Stream the remote file directly back to
def _check_rate_limit(ip: str) -> None: now = datetime.utcnow() window_start = now - timedelta(minutes=1)
# Stream in 256 KB chunks (feel free to tune) bytes_sent = 0 async for chunk in resp.aiter_bytes(chunk_size=256 * 1024): bytes_sent += len(chunk) if bytes_sent > settings.MAX_CONTENT_LENGTH: raise HTTPException( status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, detail="Remote file exceeds the allowed size limit (while streaming).", ) yield chunk ) yield chunk return parsed
return parsed



