fix(gallery): 图库清单回填缩略图(修复「图片已丢失」)

This commit is contained in:
qiqi committed 2026-11-17 08:29:18 +08:00
1 parent 99f676f460
commit 88428ca5ff
1 file changed
+42
+42
View File
@@ -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)