From 9096824444dbd4680ceb7b29a9a268ba5efe7c9a Mon Sep 17 00:00:00 2001 From: giles Date: Tue, 3 Feb 2026 22:09:19 +0000 Subject: [PATCH] Redirect /hls/stream.m3u8 to IPFS playlist when available When the local HLS playlist doesn't exist, check for IPFS playlist CID in pending/completed run and redirect to the IPFS gateway. Co-Authored-By: Claude Opus 4.5 --- app/routers/runs.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/app/routers/runs.py b/app/routers/runs.py index 94a4fce..d462059 100644 --- a/app/routers/runs.py +++ b/app/routers/runs.py @@ -1079,6 +1079,26 @@ async def serve_hls_content( else: raise HTTPException(400, "Invalid file type") + # For playlist requests, check IPFS first (redirect to IPFS gateway) + if filename == "stream.m3u8" and not file_path.exists(): + import database + from fastapi.responses import RedirectResponse + await database.init_db() + + # Check pending run for IPFS playlist + pending = await database.get_pending_run(run_id) + if pending and pending.get("ipfs_playlist_cid"): + gateway = os.environ.get("IPFS_GATEWAY_URL", "https://ipfs.io/ipfs") + ipfs_url = f"{gateway}/{pending['ipfs_playlist_cid']}" + return RedirectResponse(url=ipfs_url, status_code=302) + + # Check completed run cache + run = await database.get_run_cache(run_id) + if run and run.get("ipfs_playlist_cid"): + gateway = os.environ.get("IPFS_GATEWAY_URL", "https://ipfs.io/ipfs") + ipfs_url = f"{gateway}/{run['ipfs_playlist_cid']}" + return RedirectResponse(url=ipfs_url, status_code=302) + # Try local file first if file_path.exists(): return FileResponse(