perf(storage): gcImages 递归子目录 + trash 30 天轮转(本机清出 20.2MB 未引用图片)
Verify (Gitea) / gate (build · unit · facts) (push) Successful in 1m58s

This commit is contained in:
qiqi committed 2026-11-17 15:08:23 +08:00
1 parent 942d4068ea
commit 0ec11bfca8
1 file changed
+49 -8
+49 -8
View File
@@ -626,6 +626,8 @@
try { await migrateBlobs() } catch (e) { /* 迁移失败不阻塞启动 */ }
// 图库清单补齐缩略图(一次性)
try { await backfillGalleryThumbs() } catch (e) { /* 忽略 */ }
// trash/ 轮转:gcImages 只是移进来,超过 30 天在这里真正删除
try { await rotateTrash(30) } catch (e) { /* 忽略 */ }
// 自愈:图库清单丢了但 assets 还有 out_ 键 ⇒ 按键重建(名称/时间/idb 可推,label/远程 url 无法恢复)
try {
if (mem['iwb_web_outputs'] === undefined) {
@@ -677,31 +679,70 @@
// 清理未引用图片:只动「没有任何 assets 条目指向」的文件,移到 trash/(不直接删)。
// 保守策略:assets 里还有登记的键一律保留,避免误删仍在画布上引用的图。
// trash/ 轮转:超过 maxAgeDays 的文件直接删除(gcImages 只是移进来,不清)
async function rotateTrash(maxAgeDays) {
const days = Number(maxAgeDays) > 0 ? Number(maxAgeDays) : 30
try {
if (!enabled() || !dir) return 0
const trashAbs = await join(dir, 'trash')
const cutoff = Date.now() - days * 86400000
let removed = 0
const walk = async (abs, depth) => {
if (depth > 3) return
let entries = []
try { entries = await fs().readDir(abs) } catch (e) { return }
for (const e of entries) {
if (!e) continue
const child = await join(abs, e.name)
if (e.isDirectory) { await walk(child, depth + 1); continue }
try {
const st = await fs().stat(child)
const mt = st && st.mtime ? new Date(st.mtime).getTime() : 0
if (mt && mt < cutoff) { await fs().remove(child); removed++ }
} catch (err) { /* 单条失败忽略 */ }
}
}
await walk(trashAbs, 0)
return removed
} catch (e) { return 0 }
}
async function gcImages(opts) {
const dryRun = !!(opts && opts.dryRun)
try {
if (!enabled() || !dir) return { ok: false, error: '资料目录未就绪' }
const used = new Set(Object.keys(assets).map((k) => assets[k] && assets[k].f).filter(Boolean))
const imgAbs = await join(dir, IMG_DIR)
const entries = await fs().readDir(imgAbs)
// 子目录也要扫:images/{canvas,gallery,thumbs,legacy}/ 里的孤儿同样占空间
const subs = ['', 'canvas', 'gallery', 'thumbs', 'legacy']
const orphans = []
for (const e of entries) {
if (!e || e.isDirectory) continue
if (!used.has(IMG_DIR + '/' + e.name)) orphans.push(e.name)
for (const sub of subs) {
const abs = sub ? await join(imgAbs, sub) : imgAbs
let entries = []
try { entries = await fs().readDir(abs) } catch (e) { continue }
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 (dryRun || !orphans.length) return { ok: true, orphans: orphans.length, moved: 0, dryRun: dryRun }
const trashAbs = await join(dir, 'trash')
try { await fs().mkdir(trashAbs, { recursive: true }) } catch (e) { /* 已存在 */ }
let moved = 0
for (const name of orphans) {
try { await fs().rename(await join(imgAbs, name), await join(trashAbs, name)); moved++ } catch (e) { /* 单条失败忽略 */ }
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))
moved++
} catch (e) { /* 单条失败忽略 */ }
}
return { ok: true, orphans: orphans.length, moved: moved, dryRun: dryRun }
} catch (e) { return { ok: false, error: String((e && e.message) || e) } }
}
global.IWBStorageFile = {
enabled, ready: init, flush, runtime, pickDir, switchTo, migrateTo, gcImages,
enabled, ready: init, flush, runtime, pickDir, switchTo, migrateTo, gcImages, rotateTrash,
plugins: { manifest: pluginManifest, read: pluginRead, write: pluginWrite, remove: pluginRemove, dir: pluginDir, listLocal: pluginListLocal },
storage: () => storage,
kv: () => kv,