diff --git a/package.json b/package.json index c09a96b..f7d3292 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "netfelix-audio-fix", - "version": "2026.04.15.9", + "version": "2026.04.15.10", "scripts": { "dev:server": "NODE_ENV=development bun --hot server/index.tsx", "dev:client": "vite", diff --git a/server/api/scan.ts b/server/api/scan.ts index c58f152..cb89b74 100644 --- a/server/api/scan.ts +++ b/server/api/scan.ts @@ -163,11 +163,7 @@ app.get("/items", (c) => { ` SELECT id, jellyfin_id, name, type, series_name, season_number, episode_number, scan_status, original_language, orig_lang_source, container, file_size, file_path, - last_scanned_at, ingest_source, - (SELECT GROUP_CONCAT(DISTINCT LOWER(codec)) - FROM media_streams - WHERE item_id = media_items.id AND type = 'Audio' AND codec IS NOT NULL - ) AS audio_codecs + last_scanned_at, ingest_source FROM media_items ${where.sql} ORDER BY COALESCE(last_scanned_at, created_at) DESC, id DESC @@ -192,6 +188,24 @@ app.get("/items", (c) => { ingest_source: string | null; audio_codecs: string | null; }>; + + // Audio codecs per item, batched into one query for the current page. + // A per-row scalar subquery over media_streams was O(page × streams) + // and could block the event loop for minutes on large libraries. + if (rows.length > 0) { + const placeholders = rows.map(() => "?").join(","); + const codecRows = db + .prepare( + `SELECT item_id, GROUP_CONCAT(DISTINCT LOWER(codec)) AS codecs + FROM media_streams + WHERE item_id IN (${placeholders}) AND type = 'Audio' AND codec IS NOT NULL + GROUP BY item_id`, + ) + .all(...rows.map((r) => r.id)) as { item_id: number; codecs: string | null }[]; + const byItem = new Map(codecRows.map((r) => [r.item_id, r.codecs])); + for (const row of rows) row.audio_codecs = byItem.get(row.id) ?? null; + } + const total = (db.prepare(`SELECT COUNT(*) as n FROM media_items ${where.sql}`).get(...where.args) as { n: number }).n; return c.json({ rows, total, hasMore: query.offset + rows.length < total, query }); });