Fix camera name prefix matching (#24992)

* only remove the deleted camera's media files

Camera deletion globbed snapshot, clean copy, and review thumbnail files by "{camera}-*", and camera names can contain "-", so deleting "front" also removed every one of those files for "front-door". Files are now matched on the full name, the camera followed by an event or review id.

* only use the camera's own preview frames for review descriptions

get_cache_frames matched cached preview frames by "preview_{camera}-", so when "front" had no frame after the review ended, the first "front-door" frame became the trailing frame and went into front's GenAI prompt. It now uses is_camera_preview_frame like the preview recorder and exports.

* keep cleaning up when a media directory can't be listed
This commit is contained in:
Josh Hawkins
2026-11-16 12:04:55 -06:00
committed by GitHub
parent 128729f5ac
commit f22278bcfa
3 changed files with 66 additions and 26 deletions
@@ -35,6 +35,7 @@ from frigate.data_processing.types import PostProcessDataEnum
from frigate.genai import GenAIClient from frigate.genai import GenAIClient
from frigate.genai.manager import GenAIClientManager from frigate.genai.manager import GenAIClientManager
from frigate.models import Event, Recordings, ReviewSegment 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.builtin import EventsPerSecond, InferenceSpeed
from frigate.util.image import get_image_from_recording from frigate.util.image import get_image_from_recording
from frigate.util.recording_coverage import recording_overlap from frigate.util.recording_coverage import recording_overlap
@@ -528,7 +529,7 @@ class ReviewDescriptionProcessor(PostProcessorApi):
camera_files = [ camera_files = [
entry.name entry.name
for entry in os.scandir(preview_dir) for entry in os.scandir(preview_dir)
if entry.name.startswith(file_start) if is_camera_preview_frame(entry.name, camera)
] ]
camera_files.sort() camera_files.sort()
+34 -1
View File
@@ -1,10 +1,12 @@
"""Regression tests for runtime camera add and delete handling.""" """Regression tests for runtime camera add and delete handling."""
import asyncio import asyncio
import os
import tempfile
import threading import threading
import unittest import unittest
from types import SimpleNamespace from types import SimpleNamespace
from unittest.mock import MagicMock from unittest.mock import MagicMock, patch
# LicensePlatePostProcessor is imported via the maintainer rather than from # LicensePlatePostProcessor is imported via the maintainer rather than from
# data_processing.post.license_plate, which circularly imports back through # 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.ptz.autotrack import PtzAutoTracker
from frigate.review.maintainer import ReviewSegmentMaintainer from frigate.review.maintainer import ReviewSegmentMaintainer
from frigate.track.object_processing import TrackedObjectProcessor from frigate.track.object_processing import TrackedObjectProcessor
from frigate.util import camera_cleanup
def _make_processor() -> TrackedObjectProcessor: def _make_processor() -> TrackedObjectProcessor:
@@ -231,3 +234,33 @@ class TestCameraStateAccessors(unittest.TestCase):
processor = _make_processor() processor = _make_processor()
self.assertEqual(processor.get_current_frame_time("deleted_cam"), 0.0) 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)
+28 -22
View File
@@ -1,8 +1,8 @@
"""Utilities for cleaning up camera data from database and filesystem.""" """Utilities for cleaning up camera data from database and filesystem."""
import glob
import logging import logging
import os import os
import re
import shutil import shutil
from frigate.const import CLIPS_DIR, RECORD_DIR, THUMB_DIR from frigate.const import CLIPS_DIR, RECORD_DIR, THUMB_DIR
@@ -126,33 +126,39 @@ def cleanup_camera_files(
except Exception as e: except Exception as e:
logger.error("Failed to remove %s: %s", dir_path, e) logger.error("Failed to remove %s: %s", dir_path, e)
# Remove event snapshot files # Match the whole file name so deleting "front" keeps the files of "front-door"
for snapshot in glob.glob(os.path.join(CLIPS_DIR, f"{camera_name}-*.jpg")): media_id = r"[\d.]+-[a-z0-9]{6}"
try: camera = re.escape(camera_name)
os.remove(snapshot) media_patterns = [
except Exception as e: (
logger.error("Failed to remove snapshot %s: %s", snapshot, e) 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")): for directory, pattern in media_patterns:
try: if not os.path.isdir(directory):
os.remove(snapshot) continue
except Exception as e:
logger.error("Failed to remove snapshot %s: %s", snapshot, e)
for snapshot in glob.glob(os.path.join(CLIPS_DIR, f"{camera_name}-*-clean.png")):
try: try:
os.remove(snapshot) files = os.listdir(directory)
except Exception as e: except OSError as e:
logger.error("Failed to remove snapshot %s: %s", snapshot, e) logger.error("Failed to list %s: %s", directory, e)
continue
# Remove review thumbnail files for file in files:
for thumb in glob.glob( if not pattern.fullmatch(file):
os.path.join(CLIPS_DIR, "review", f"thumb-{camera_name}-*.webp") continue
):
path = os.path.join(directory, file)
try: try:
os.remove(thumb) os.remove(path)
except Exception as e: except Exception as e:
logger.error("Failed to remove review thumbnail %s: %s", thumb, e) logger.error("Failed to remove media file %s: %s", path, e)
# Remove export files if requested # Remove export files if requested
if export_paths: if export_paths: