# 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
# --------------------------------------------------------------------------- # # Helper: verify that the URL is allowed (whitelist, scheme, size limit, etc.) # --------------------------------------------------------------------------- # def _validate_url(url: str) -> urllib.parse.ParseResult: try: parsed = urllib.parse.urlparse(url) except Exception as exc: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=f"Invalid URL: exc", ) heretic webdl
# 2️⃣ Create a virtualenv python3 -m venv venv source venv/bin/activate # Stream in 256 KB chunks (feel free
# 3️⃣ Install deps pip install -r requirements.txt detail=f"Invalid URL: exc"
# Guess a filename from the URL if the remote server doesn't set one filename = payload.url.path.split("/")[-1] or "downloaded_file"
# --------------------------------------------------- # # Health endpoint (useful for Heroku's dyno monitoring) # --------------------------------------------------- # @app.get("/healthz", response_class=JSONResponse) async def health(): return "status": "ok", "timestamp": time.time()
# 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