From 71872322363f98713cb704597e3131b34197d25d Mon Sep 17 00:00:00 2001 From: Garret Patti Date: Sat, 16 May 2026 19:41:06 -0400 Subject: [PATCH] avoid incorrect thumbnails --- backend/app/services/scanner.py | 10 +++++++++- backend/app/services/watcher.py | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/backend/app/services/scanner.py b/backend/app/services/scanner.py index b1e92a6..8a3ccab 100644 --- a/backend/app/services/scanner.py +++ b/backend/app/services/scanner.py @@ -8,6 +8,7 @@ from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select from app.models import MediaItem from app.database import SessionLocal +from app.services.thumbnails import thumbnail_path log = logging.getLogger(__name__) @@ -86,6 +87,11 @@ async def _do_scan(library_id: int, library_path: str, db: AsyncSession) -> None if rel in db_items: item = db_items[rel] + new_hash = await loop.run_in_executor(None, hash_file, file_path) + if item.file_hash != new_hash: + item.file_hash = new_hash + item.updated_at = datetime.utcnow() + thumbnail_path(item.id).unlink(missing_ok=True) if item.missing: item.missing = False item.updated_at = datetime.utcnow() @@ -93,6 +99,7 @@ async def _do_scan(library_id: int, library_path: str, db: AsyncSession) -> None file_hash = await loop.run_in_executor(None, hash_file, file_path) moved = await _find_by_hash(library_id, file_hash, db) if moved: + thumbnail_path(moved.id).unlink(missing_ok=True) moved.rel_path = rel moved.filename = file_path.name moved.missing = False @@ -131,4 +138,5 @@ async def _find_by_hash(library_id: int, file_hash: str, db: AsyncSession) -> Me MediaItem.missing == True, # noqa: E712 ) ) - return result.scalars().first() + rows = result.scalars().all() + return rows[0] if len(rows) == 1 else None diff --git a/backend/app/services/watcher.py b/backend/app/services/watcher.py index e0d6705..a68687e 100644 --- a/backend/app/services/watcher.py +++ b/backend/app/services/watcher.py @@ -9,6 +9,7 @@ from sqlalchemy import select from app.database import SessionLocal from app.models import Library, MediaItem from app.services.scanner import classify, hash_file +from app.services.thumbnails import thumbnail_path log = logging.getLogger(__name__) @@ -59,6 +60,7 @@ class LibraryEventHandler(FileSystemEventHandler): ) item = result.scalars().first() if item: + thumbnail_path(item.id).unlink(missing_ok=True) item.rel_path = dest_rel item.filename = Path(event.dest_path).name item.missing = False