From 99f676f4605e8a900c9213a87645820730720a0a Mon Sep 17 00:00:00 2001 From: qiqi Date: Tue, 17 Nov 2026 07:48:44 +0800 Subject: [PATCH] =?UTF-8?q?fix(storage):=20=E5=9B=BE=E5=BA=93=E6=B8=85?= =?UTF-8?q?=E5=8D=95=E6=94=B9=E4=B8=BA=20library/gallery/index.json?= =?UTF-8?q?=EF=BC=9B=E8=BF=81=E7=A7=BB=E6=94=B9=E4=B8=BA=E5=85=88=E5=86=99?= =?UTF-8?q?=E5=90=8E=E5=88=A0=20+=20=E6=B8=85=E5=8D=95=E8=87=AA=E6=84=88?= =?UTF-8?q?=E9=87=8D=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/storage-file.js | 54 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/src/storage-file.js b/src/storage-file.js index 63950ed..9c54ce8 100644 --- a/src/storage-file.js +++ b/src/storage-file.js @@ -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 是会话级的)