fix(storage): 画布文件名少了 2 个字符导致键对不上(节点显示为空);加一次性文件名修复
This commit is contained in:
1 parent
b193dc38ff
commit
8725e65fcd
1 file changed
+34
-3
+34
-3
@@ -44,7 +44,8 @@
|
||||
return n
|
||||
}
|
||||
const CANVAS_DIR = 'canvases'
|
||||
const isWsKey = (k) => typeof k === 'string' && k.indexOf('iwb_ws_') === 0
|
||||
const WS_PREFIX = 'iwb_ws_' // 长度 7(早期实现误用 slice(9),文件名丢了 2 个字符)
|
||||
const isWsKey = (k) => typeof k === 'string' && k.indexOf(WS_PREFIX) === 0
|
||||
// 资源库:按分类单独成文件(library/<分类>.json),内容是该键的原始 JSON 文本
|
||||
// 图片按"来源类别"分二级目录:可读、可分类清理、thumbs 可整体删
|
||||
const IMG_CAT = { canvas: 'canvas', gallery: 'gallery', thumbs: 'thumbs', legacy: 'legacy' }
|
||||
@@ -94,7 +95,7 @@
|
||||
for (const k of wsDirty) {
|
||||
const text = mem[k]
|
||||
if (text === undefined) continue
|
||||
const abs = await join(cdir, k.slice(9) + '.json')
|
||||
const abs = await join(cdir, k.slice(WS_PREFIX.length) + '.json')
|
||||
try {
|
||||
const tmp = await join(cdir, k.slice(9) + '.json.tmp')
|
||||
await fs().writeTextFile(tmp, String(text))
|
||||
@@ -103,7 +104,7 @@
|
||||
}
|
||||
}
|
||||
for (const k of wsGone) {
|
||||
try { await fs().remove(await join(dir, CANVAS_DIR, k.slice(9) + '.json')) } catch (e) { /* 不存在就算了 */ }
|
||||
try { await fs().remove(await join(dir, CANVAS_DIR, k.slice(WS_PREFIX.length) + '.json')) } catch (e) { /* 不存在就算了 */ }
|
||||
}
|
||||
// 资源库:整体重写分类文件(都很小,改动频繁,不做脏标记)
|
||||
const libKeys = Object.keys(mem).filter((k) => !!libFileOf(k))
|
||||
@@ -334,6 +335,35 @@
|
||||
} catch (e) { return 0 }
|
||||
}
|
||||
|
||||
// 修复(一次性):早期版本用 slice(9) 生成文件名,丢了 id 前 2 个字符("ws")
|
||||
// ⇒ 读每个文件内容里的 id,把文件名改回 <id>.json,键才能和画布对上。
|
||||
async function repairCanvasFileNames() {
|
||||
try {
|
||||
const marker = await join(dir, 'canvases-fixed.flag')
|
||||
if (await fs().exists(marker)) return 0
|
||||
const cdir = await join(dir, CANVAS_DIR)
|
||||
const entries = await fs().readDir(cdir)
|
||||
let n = 0
|
||||
for (const e of entries) {
|
||||
if (!e || e.isDirectory || !/\.json$/.test(e.name)) continue
|
||||
try {
|
||||
const txt = await fs().readTextFile(await join(cdir, e.name))
|
||||
let id = ''
|
||||
try { const j = JSON.parse(txt); id = String((j && j.id) || '') } catch (err) { id = '' }
|
||||
const base = e.name.replace(/\.json$/, '')
|
||||
const want = id ? (id.indexOf('ws') === 0 ? id : 'ws' + id) : 'ws' + base
|
||||
const target = want + '.json'
|
||||
if (target !== e.name) {
|
||||
await fs().rename(await join(cdir, e.name), await join(cdir, target))
|
||||
n++
|
||||
}
|
||||
} catch (err) { /* 单个失败跳过 */ }
|
||||
}
|
||||
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)
|
||||
@@ -505,6 +535,7 @@
|
||||
dir = want
|
||||
try { global.localStorage.setItem(PTR_KEY, dir) } catch (e) { /* ignore */ }
|
||||
try { await fs().mkdir(await join(dir, IMG_DIR), { recursive: true }) } catch (e) { /* 已存在 */ }
|
||||
try { await repairCanvasFileNames() } catch (e) { /* 修复失败按原样装载 */ }
|
||||
await loadInto()
|
||||
loaded = true
|
||||
// 首次迁移要立刻落盘,别等 debounce
|
||||
|
||||
Reference in new issue
Block a user