diff --git a/packages/convex/convex/actions.ts b/packages/convex/convex/actions.ts index 41feab7..21e01ab 100644 --- a/packages/convex/convex/actions.ts +++ b/packages/convex/convex/actions.ts @@ -68,6 +68,21 @@ async function coverPathIfExists(path: string): Promise { .catch(() => null) } +async function shouldReplaceCover(dbCover: string | null) { + return dbCover === null || (await coverPathIfExists(dbCover)) === null +} + +function isCodecPath(filePath: string, codec: string) { + return filePath.startsWith(`${DATA_DIR}/${codec}/`) +} + +function releaseDate(common: music.ICommonTagsResult) { + if (common.date) { + return common.date + } + return common.year ? `01-01-${common.year}` : "" +} + async function syncArtistCover( ctx: ActionCtx, codec: string, @@ -77,13 +92,13 @@ async function syncArtistCover( ) { const onDisk = await coverPathIfExists(diskPath) if (onDisk !== null) { - if (dbCover !== onDisk) { + if (await shouldReplaceCover(dbCover)) { await ctx.runMutation(internal.library.updateArtistCover, { id, cover: onDisk, }) } - } else if (dbCover !== null && dbCover.includes(codec)) { + } else if (dbCover !== null && isCodecPath(dbCover, codec)) { await ctx.runMutation(internal.library.updateArtistCover, { id, cover: null, @@ -100,13 +115,13 @@ async function syncAlbumCover( ) { const onDisk = await coverPathIfExists(diskPath) if (onDisk !== null) { - if (dbCover !== onDisk) { + if (await shouldReplaceCover(dbCover)) { await ctx.runMutation(internal.library.updateAlbumCover, { id, cover: onDisk, }) } - } else if (dbCover !== null && dbCover.includes(codec)) { + } else if (dbCover !== null && isCodecPath(dbCover, codec)) { await ctx.runMutation(internal.library.updateAlbumCover, { id, cover: null, @@ -402,9 +417,8 @@ export const scheduledUpdateLibrary = internalAction({ name: metadata.common.album || potentialAlbum, folder: potentialAlbum, cover, - year: metadata.common.year || 2026, - date: - metadata.common.date || `01-01-${metadata.common.year || 2026}`, + year: metadata.common.year || 0, + date: releaseDate(metadata.common), artist: dbArtist._id, }) } catch {} @@ -494,11 +508,14 @@ export const scheduledUpdateLibrary = internalAction({ ) if (existingSong) { + // A tracked path that still exists is a different file with the + // same title; replacing it would flip-flop between scans. + const trackedPath = existingSong.files[codec] if ( - !(codec in existingSong.files) || - existingSong.files[codec] !== path + trackedPath === undefined || + !fsSync.existsSync(trackedPath) ) { - const update = { [codec]: path, ...existingSong.files } + const update = { ...existingSong.files, [codec]: path } updateSongs.push({ id: existingSong._id, files: update, @@ -550,10 +567,8 @@ export const scheduledUpdateLibrary = internalAction({ ? null : metadata.common.disk.no, track: metadata.common.track.no || 0, - year: metadata.common.year || 2026, - date: - metadata.common.date || - `01-01-${metadata.common.year || 2026}`, + year: metadata.common.year || 0, + date: releaseDate(metadata.common), genres: metadata.common.genre || [], }) }