Use query parameters for the media snapshot.jpg endpoint

This commit is contained in:
Rui Alves 2024-09-21 12:48:13 +01:00
parent 17788bb9c7
commit c33327175a
2 changed files with 19 additions and 13 deletions

View File

@ -21,3 +21,11 @@ class MediaLatestFrameQueryParams(BaseModel):
regions: Optional[int] = None regions: Optional[int] = None
quality: Optional[int] = 70 quality: Optional[int] = 70
height: Optional[int] = None height: Optional[int] = None
class MediaEventsSnapshotQueryParams(BaseModel):
download: bool = False,
timestamp: Optional[int] = None,
bbox: Optional[int] = None,
crop: Optional[int] = None,
height: Optional[int] = None,
quality: Optional[int] = 70,

View File

@ -20,7 +20,10 @@ from fastapi.responses import FileResponse, JSONResponse, StreamingResponse
from peewee import DoesNotExist, fn from peewee import DoesNotExist, fn
from tzlocal import get_localzone_name from tzlocal import get_localzone_name
from frigate.api.defs.media_query_parameters import MediaLatestFrameQueryParams from frigate.api.defs.media_query_parameters import (
MediaEventsSnapshotQueryParams,
MediaLatestFrameQueryParams,
)
from frigate.api.defs.tags import Tags from frigate.api.defs.tags import Tags
from frigate.config import FrigateConfig from frigate.config import FrigateConfig
from frigate.const import ( from frigate.const import (
@ -984,12 +987,7 @@ def event_snapshot_clean(request: Request, event_id: str, download: bool = False
def event_snapshot( def event_snapshot(
request: Request, request: Request,
event_id: str, event_id: str,
download: bool = False, params: MediaEventsSnapshotQueryParams = Depends(),
timestamp: Optional[int] = None,
bbox: Optional[int] = None,
crop: Optional[int] = None,
height: Optional[int] = None,
quality: Optional[int] = 70,
): ):
event_complete = False event_complete = False
jpg_bytes = None jpg_bytes = None
@ -1015,11 +1013,11 @@ def event_snapshot(
tracked_obj = camera_state.tracked_objects.get(event_id) tracked_obj = camera_state.tracked_objects.get(event_id)
if tracked_obj is not None: if tracked_obj is not None:
jpg_bytes = tracked_obj.get_jpg_bytes( jpg_bytes = tracked_obj.get_jpg_bytes(
timestamp=timestamp, timestamp=params.timestamp,
bounding_box=bbox, bounding_box=params.bbox,
crop=crop, crop=params.crop,
height=height, height=params.height,
quality=quality, quality=params.quality,
) )
except Exception: except Exception:
return JSONResponse( return JSONResponse(
@ -1041,7 +1039,7 @@ def event_snapshot(
"Cache-Control": "private, max-age=31536000" if event_complete else "no-store", "Cache-Control": "private, max-age=31536000" if event_complete else "no-store",
} }
if download: if params.download:
headers["Content-Disposition"] = f"attachment; filename=snapshot-{event_id}.jpg" headers["Content-Disposition"] = f"attachment; filename=snapshot-{event_id}.jpg"
return StreamingResponse( return StreamingResponse(