diff --git a/src/lib/scanner.ts b/src/lib/scanner.ts index 64b45a0..6dc7ea2 100644 --- a/src/lib/scanner.ts +++ b/src/lib/scanner.ts @@ -531,6 +531,22 @@ function detectMoves( * Tags on deleted items are intentionally left as orphans — harmless and * recoverable if the file reappears. */ +/** + * Converts an item_key (used in media_items) to the media_key format used in + * media_tags. The UI constructs media_keys as `${libraryId}:${shortId}` where + * shortId is only the terminal path segment — e.g.: + * "lib1:movie:Inception%20(2010)" → "lib1:Inception%20(2010)" + * "lib1:tv_episode:Show:S1:ep.mkv" → "lib1:ep.mkv" + * "lib1:mixed_file:dir%2Ffile.mp4" → "lib1:dir%2Ffile.mp4" + */ +function itemKeyToMediaKey(itemKey: string): string { + const firstColon = itemKey.indexOf(':') + const lastColon = itemKey.lastIndexOf(':') + const libraryId = itemKey.slice(0, firstColon) + const shortId = itemKey.slice(lastColon + 1) + return `${libraryId}:${shortId}` +} + function reconcileAndPrune( db: Database.Database, libraryId: string, @@ -541,7 +557,12 @@ function reconcileAndPrune( for (const { oldKey, newKey } of moves) { renameItem.run(newKey, oldKey) - reKeyMediaItem(oldKey, newKey) + // Convert item_keys to the media_key format actually used in media_tags + const oldMediaKey = itemKeyToMediaKey(oldKey) + const newMediaKey = itemKeyToMediaKey(newKey) + if (oldMediaKey !== newMediaKey) { + reKeyMediaItem(oldMediaKey, newMediaKey) + } console.log(`[scanner] fingerprint match: renamed "${oldKey}" → "${newKey}"`) }