fix(settings): 清理未引用图片跳过缩略图缓存;弹窗文案与实现对齐
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m3s

This commit is contained in:
qiqi committed 2026-12-03 13:54:30 +08:00
1 parent c3bbff9f57
commit c98b14df5b
3 files changed
+34 -8

No files matched your search

+20 -4
View File
@@ -1510,6 +1510,10 @@
// 子目录也要扫:images/{canvas,gallery,thumbs,legacy}/ 里的孤儿同样占空间
const subs = ['', 'canvas', 'gallery', 'thumbs', 'legacy']
const orphans = []
// 2026-12-03:thumbs/<hash>_<max>.<ext> 是**显示缓存**(320/512/768/2048),不进 assets、可再生。
// 之前把它们算成「未引用图片」并搬进 trash ⇒ 每次清理都搬一遍新缓存,数字也被夸大。默认不动。
const THUMB_CACHE_RE = /_\d+\.(jpg|jpeg|webp|png)$/i
let thumbCaches = 0
for (const sub of subs) {
const abs = sub ? await join(imgAbs, sub) : imgAbs
let entries = []
@@ -1517,25 +1521,37 @@
for (const e of entries) {
if (!e || e.isDirectory) continue
const rel = IMG_DIR + '/' + (sub ? sub + '/' : '') + e.name
if (!used.has(rel)) orphans.push({ sub: sub, name: e.name, rel: rel })
if (used.has(rel)) continue
if (sub === 'thumbs' && THUMB_CACHE_RE.test(String(e.name))) { thumbCaches++; continue }
orphans.push({ sub: sub, name: e.name, rel: rel })
}
}
// 断链:画布引用了、但登记表里没有的 key —— 这类节点的图**已经显示不出来**,
// GC 不会动它们(文件根本不在),但值得在确认弹窗里报数,让用户知道数据已经缺了。
const dangling = Array.from(canvasKeys).filter((k) => !assets[k]).length
const keptByCanvas = referenced.size
if (dryRun || !orphans.length) return { ok: true, orphans: orphans.length, moved: 0, dryRun: dryRun, dangling: dangling, keptByCanvas: keptByCanvas }
if (dryRun || !orphans.length) return { ok: true, orphans: orphans.length, moved: 0, dryRun: dryRun, dangling: dangling, keptByCanvas: keptByCanvas, thumbCaches: thumbCaches }
const trashAbs = await join(dir, 'trash')
let moved = 0
for (const item of orphans) {
try {
const targetDir = item.sub ? await join(trashAbs, item.sub) : trashAbs
await fs().mkdir(targetDir, { recursive: true })
await fs().rename(await join(imgAbs, item.sub, item.name), await join(targetDir, item.name))
let target = await join(targetDir, item.name)
try {
await fs().rename(await join(imgAbs, item.sub, item.name), target)
} catch (eR) {
// 同名已在 trash 里(上次清理过):加时间戳,别让整条静默失败
const dot = String(item.name).lastIndexOf('.')
const sfx = '-' + Date.now()
const alt = dot > 0 ? String(item.name).slice(0, dot) + sfx + String(item.name).slice(dot) : String(item.name) + sfx
target = await join(targetDir, alt)
await fs().rename(await join(imgAbs, item.sub, item.name), target)
}
moved++
} catch (e) { /* 单条失败忽略 */ }
}
return { ok: true, orphans: orphans.length, moved: moved, dryRun: dryRun, dangling: dangling, keptByCanvas: keptByCanvas }
return { ok: true, orphans: orphans.length, moved: moved, dryRun: dryRun, dangling: dangling, keptByCanvas: keptByCanvas, thumbCaches: thumbCaches }
} catch (e) { return { ok: false, error: String((e && e.message) || e) } }
}
+4 -3
View File
@@ -631,9 +631,10 @@ function GSetGroup (props) {
const rDry = await F.gcImages({ dryRun: true })
if (!rDry.ok) { setNote('检查失败:' + rDry.error); return }
if (!rDry.orphans) { setNote('没有未引用的图片'); return }
const lines = ['发现 ' + rDry.orphans + ' 个文件没有被画布/图库引用。']
if (rDry.keptByCanvas) lines.push('其中 ' + rDry.keptByCanvas + ' 张虽然登记表里没有,但画布仍在引用 → 会自动保留。')
if (rDry.dangling) lines.push('另有 ' + rDry.dangling + ' 处画布引用已经找不到图片(之前丢的,本次不影响它们)。')
const lines = ['发现 ' + rDry.orphans + ' 个图片文件没有被画布/图库引用。']
if (rDry.thumbCaches) lines.push('另有 ' + rDry.thumbCaches + ' 个缩略图缓存(可再生,本次不动)。')
if (rDry.keptByCanvas) lines.push(rDry.keptByCanvas + ' 个文件画布/图库同时在用 → 自动保留。')
if (rDry.dangling) lines.push('另有 ' + rDry.dangling + ' 处画布引用的图片已经缺失(登记表里没有,无法恢复,本次不受影响)。')
lines.push('移动到 trash/(不直接删除,可在回收站找回)?')
if (!(await window.IWBUIConfirm.askOrNative(lines.join('\n'), { title: '清理未引用图片', okText: '移动' }))) return
const rGo = await F.gcImages({ dryRun: false })
+10 -1
View File
@@ -36,4 +36,13 @@ for (let i = 1; i < putRegions.length; i++) {
}
assert.ok(flushed >= 2, '图片登记后必须立刻 await flush()(当前 ' + flushed + '/2 处)')
console.log('storage-gc-canvas-refs: 8 通过 / 0 失败')
// ④ 显示缩略图缓存(thumbs/<hash>_<max>.<ext>)是再生缓存,不得被当作未引用图片搬走
assert.ok(/THUMB_CACHE_RE/.test(gcBody) && /thumbCaches/.test(gcBody), '④ gcImages 必须跳过 thumbs 下的显示缓存并单独计数')
assert.ok(/sub === 'thumbs' && THUMB_CACHE_RE/.test(gcBody), '④ 只对 thumbs 目录应用缓存判据')
// ⑤ 确认弹窗文案必须与实现一致:assets 缺键时拿不到文件名,并不存在「自动保留」的兜底
const UI = fs.readFileSync(path.join(__dirname, '..', 'src', 'ui-settings-sections.js'), 'utf8')
assert.ok(!/登记表里没有,但画布仍在引用/.test(UI), '⑤ 弹窗不得声称「登记表里没有但画布仍在引用 → 自动保留」')
assert.ok(/缩略图缓存/.test(UI), '⑤ 弹窗应说明跳过的缩略图缓存数量')
console.log('storage-gc-canvas-refs: 12 通过 / 0 失败')