from .downloader import stream_remote_file from .config import settings
if parsed.scheme not in "http", "https": raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Only http and https URLs are supported.", ) heretic webdl
app = FastAPI( title="Heroku Web‑DL", description="A tiny HTTP API that streams any publicly reachable file back to the caller.", version="0.1.0", ) ) app = FastAPI( title="Heroku Web‑DL"
# ---- logging ----------------------------------------------------------------- LOG_LEVEL = os.getenv("LOG_LEVEL", "info") detail=f"Error contacting remote host: exc"
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
import httpx from fastapi import HTTPException, status from .config import settings
# 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