feat(storage): data.json 备份/资产骤减保护 + 缩略图批量读(Rust 批次 A2)
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 54s
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 54s
This commit is contained in:
1 parent
352b2e0fe6
commit
0416bac128
9 files changed
+208
-11
No files matched your search
+81
-2
@@ -25,6 +25,12 @@
|
||||
let loaded = false
|
||||
let timer = null
|
||||
let writing = null
|
||||
// 2026-12-05(事故后保护):data.json 的 assets 登记表骤减检测。
|
||||
// 事故:登记表被旧版本覆盖 ⇒ 图库/输出老图全部解析不到文件(文件还在)。
|
||||
// 做法:每次启动把上一轮 data.json 留成 data.json.bak;读到的 assets 若比 bak 少 30% 以上,
|
||||
// 判定为异常,**保留 bak 不覆盖**并写 _boot.log;运行期再骤减也只记录、不改 bak。
|
||||
let lastGoodAssets = 0
|
||||
let assetsGuard = null
|
||||
|
||||
// 惰性取 __TAURI__:模块加载时它未必已注入,但每个调用点都要能拿到
|
||||
const tk = () => { if (!tauri) { try { tauri = global.__TAURI__ || null } catch (e) { tauri = null } } return tauri }
|
||||
@@ -48,6 +54,10 @@
|
||||
if (!enabled() || !dir) return null
|
||||
try { const r = await rustInvoke('list_dir_entries', { base: dir, rel: String(rel || '') }); return Array.isArray(r) ? r : null } catch (e) { return null }
|
||||
}
|
||||
const readFilesB64Of = async (rels) => {
|
||||
if (!enabled() || !dir || !Array.isArray(rels) || !rels.length) return null
|
||||
try { const r = await rustInvoke('read_files_b64', { base: dir, rels: rels }); return Array.isArray(r) ? r : null } catch (e) { return null }
|
||||
}
|
||||
|
||||
const join = async (...p) => {
|
||||
const P = path()
|
||||
@@ -165,6 +175,14 @@
|
||||
}
|
||||
// 2026-12-04:data.json 用内容签名判重(不含 at),没变化就不写
|
||||
if (dataSig !== lastDataSig) {
|
||||
// 2026-12-05:assets 骤减只记录(bak 已在启动时保存,不在运行期覆盖)
|
||||
const curN = Object.keys(assets).length
|
||||
if (lastGoodAssets > 20 && curN < lastGoodAssets * 0.7) {
|
||||
assetsGuard = { cur: curN, prev: lastGoodAssets, at: Date.now() }
|
||||
try { await blog('assets-regress-write cur=' + curN + ' prev=' + lastGoodAssets) } catch (e) { /* ignore */ }
|
||||
} else {
|
||||
lastGoodAssets = Math.max(lastGoodAssets, curN)
|
||||
}
|
||||
const tmp2 = await join(dir, DATA_FILE + '.tmp')
|
||||
await fs().writeTextFile(tmp2, body)
|
||||
await fs().rename(tmp2, target)
|
||||
@@ -857,6 +875,27 @@
|
||||
return fromDataJson ? 'loaded' : 'migrated'
|
||||
}
|
||||
|
||||
// 2026-12-05:启动时的 data.json 备份 + 登记表骤减检测(见顶部 lastGoodAssets 注释)。
|
||||
async function guardDataJsonBackup () {
|
||||
try {
|
||||
const target = await join(dir, DATA_FILE)
|
||||
const bak = await join(dir, DATA_FILE + '.bak')
|
||||
let curTxt = ''
|
||||
try { curTxt = await fs().readTextFile(target) } catch (e) { return }
|
||||
let bakN = 0
|
||||
try { const b = JSON.parse(await fs().readTextFile(bak)); bakN = Object.keys((b && b.assets) || {}).length } catch (e) { bakN = 0 }
|
||||
let curN = 0
|
||||
try { const c = JSON.parse(curTxt); curN = Object.keys((c && c.assets) || {}).length } catch (e) { curN = 0 }
|
||||
lastGoodAssets = Math.max(curN, bakN)
|
||||
if (bakN > 20 && curN < bakN * 0.7) {
|
||||
assetsGuard = { cur: curN, prev: bakN, at: Date.now() }
|
||||
await blog('assets-regress-load cur=' + curN + ' prev=' + bakN)
|
||||
return
|
||||
}
|
||||
try { await fs().writeTextFile(bak, curTxt) } catch (e) { /* ignore */ }
|
||||
} catch (e) { /* ignore */ }
|
||||
}
|
||||
|
||||
// 一次性迁移:把老版本存在 IndexedDB 里的图片字节搬到 images/ 下(跑过一次就写标记文件)
|
||||
// 启动日志:超过 256KB 只保留最后 64KB(否则长年累月会一直长)
|
||||
const blog = async (m) => {
|
||||
@@ -1035,6 +1074,7 @@
|
||||
try { await repairCanvasFileNames() } catch (e) { /* 修复失败按原样装载 */ }
|
||||
bpReport('prepare', 4, 4)
|
||||
await loadInto()
|
||||
try { await guardDataJsonBackup() } catch (e) { /* 保护失败不影响启动 */ }
|
||||
// 从 WebView 的 localStorage 捞回被截断/覆盖的画布(一次性)
|
||||
try { await recoverFromWebStorage() } catch (e) { /* 忽略 */ }
|
||||
loaded = true
|
||||
@@ -1372,6 +1412,45 @@
|
||||
return url
|
||||
} catch (e) { return '' }
|
||||
}
|
||||
// 2026-12-05(批次 A2):画布打开时的缩略图批量读取。
|
||||
// 旧实现每张图 3 次 exists + 1 次读文件;这里改成「一次列 thumbs 目录 + 一次批量读」。
|
||||
// 返回 { '<hash>_<max>': dataURL };命令不可用/失败返回 null(调用方回退逐张)。
|
||||
async function readDisplayThumbsMany (pairs) {
|
||||
try {
|
||||
if (!enabled() || !dir || !Array.isArray(pairs) || !pairs.length) return null
|
||||
const ents = await listDirEntriesOf(IMG_DIR + '/thumbs')
|
||||
if (!ents) return null
|
||||
const names = new Set(ents.filter((e) => e && !e.is_dir).map((e) => String(e.name)))
|
||||
if (!names.size) return {}
|
||||
const plan = []
|
||||
const rels = []
|
||||
for (const p of pairs) {
|
||||
const h = String((p && p.hash) || '')
|
||||
if (!h) continue
|
||||
const m = Math.max(64, Number(p.max) || 768)
|
||||
const ck = h + '_' + m
|
||||
let name = ''
|
||||
for (const ext of ['.webp', '.jpg', '.png']) { const n = h + '_' + m + ext; if (names.has(n)) { name = n; break } }
|
||||
if (!name) continue
|
||||
rels.push(IMG_DIR + '/thumbs/' + name)
|
||||
plan.push({ ck: ck, name: name })
|
||||
}
|
||||
if (!rels.length) return {}
|
||||
const got = await readFilesB64Of(rels)
|
||||
if (!got) return null
|
||||
const byName = new Map(plan.map((x) => [x.name, x.ck]))
|
||||
const out = {}
|
||||
for (const g of got) {
|
||||
if (!g || !g.b64) continue
|
||||
const name = String(g.rel || '').split('/').pop()
|
||||
const ck = byName.get(name)
|
||||
if (!ck) continue
|
||||
const mime = /\.png$/i.test(name) ? 'image/png' : (/\.webp$/i.test(name) ? 'image/webp' : 'image/jpeg')
|
||||
out[ck] = 'data:' + mime + ';base64,' + String(g.b64)
|
||||
}
|
||||
return out
|
||||
} catch (e) { return null }
|
||||
}
|
||||
// v3.25.78:缩略图没有回收机制,历史上会无限增长(实测 470 个 / 67MB)。
|
||||
// 这里按总大小封顶(默认 300MB,超了删最老的 20%),缩略图可再生,删掉不影响数据。
|
||||
async function pruneThumbs (maxMB) {
|
||||
@@ -1845,7 +1924,7 @@
|
||||
enabled, ready: init, flush,
|
||||
// 供其它模块把诊断写进 _boot.log(v3.26.109)
|
||||
logLine: (m) => blog(m),
|
||||
runtime, pickDir, switchTo, migrateTo, deleteImageBytes, deleteGalleryBytesFor, galleryAudit, gcImages, rotateTrash, galleryThumb, galleryThumbUrl, galleryThumbOfEntry, galleryOriginalOf, galleryOriginalBlob, putFileFrom, displayThumbDataUrlOfRef, readDisplayThumb, readDisplayThumbUrl, makeDisplayThumb, makeDisplayThumbFromPath, resizeDataUrl, decodeStats, thumbStats, writeDisplayThumb, writeDisplayThumbBlob, pruneThumbs, cleanupLegacy, maintain,
|
||||
runtime, pickDir, switchTo, migrateTo, deleteImageBytes, deleteGalleryBytesFor, galleryAudit, gcImages, rotateTrash, galleryThumb, galleryThumbUrl, galleryThumbOfEntry, galleryOriginalOf, galleryOriginalBlob, putFileFrom, displayThumbDataUrlOfRef, readDisplayThumb, readDisplayThumbUrl, readDisplayThumbsMany, makeDisplayThumb, makeDisplayThumbFromPath, resizeDataUrl, decodeStats, thumbStats, writeDisplayThumb, writeDisplayThumbBlob, pruneThumbs, cleanupLegacy, maintain,
|
||||
plugins: { manifest: pluginManifest, read: pluginRead, write: pluginWrite, remove: pluginRemove, dir: pluginDir, listLocal: pluginListLocal, listDirs: pluginListDirs, readFile: pluginReadFile, writeFile: pluginWriteFile, writeBinary: pluginWriteBinary, mkdir: pluginMkdir, removeDir: pluginRemoveDir, renameDir: pluginRenameDir, existsDir: pluginExistsDir },
|
||||
pluginData: { read: pluginDataRead, write: pluginDataWrite, remove: pluginDataRemove, list: pluginDataList },
|
||||
pluginFiles: { put: pluginFilePut, get: pluginFileGet, list: pluginFileList, remove: pluginFileRemove, stat: pluginFileStat, dir: pluginFilesDir, readBytes: pluginFileReadBytes, listAll: pluginFilesListAll },
|
||||
@@ -1853,7 +1932,7 @@
|
||||
kv: () => kv,
|
||||
dir: () => dir,
|
||||
setDir: async (next) => { dir = String(next || ''); await flush();},
|
||||
stats: () => ({ dir, keys: Object.keys(mem).length, images: Object.keys(assets).length, ipc: ipcCalls }),
|
||||
stats: () => ({ dir, keys: Object.keys(mem).length, images: Object.keys(assets).length, ipc: ipcCalls, assetsGuard: assetsGuard, lastGoodAssets: lastGoodAssets }),
|
||||
__internals: { snapshot, loadInto, sha16, extOf, dataUrlToBytes, bytesToDataUrl, pluginFileNameOk },
|
||||
}
|
||||
// 关窗/收托盘前强制刷盘(beforeunload 在桌面端不保证,但能救一次是一次)
|
||||
|
||||
Reference in new issue
Block a user