perf(image): 图库清单去 base64(367KB→1.6KB,按 thumbHash 读文件)+ 画布显示缩略图 768px 落盘
Verify (Gitea) / gate (build · unit · facts) (push) Successful in 1m36s

This commit is contained in:
qiqi committed 2026-11-17 15:36:20 +08:00
1 parent e9a6402373
commit 3bbf1ea85d
3 files changed
+140 -5

No files matched your search

+86 -3
View File
@@ -324,8 +324,11 @@
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(/\.[^.]+$/, '')
const hash = String(a.f).split('/').pop().replace(/\.[^.]+$/, '')
const thumbPath = await join(dir, IMG_DIR, 'thumbs', hash + '.jpg')
try { if (!(await fs().exists(thumbPath))) await fs().writeFile(thumbPath, u8) } catch (e) { /* 写失败不阻塞 */ }
it.thumbHash = hash
it.data = ''
n++
} catch (e) { /* 单张失败忽略 */ }
}
@@ -642,6 +645,8 @@
try { await migrateBlobs() } catch (e) { /* 迁移失败不阻塞启动 */ }
// 图库清单补齐缩略图(一次性)
try { await backfillGalleryThumbs() } catch (e) { /* 忽略 */ }
// 图库清单去 base64(一次性):缩略图已在 images/thumbs/,清单只留 thumbHash
try { await stripInlineGalleryThumbs() } catch (e) { /* 忽略 */ }
// trash/ 轮转:gcImages 只是移进来,超过 30 天在这里真正删除
try { await rotateTrash(30) } catch (e) { /* 忽略 */ }
// 自愈:图库清单丢了但 assets 还有 out_ 键 ⇒ 按键重建(名称/时间/idb 可推,label/远程 url 无法恢复)
@@ -723,6 +728,84 @@
} catch (e) { return 0 }
}
// 图库缩略图:清单里只存 thumbHash,渲染时按需从 images/thumbs/ 读(避免 base64 撑爆 index.json)
let thumbMapCache = null
async function thumbFileMap() {
if (thumbMapCache) return thumbMapCache
const map = {}
try {
const abs = await join(dir, IMG_DIR, 'thumbs')
const entries = await fs().readDir(abs)
for (const e of entries) {
if (!e || e.isDirectory) continue
const base = String(e.name).replace(/\.[^.]+$/, '')
if (!map[base]) map[base] = String(e.name)
}
} catch (e) { /* 没有 thumbs 目录就返回空表 */ }
thumbMapCache = map
return map
}
async function galleryThumb(hash) {
try {
if (!enabled() || !dir || !hash) return ''
const map = await thumbFileMap()
const name = map[String(hash)]
if (!name) return ''
const u8 = await fs().readFile(await join(dir, IMG_DIR, 'thumbs', name))
if (!u8 || !u8.length) return ''
const mime = /\.png$/i.test(name) ? 'image/png' : 'image/jpeg'
return bytesToDataUrl(mime, u8)
} catch (e) { return '' }
}
// 画布显示缩略图(768px)落盘:重启后直接命中,不用重新解码 4K 原图
async function readDisplayThumb(hash) {
try {
if (!enabled() || !dir || !hash) return ''
const abs = await join(dir, IMG_DIR, 'thumbs', String(hash) + '_768.jpg')
if (!(await fs().exists(abs))) return ''
const u8 = await fs().readFile(abs)
if (!u8 || !u8.length) return ''
return bytesToDataUrl('image/jpeg', u8)
} catch (e) { return '' }
}
async function writeDisplayThumb(hash, dataUrl) {
try {
if (!enabled() || !dir || !hash || !dataUrl) return false
const m = /^data:[^;]+;base64,(.*)$/.exec(String(dataUrl))
if (!m) return false
const bin = atob(m[1])
const u8 = new Uint8Array(bin.length)
for (let i = 0; i < bin.length; i++) u8[i] = bin.charCodeAt(i)
const abs = await join(dir, IMG_DIR, 'thumbs', String(hash) + '_768.jpg')
if (await fs().exists(abs)) return true
await fs().mkdir(await join(dir, IMG_DIR, 'thumbs'), { recursive: true })
await fs().writeFile(abs, u8)
return true
} catch (e) { return false }
}
// 一次性:图库清单里历史遗留的 base64 data 删掉(缩略图已在 images/thumbs/)
async function stripInlineGalleryThumbs() {
try {
const marker = await join(dir, 'gallery-data-stripped.flag')
if (await fs().exists(marker)) return 0
const raw = mem['iwb_web_outputs']
if (!raw) { await fs().writeTextFile(marker, '0'); return 0 }
const items = JSON.parse(raw)
if (!Array.isArray(items)) return 0
const map = await thumbFileMap()
let n = 0
for (const it of items) {
if (!it || !it.data) continue
if (it.thumbHash && map[String(it.thumbHash)]) { it.data = ''; n++ }
}
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 gcImages(opts) {
const dryRun = !!(opts && opts.dryRun)
try {
@@ -758,7 +841,7 @@
}
global.IWBStorageFile = {
enabled, ready: init, flush, runtime, pickDir, switchTo, migrateTo, gcImages, rotateTrash,
enabled, ready: init, flush, runtime, pickDir, switchTo, migrateTo, gcImages, rotateTrash, galleryThumb, readDisplayThumb, writeDisplayThumb,
plugins: { manifest: pluginManifest, read: pluginRead, write: pluginWrite, remove: pluginRemove, dir: pluginDir, listLocal: pluginListLocal },
storage: () => storage,
kv: () => kv,