diff --git a/src/app/api/nfo-refresh/route.ts b/src/app/api/nfo-refresh/route.ts index 576f4a1..dba57f3 100644 --- a/src/app/api/nfo-refresh/route.ts +++ b/src/app/api/nfo-refresh/route.ts @@ -120,7 +120,46 @@ export async function POST(request: NextRequest) { status: nfo.status ?? null, }), }) - return NextResponse.json({ updated: true, title: nfo.title, year: nfo.year }) + + // Optionally also refresh every episode NFO in this series + let episodesUpdated = 0 + const includeEpisodes = searchParams.get('includeEpisodes') === 'true' + if (includeEpisodes) { + type EpRow = { item_key: string; file_path: string | null; metadata: string | null } + const episodeRows = db + .prepare(`SELECT item_key, file_path, metadata FROM media_items WHERE item_type = 'tv_episode' AND item_key LIKE ?`) + .all(`${libraryId}:tv_episode:${encodedDirName}:%`) as EpRow[] + + const updateEp = db.prepare(` + UPDATE media_items SET title = @title, plot = @plot, metadata = @metadata WHERE item_key = @item_key + `) + + db.transaction(() => { + for (const ep of episodeRows) { + if (!ep.file_path) continue + const epDir = path.join(libraryRoot, path.dirname(ep.file_path)) + const baseName = path.basename(ep.file_path, path.extname(ep.file_path)) + const epNfo = parseEpisodeNfo(path.join(epDir, `${baseName}.nfo`)) + if (!epNfo) continue + const epMeta = ep.metadata ? JSON.parse(ep.metadata) : {} + updateEp.run({ + item_key: ep.item_key, + title: epNfo.title ?? null, + plot: epNfo.plot ?? null, + metadata: JSON.stringify({ + ...epMeta, + episodeNumber: epNfo.episode ?? epMeta.episodeNumber ?? null, + seasonNumber: epNfo.season ?? epMeta.seasonNumber ?? null, + aired: epNfo.aired ?? null, + rating: epNfo.rating ?? null, + }), + }) + episodesUpdated++ + } + })() + } + + return NextResponse.json({ updated: true, title: nfo.title, year: nfo.year, episodesUpdated }) } if (itemType === 'tv_episode') { diff --git a/src/app/library/[id]/page.tsx b/src/app/library/[id]/page.tsx index 3ae6ffa..3ba1d1c 100644 --- a/src/app/library/[id]/page.tsx +++ b/src/app/library/[id]/page.tsx @@ -30,23 +30,30 @@ export default async function LibraryPage({ params, searchParams }: Props) { return (