From 88428ca5ffa9f53b90134434feaa8f3a6c880561 Mon Sep 17 00:00:00 2001 From: qiqi Date: Tue, 17 Nov 2026 08:29:18 +0800 Subject: [PATCH] =?UTF-8?q?fix(gallery):=20=E5=9B=BE=E5=BA=93=E6=B8=85?= =?UTF-8?q?=E5=8D=95=E5=9B=9E=E5=A1=AB=E7=BC=A9=E7=95=A5=E5=9B=BE=EF=BC=88?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E3=80=8C=E5=9B=BE=E7=89=87=E5=B7=B2=E4=B8=A2?= =?UTF-8?q?=E5=A4=B1=E3=80=8D=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/storage-file.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/storage-file.js b/src/storage-file.js index 9c54ce8..f48be36 100644 --- a/src/storage-file.js +++ b/src/storage-file.js @@ -306,6 +306,34 @@ } catch (e) { return 0 } } + // 一次性:给图库清单里缺 data 的条目回填缩略图(图库渲染读 f.data || f.url) + async function backfillGalleryThumbs() { + try { + const marker = await join(dir, 'gallery-thumbs.flag') + if (await fs().exists(marker)) return 0 + const raw = mem['iwb_web_outputs'] + if (!raw) return 0 + const items = JSON.parse(raw) + if (!Array.isArray(items)) return 0 + let n = 0 + for (const it of items) { + if (!it || it.data) continue + const a = assets[it.idb] + if (!a || !a.f) continue + try { + const u8 = await fs().readFile(await join(dir, a.f)) + if (!u8 || u8.length > 96 * 1024) continue + it.data = bytesToDataUrl(a.m || 'image/jpeg', u8) + it.thumbHash = String(a.f).split('/').pop().replace(/\.[^.]+$/, '') + n++ + } catch (e) { /* 单张失败忽略 */ } + } + if (n) { mem['iwb_web_outputs'] = JSON.stringify(items); await flush() } + await fs().writeTextFile(marker, String(n)) + return n + } catch (e) { return 0 } + } + async function loadInto() { // data.json 不存在 ⇒ 把当前 WebView 里的数据迁过去(只做一次) const target = await join(dir, DATA_FILE) @@ -470,6 +498,8 @@ await flush() // 老版本图片在 IndexedDB 里 ⇒ 搬到 images/(带标记,只跑一次) try { await migrateBlobs() } catch (e) { /* 迁移失败不阻塞启动 */ } + // 图库清单补齐缩略图(一次性) + try { await backfillGalleryThumbs() } catch (e) { /* 忽略 */ } // 自愈:图库清单丢了但 assets 还有 out_ 键 ⇒ 按键重建(名称/时间/idb 可推,label/远程 url 无法恢复) try { if (mem['iwb_web_outputs'] === undefined) { @@ -490,6 +520,18 @@ } }) items.sort((a, b) => a.mtime - b.mtime) + // 关键:把缩略图字节回填到清单的 data 字段(图库渲染读 f.data || f.url)。 + // 只回填小于 96KB 的缩略图,避免清单膨胀。 + for (const it of items) { + const a = assets[it.idb] + if (!a || !a.f) continue + try { + const u8 = await fs().readFile(await join(dir, a.f)) + if (!u8 || u8.length > 96 * 1024) continue + it.data = bytesToDataUrl(a.m || 'image/jpeg', u8) + it.thumbHash = String(a.f).split('/').pop().replace(/\.[^.]+$/, '') + } catch (e) { /* 单张失败不影响其它 */ } + } mem['iwb_web_outputs'] = JSON.stringify(items) await flush() await blog('gallery:rebuilt ' + items.length)