diff --git a/frigate/data_processing/post/review_descriptions.py b/frigate/data_processing/post/review_descriptions.py index c56d4e1961..e55b51f943 100644 --- a/frigate/data_processing/post/review_descriptions.py +++ b/frigate/data_processing/post/review_descriptions.py @@ -35,6 +35,7 @@ from frigate.data_processing.types import PostProcessDataEnum from frigate.genai import GenAIClient from frigate.genai.manager import GenAIClientManager from frigate.models import Event, Recordings, ReviewSegment +from frigate.output.preview import is_camera_preview_frame from frigate.util.builtin import EventsPerSecond, InferenceSpeed from frigate.util.image import get_image_from_recording from frigate.util.recording_coverage import recording_overlap @@ -528,7 +529,7 @@ class ReviewDescriptionProcessor(PostProcessorApi): camera_files = [ entry.name for entry in os.scandir(preview_dir) - if entry.name.startswith(file_start) + if is_camera_preview_frame(entry.name, camera) ] camera_files.sort() diff --git a/frigate/test/test_camera_lifecycle.py b/frigate/test/test_camera_lifecycle.py index 6917a7bccd..5184266dce 100644 --- a/frigate/test/test_camera_lifecycle.py +++ b/frigate/test/test_camera_lifecycle.py @@ -1,10 +1,12 @@ """Regression tests for runtime camera add and delete handling.""" import asyncio +import os +import tempfile import threading import unittest from types import SimpleNamespace -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch # LicensePlatePostProcessor is imported via the maintainer rather than from # data_processing.post.license_plate, which circularly imports back through @@ -16,6 +18,7 @@ from frigate.embeddings.maintainer import ( from frigate.ptz.autotrack import PtzAutoTracker from frigate.review.maintainer import ReviewSegmentMaintainer from frigate.track.object_processing import TrackedObjectProcessor +from frigate.util import camera_cleanup def _make_processor() -> TrackedObjectProcessor: @@ -231,3 +234,33 @@ class TestCameraStateAccessors(unittest.TestCase): processor = _make_processor() self.assertEqual(processor.get_current_frame_time("deleted_cam"), 0.0) + + +class TestCameraFileCleanup(unittest.TestCase): + def test_delete_keeps_media_of_camera_sharing_name_prefix(self): + own = [ + "front-1700000000.123456-abc123.jpg", + "front-1700000000.123456-abc123-clean.webp", + "review/thumb-front-1700000000.5-abc123.webp", + ] + other = [ + "front-door-1700000000.123456-def456.jpg", + "front-door-1700000000.123456-def456-clean.webp", + "review/thumb-front-door-1700000000.5-def456.webp", + ] + + with tempfile.TemporaryDirectory() as clips: + os.makedirs(os.path.join(clips, "review")) + for name in own + other: + open(os.path.join(clips, name), "w").close() + + with patch.multiple( + camera_cleanup, CLIPS_DIR=clips, RECORD_DIR=clips, THUMB_DIR=clips + ): + camera_cleanup.cleanup_camera_files("front") + + remaining = [ + n for n in own + other if os.path.exists(os.path.join(clips, n)) + ] + + self.assertEqual(remaining, other) diff --git a/frigate/util/camera_cleanup.py b/frigate/util/camera_cleanup.py index 76a6891f7d..be0131fdec 100644 --- a/frigate/util/camera_cleanup.py +++ b/frigate/util/camera_cleanup.py @@ -1,8 +1,8 @@ """Utilities for cleaning up camera data from database and filesystem.""" -import glob import logging import os +import re import shutil from frigate.const import CLIPS_DIR, RECORD_DIR, THUMB_DIR @@ -126,33 +126,39 @@ def cleanup_camera_files( except Exception as e: logger.error("Failed to remove %s: %s", dir_path, e) - # Remove event snapshot files - for snapshot in glob.glob(os.path.join(CLIPS_DIR, f"{camera_name}-*.jpg")): - try: - os.remove(snapshot) - except Exception as e: - logger.error("Failed to remove snapshot %s: %s", snapshot, e) + # Match the whole file name so deleting "front" keeps the files of "front-door" + media_id = r"[\d.]+-[a-z0-9]{6}" + camera = re.escape(camera_name) + media_patterns = [ + ( + CLIPS_DIR, + re.compile(rf"{camera}-{media_id}(\.jpg|-clean\.webp|-clean\.png)"), + ), + ( + os.path.join(CLIPS_DIR, "review"), + re.compile(rf"thumb-{camera}-{media_id}\.webp"), + ), + ] - for snapshot in glob.glob(os.path.join(CLIPS_DIR, f"{camera_name}-*-clean.webp")): - try: - os.remove(snapshot) - except Exception as e: - logger.error("Failed to remove snapshot %s: %s", snapshot, e) + for directory, pattern in media_patterns: + if not os.path.isdir(directory): + continue - for snapshot in glob.glob(os.path.join(CLIPS_DIR, f"{camera_name}-*-clean.png")): try: - os.remove(snapshot) - except Exception as e: - logger.error("Failed to remove snapshot %s: %s", snapshot, e) + files = os.listdir(directory) + except OSError as e: + logger.error("Failed to list %s: %s", directory, e) + continue - # Remove review thumbnail files - for thumb in glob.glob( - os.path.join(CLIPS_DIR, "review", f"thumb-{camera_name}-*.webp") - ): - try: - os.remove(thumb) - except Exception as e: - logger.error("Failed to remove review thumbnail %s: %s", thumb, e) + for file in files: + if not pattern.fullmatch(file): + continue + + path = os.path.join(directory, file) + try: + os.remove(path) + except Exception as e: + logger.error("Failed to remove media file %s: %s", path, e) # Remove export files if requested if export_paths: