Camera popups: live MJPEG preview via /api/cameras/{id}/stream passthrough, snapshot fallback, taller thumb
All checks were successful
build-and-deploy / build (push) Successful in 1m52s
All checks were successful
build-and-deploy / build (push) Successful in 1m52s
This commit is contained in:
parent
1f0b831835
commit
250dbbb5df
2 changed files with 67 additions and 7 deletions
57
app/main.py
57
app/main.py
|
|
@ -769,6 +769,63 @@ async def camera_snapshot(camera_id: UUID):
|
|||
return Response(content=data, media_type="image/jpeg")
|
||||
|
||||
|
||||
@app.get("/api/cameras/{camera_id}/stream")
|
||||
async def camera_stream(camera_id: UUID):
|
||||
"""Live MJPEG passthrough for one camera.
|
||||
|
||||
Browsers render multipart/x-mixed-replace responses natively inside an
|
||||
<img> tag, so proxying the camera's own MJPEG stream through here gives a
|
||||
true live preview in the map popup (no player, no JS). Single-frame JPEG
|
||||
endpoints also work — they render as a static image.
|
||||
"""
|
||||
from camera_models import cameras as cam_table
|
||||
from fastapi.responses import StreamingResponse
|
||||
import httpx
|
||||
|
||||
async with async_session() as session:
|
||||
url = (await session.execute(
|
||||
select(cam_table.c.snapshot_url).where(cam_table.c.id == camera_id)
|
||||
)).scalar_one_or_none()
|
||||
if not url:
|
||||
raise HTTPException(404, "Camera or snapshot not found")
|
||||
if not str(url).lower().startswith(("http://", "https://")):
|
||||
raise HTTPException(400, "URL is not streamable over HTTP")
|
||||
|
||||
# connect timeout short so dead cams fail fast; read timeout None because
|
||||
# an MJPEG stream legitimately idles between frames.
|
||||
client = httpx.AsyncClient(
|
||||
timeout=httpx.Timeout(5.0, read=None), follow_redirects=True,
|
||||
headers={"User-Agent": "osint-dashboard-camera-view/1.0"},
|
||||
)
|
||||
try:
|
||||
req = client.build_request("GET", url)
|
||||
resp = await client.send(req, stream=True)
|
||||
if resp.status_code != 200 or len(resp.headers.get("content-type", "")) == 0:
|
||||
await resp.aclose()
|
||||
await client.aclose()
|
||||
raise HTTPException(502, "Stream unavailable")
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception:
|
||||
await client.aclose()
|
||||
raise HTTPException(502, "Stream unavailable")
|
||||
|
||||
async def gen():
|
||||
try:
|
||||
async for chunk in resp.aiter_bytes():
|
||||
yield chunk
|
||||
finally:
|
||||
await resp.aclose()
|
||||
await client.aclose()
|
||||
|
||||
ctype = resp.headers.get("content-type", "")
|
||||
media = ctype if "multipart" in ctype.lower() else (
|
||||
ctype if "image/" in ctype.lower()
|
||||
else "multipart/x-mixed-replace; boundary=frame"
|
||||
)
|
||||
return StreamingResponse(gen(), media_type=media)
|
||||
|
||||
|
||||
# ── News pipeline (scraper + summarizer) ──────────────────────────────────
|
||||
# Backing data for the frontend news panel. Written by the vendored
|
||||
# news-scraper (hourly Scrapy crawl) and news-summarizer (hourly Gemini
|
||||
|
|
|
|||
Loading…
Reference in a new issue