diff --git a/frigate/api/media_auth.py b/frigate/api/media_auth.py index 49a3e6f8f9..df29cf5bce 100644 --- a/frigate/api/media_auth.py +++ b/frigate/api/media_auth.py @@ -20,7 +20,7 @@ from urllib.parse import unquote from peewee import DoesNotExist, Field from frigate.config import FrigateConfig -from frigate.const import CLIPS_DIR, EXPORT_DIR +from frigate.const import CLIPS_DIR, EXPORT_DIR, SUB_CACHE_TAG from frigate.models import Export, User logger = logging.getLogger(__name__) @@ -137,11 +137,12 @@ def _resolve_recording( # /recordings/{date} → neutral # /recordings/{date}/{hour} → multi-camera listing # /recordings/{date}/{hour}/{cam}/... → camera + # /recordings/{date}/{hour}/{cam}@sub/... → camera (sub stream) if len(parts) <= 2: return MediaAuthResolution.LISTING_NEUTRAL, None if len(parts) == 3: return MediaAuthResolution.LISTING_MULTI_CAMERA, None - return MediaAuthResolution.CAMERA, parts[3] + return MediaAuthResolution.CAMERA, parts[3].removesuffix(SUB_CACHE_TAG) def _resolve_clip( @@ -150,6 +151,7 @@ def _resolve_clip( # /clips → multi-camera listing # /clips/thumbs/{cam}/... → camera # /clips/previews/{cam}/... → camera + # /clips/triggers/{cam}/... → camera # /clips/review/thumb-{cam}-{review_id}.webp → camera (parsed) # /clips/export/{export_id}.{ext} → camera (DB lookup) # /clips/faces/... → admin-only @@ -163,7 +165,7 @@ def _resolve_clip( second = parts[1] - if second in ("thumbs", "previews"): + if second in ("thumbs", "previews", "triggers"): if len(parts) == 2: return MediaAuthResolution.LISTING_MULTI_CAMERA, None return MediaAuthResolution.CAMERA, parts[2] diff --git a/frigate/comms/webpush.py b/frigate/comms/webpush.py index d8ccedbcaa..a46d1cae30 100644 --- a/frigate/comms/webpush.py +++ b/frigate/comms/webpush.py @@ -685,7 +685,7 @@ class WebPushClient(Communicator): title = f"{name.replace('_', ' ')} triggered on {camera_name}" message = f"{titlecase(trigger_type)} trigger fired for {camera_name} with score {score:.2f}" - image = f"clips/triggers/{camera}/{event_id}.webp" + image = f"api/events/{event_id}/thumbnail.webp" direct_url = f"/explore?event_id={event_id}" ttl = 0 diff --git a/frigate/test/test_media_auth.py b/frigate/test/test_media_auth.py index 98a84a74c9..ae6de27813 100644 --- a/frigate/test/test_media_auth.py +++ b/frigate/test/test_media_auth.py @@ -122,6 +122,11 @@ class TestResolveMediaUri(unittest.TestCase): MediaAuthResolution.CAMERA, camera="back_door", ) + self._assert( + "/recordings/2026-05-11/14/back_door@sub/00.00.mp4", + MediaAuthResolution.CAMERA, + camera="back_door", + ) def test_clip_flat_filename_resolves_camera(self): self._assert( @@ -177,6 +182,14 @@ class TestResolveMediaUri(unittest.TestCase): camera="back_door", ) + def test_clip_triggers(self): + self._assert("/clips/triggers/", MediaAuthResolution.LISTING_MULTI_CAMERA) + self._assert( + "/clips/triggers/back_door/abc.webp", + MediaAuthResolution.CAMERA, + camera="back_door", + ) + def test_clip_review_thumbs(self): # Format: /clips/review/thumb-{camera}-{review_id}.webp (frigate/review/maintainer.py). self._assert( @@ -340,6 +353,14 @@ class TestDenyResponseForMediaUri(unittest.TestCase): self.assertIsNone( self._deny("/clips/thumbs/front_door/abc.webp", "limited_user") ) + self.assertIsNone( + self._deny( + "/recordings/2026-05-11/14/front_door@sub/00.00.mp4", "limited_user" + ) + ) + self.assertIsNone( + self._deny("/clips/triggers/front_door/abc.webp", "limited_user") + ) def test_restricted_role_blocked_other_camera(self): self.assertEqual(self._deny("/clips/back_door-1.jpg", "limited_user"), 403) @@ -350,6 +371,15 @@ class TestDenyResponseForMediaUri(unittest.TestCase): self.assertEqual( self._deny("/clips/thumbs/back_door/abc.webp", "limited_user"), 403 ) + self.assertEqual( + self._deny( + "/recordings/2026-05-11/14/back_door@sub/00.00.mp4", "limited_user" + ), + 403, + ) + self.assertEqual( + self._deny("/clips/triggers/back_door/abc.webp", "limited_user"), 403 + ) def test_restricted_role_blocked_admin_only(self): self.assertEqual(self._deny("/clips/faces/train/foo.webp", "limited_user"), 403) diff --git a/frigate/test/test_media_sync.py b/frigate/test/test_media_sync.py new file mode 100644 index 0000000000..ebab0f6862 --- /dev/null +++ b/frigate/test/test_media_sync.py @@ -0,0 +1,63 @@ +import os +import tempfile +import unittest +from unittest.mock import patch + +from peewee import SqliteDatabase + +from frigate.models import Export, ExportCase +from frigate.util.media import sync_exports + + +class TestSyncExports(unittest.TestCase): + def setUp(self): + self.db = SqliteDatabase(":memory:") + self.db.bind([Export, ExportCase]) + self.db.create_tables([Export, ExportCase]) + + self.tmp = tempfile.TemporaryDirectory() + self.clips_dir = os.path.join(self.tmp.name, "clips") + self.export_dir = os.path.join(self.tmp.name, "exports") + self.thumb_dir = os.path.join(self.clips_dir, "export") + os.makedirs(self.thumb_dir) + os.makedirs(self.export_dir) + + patcher_clips = patch("frigate.util.media.CLIPS_DIR", self.clips_dir) + patcher_exports = patch("frigate.util.media.EXPORT_DIR", self.export_dir) + patcher_clips.start() + patcher_exports.start() + self.addCleanup(patcher_clips.stop) + self.addCleanup(patcher_exports.stop) + + def tearDown(self): + self.db.close() + self.tmp.cleanup() + + def _touch(self, path: str) -> str: + open(path, "wb").close() + return path + + def test_orphaned_webp_and_jpg_thumbnails_are_removed(self): + for i, ext in enumerate(("webp", "jpg")): + Export.insert( + id=f"front_{i}", + camera="front", + name=f"export {i}", + date=0, + video_path=self._touch(os.path.join(self.export_dir, f"{i}.mp4")), + thumb_path=self._touch(os.path.join(self.thumb_dir, f"{i}.{ext}")), + in_progress=False, + ).execute() + + orphan_webp = self._touch(os.path.join(self.thumb_dir, "gone.webp")) + orphan_jpg = self._touch(os.path.join(self.thumb_dir, "gone.jpg")) + + result = sync_exports() + + self.assertEqual(sorted(result.orphan_paths), sorted([orphan_jpg, orphan_webp])) + self.assertEqual(result.orphans_deleted, 2) + self.assertEqual(sorted(os.listdir(self.thumb_dir)), ["0.webp", "1.jpg"]) + + +if __name__ == "__main__": + unittest.main() diff --git a/frigate/test/test_webpush_trigger.py b/frigate/test/test_webpush_trigger.py new file mode 100644 index 0000000000..4ee0b6b652 --- /dev/null +++ b/frigate/test/test_webpush_trigger.py @@ -0,0 +1,40 @@ +import unittest +from types import SimpleNamespace +from unittest.mock import MagicMock + +from frigate.comms.webpush import WebPushClient + + +class TestTriggerNotification(unittest.TestCase): + def test_image_is_the_triggering_event_thumbnail(self): + client = WebPushClient.__new__(WebPushClient) + client.config = SimpleNamespace( + notifications=SimpleNamespace(email="admin@example.com"), + cameras={"front_door": SimpleNamespace(friendly_name=None)}, + ) + client.web_pushers = {"admin": []} + client.user_cameras = {"admin": {"front_door"}} + client.last_camera_notification_time = {} + client._within_cooldown = MagicMock(return_value=False) + client.check_registrations = MagicMock() + client.cleanup_registrations = MagicMock() + client.send_push_notification = MagicMock() + + client.send_trigger( + { + "camera": "front_door", + "event_id": "1700000000.0-abc123", + "name": "red_car", + "type": "thumbnail", + "score": 0.9, + } + ) + + self.assertEqual( + client.send_push_notification.call_args.kwargs["image"], + "api/events/1700000000.0-abc123/thumbnail.webp", + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/frigate/util/media.py b/frigate/util/media.py index e87b8ec2dc..68d207229b 100644 --- a/frigate/util/media.py +++ b/frigate/util/media.py @@ -639,7 +639,7 @@ def sync_exports(dry_run: bool = False, force: bool = False) -> SyncResult: """Sync export files - delete files not referenced by any export record. Export videos are stored at: EXPORT_DIR/*.mp4 - Export thumbnails are stored at: CLIPS_DIR/export/*.jpg + Export thumbnails are stored at: CLIPS_DIR/export/*.webp (legacy *.jpg) The paths are stored in Export.video_path and Export.thumb_path """ result = SyncResult(media_type="exports") @@ -667,7 +667,7 @@ def sync_exports(dry_run: bool = False, force: bool = False) -> SyncResult: thumb_files: list[str] = [] if os.path.isdir(export_thumb_dir): for file in os.listdir(export_thumb_dir): - if file.endswith(".jpg"): + if file.endswith((".webp", ".jpg")): file_path = os.path.join(export_thumb_dir, file) thumb_files.append(file_path)