fix(storage): 图库清单改为 library/gallery/index.json;迁移改为先写后删 + 清单自愈重建

This commit is contained in:
qiqi committed 2026-11-17 07:48:44 +08:00
1 parent a0f88bf306
commit 99f676f460
1 file changed
+44 -10
+44 -10
View File
@@ -61,7 +61,7 @@
iwb_web_prompts: 'prompts.json', // 提示词库
iwb_web_groups: 'groups.json', // 节点组
iwb_web_groupgroups: 'group-groups.json', // 节点组的分组树
iwb_web_outputs: 'gallery.json', // 图库索引(图片字节仍在 images/,按 hash 引用)
iwb_web_outputs: 'gallery/index.json', // 图库清单(图片字节仍在 images/gallery/,按 hash 引用)
}
const libFileOf = (k) => LIB_FILES[k] || ''
const dirtyWs = new Set()
@@ -111,7 +111,16 @@
const ldir = await join(dir, LIB_DIR)
try { await fs().mkdir(ldir, { recursive: true }) } catch (e) { /* 已存在 */ }
for (const k of libKeys) {
try { await fs().writeTextFile(await join(ldir, libFileOf(k)), String(mem[k])) } catch (e) { /* 单个失败不影响其它 */ }
const rel = libFileOf(k)
try {
if (rel.indexOf('/') > 0) {
const parent = await join(ldir, rel.slice(0, rel.lastIndexOf('/')))
try { await fs().mkdir(parent, { recursive: true }) } catch (e) { /* 已存在 */ }
}
await fs().writeTextFile(await join(ldir, rel), String(mem[k]))
// 新位置写成功后才删旧布局文件,避免"删了但没写上"造成数据丢失
if (rel === 'gallery/index.json') { try { await fs().remove(await join(ldir, 'gallery.json')) } catch (e2) { /* 不存在 */ } }
} catch (e) { /* 单个失败不影响其它 */ }
}
}
const tmp2 = await join(dir, DATA_FILE + '.tmp')
@@ -332,14 +341,13 @@
// 资源库分类文件(library/*.json):文件比 data.json 新,直接覆盖同名键
try {
const ldir = await join(dir, LIB_DIR)
const byFile = {}
for (const k of Object.keys(LIB_FILES)) byFile[LIB_FILES[k]] = k
const lentries = await fs().readDir(ldir)
for (const e of lentries) {
if (!e || e.isDirectory) continue
const key = byFile[e.name]
if (!key) continue
try { mem[key] = await fs().readTextFile(await join(ldir, e.name)) } catch (err) { /* 单个失败忽略 */ }
for (const key of Object.keys(LIB_FILES)) {
try { mem[key] = await fs().readTextFile(await join(ldir, LIB_FILES[key])) } catch (err) {
// 兼容旧布局:library/gallery.json(平铺);读到就沿用,flush 会写到新位置
if (key === 'iwb_web_outputs') {
try { mem[key] = await fs().readTextFile(await join(ldir, 'gallery.json')) } catch (err2) { /* 也没有 */ }
}
}
}
} catch (e) { /* 目录还没有 */ }
// 旧格式:画布还压在 data.json 里 ⇒ 标脏,下一次 flush 拆成单文件
@@ -462,6 +470,32 @@
await flush()
// 老版本图片在 IndexedDB 里 ⇒ 搬到 images/(带标记,只跑一次)
try { await migrateBlobs() } catch (e) { /* 迁移失败不阻塞启动 */ }
// 自愈:图库清单丢了但 assets 还有 out_ 键 ⇒ 按键重建(名称/时间/idb 可推,label/远程 url 无法恢复)
try {
if (mem['iwb_web_outputs'] === undefined) {
const names = []
for (const k of Object.keys(assets)) {
if (k.indexOf('out_orig_') === 0) names.push(k.slice(9))
else if (k.indexOf('out_') === 0) names.push(k.slice(4))
}
const uniq = Array.from(new Set(names))
if (uniq.length) {
const items = uniq.map((name) => {
const m = /(\d{13})/.exec(name)
const orig = 'out_orig_' + name
return {
name: name, url: '', size: 0, mtime: m ? Number(m[1]) : Date.now(), w: 0, h: 0, label: '',
idb: 'out_' + name,
origIdb: Object.prototype.hasOwnProperty.call(assets, orig) ? orig : undefined,
}
})
items.sort((a, b) => a.mtime - b.mtime)
mem['iwb_web_outputs'] = JSON.stringify(items)
await flush()
await blog('gallery:rebuilt ' + items.length)
}
}
} catch (e) { /* 自愈失败不影响启动 */ }
// 平铺图片 → 二级分类目录(一次性)
try { await migrateV2() } catch (e) { /* 迁移失败不阻塞启动 */ }
// 「点下载直接落盘」要求下载目录在本次运行里也被授权(fs scope 是会话级的)