/* gallery-audit.js —— 图库落盘自检(2026-11-26,只读) * * 为什么单独成模块:用户报障「图库面板有图,但资料目录 images/gallery 里找不到」。 * 这句话可能对应三种完全不同的状态,靠猜会修错地方: * ① 原图已落盘(out_orig_ 有键、磁盘有文件) * ② 仅缩略图(只有 out_ 的 320px 副本;原图从未写,或写了但文件被删) * ③ 全丢(索引条目在,缩略图与原图都没有,只剩远程 URL) * 这里只做判定与报告,不改任何存储;写入侧用 noteWrite() 记本次会话的成功/失败。 * * 判定输入刻意是可注入的事实(磁盘登记 + IDB 键集合),便于单测: * run({ index, storageFile, idbKeys }) * · storageFile = IWBStorageFile.galleryAudit(index) 的结果(桌面端才有真实磁盘文件) * · idbKeys = IDB.keys()(浏览器/兜底路径只有键集合) */ ;(function (global) { 'use strict' const STATES = { orig: 'orig', thumb: 'thumb', lost: 'lost' } const LABELS = { orig: '原图 OK', thumb: '仅缩略图', lost: '全丢' } const origKeyOf = (item) => String((item && item.origIdb) || ('out_orig_' + String((item && item.name) || ''))) const thumbKeyOf = (item) => String((item && item.idb) || ('out_' + String((item && item.name) || ''))) // 单条判定。fact 由 storageFile.galleryAudit 或键集合归一而来: // { orig: bool, thumb: bool, origPath: string, thumbPath: string, origKeyKnown: bool } function classify (item, fact) { const f = fact || {} const hasOrig = !!f.orig const hasThumb = !!f.thumb let state = STATES.lost let reason = '原图和缩略图都不在本地(只剩远程 URL)' if (hasOrig) { state = STATES.orig reason = '原图已落盘' } else if (hasThumb) { state = STATES.thumb if (f.origPath) reason = '原图文件缺失(登记还在,磁盘没有)' else if (f.origKeyKnown === false) reason = '原图键未登记(生成后没有写入 out_orig_)' else reason = '原图未落盘(只有 320px 缩略图)' } return { name: String((item && item.name) || ''), url: String((item && item.url) || ''), state, label: LABELS[state], reason, origKey: origKeyOf(item), thumbKey: thumbKeyOf(item), origPath: String(f.origPath || ''), thumbPath: String(f.thumbPath || ''), } } function summarize (rows) { const list = Array.isArray(rows) ? rows : [] const out = { total: list.length, orig: 0, thumb: 0, lost: 0, pending: 0 } for (const r of list) { if (!r) continue if (r.state === STATES.orig) out.orig++ else if (r.state === STATES.thumb) out.thumb++ else out.lost++ } return out } function deduceFactsFromKeys (item, keySet) { const keys = keySet || new Set() const ok = origKeyOf(item) const tk = thumbKeyOf(item) return { orig: keys.has(ok), thumb: keys.has(tk), origPath: '', thumbPath: '', origKeyKnown: !!(item && item.origIdb), } } function fileFacts (item, fileRows) { const name = String((item && item.name) || '') const f = fileRows && fileRows[name] if (!f) return deduceFactsFromKeys(item, new Set()) return { orig: !!f.orig, thumb: !!f.thumb, origPath: String(f.origPath || ''), thumbPath: String(f.thumbPath || ''), origKeyKnown: !!f.origKey, } } async function run (opts) { const o = opts || {} const index = Array.isArray(o.index) ? o.index : [] let facts = null let dir = '' let galleryFiles = [] let deskError = '' const sf = o.storageFile if (sf && typeof sf.galleryAudit === 'function') { try { const r = await sf.galleryAudit(index) if (r && r.ok) { facts = {} for (const row of (r.rows || [])) if (row && row.name) facts[String(row.name)] = row dir = String(r.dir || '') galleryFiles = Array.isArray(r.galleryFiles) ? r.galleryFiles.slice() : [] } else if (r && r.error) deskError = String(r.error) } catch (e) { deskError = String((e && e.message) || e) } } const keySet = new Set(Array.isArray(o.idbKeys) ? o.idbKeys.map(String) : []) const rows = index.map((it) => classify(it, facts ? fileFacts(it, facts) : deduceFactsFromKeys(it, keySet))) // 磁盘上有、但没有任何索引条目引用的原图:删图只清了索引时留下的孤儿 let orphans = [] if (facts && galleryFiles.length) { const used = new Set() for (const r of rows) if (r.origPath) used.add(String(r.origPath).split('/').pop()) orphans = galleryFiles.filter((n) => !used.has(String(n))) } return { at: Date.now(), dir, desktop: !!facts, error: deskError, summary: summarize(rows), rows, orphans, session: stats(), } } // 本次会话写入计数:挂在存储层(storage-file / storage-runtime),不改生成流程。 const session = { origOk: 0, origFail: 0, reasons: {} } function noteWrite (key, ok, error) { const k = String(key || '') if (k.indexOf('out_orig_') !== 0) return if (ok) { session.origOk++; return } session.origFail++ const why = String((error && (error.message || error.error)) || error || '未知原因').slice(0, 120) session.reasons[why] = (session.reasons[why] || 0) + 1 } function stats () { const reasons = Object.keys(session.reasons).map((k) => ({ reason: k, count: session.reasons[k] })).sort((a, b) => b.count - a.count) return { origOk: session.origOk, origFail: session.origFail, reasons } } function resetSession () { session.origOk = 0; session.origFail = 0; session.reasons = {} } function report (result) { const r = result || {} const s = r.summary || summarize(r.rows) const lines = [] lines.push('图库落盘自检' + (r.at ? ' · ' + new Date(r.at).toISOString() : '')) if (r.dir) lines.push('资料目录:' + r.dir) lines.push('索引 ' + s.total + ' 条:原图 OK ' + s.orig + ' / 仅缩略图 ' + s.thumb + ' / 全丢 ' + s.lost) if (r.desktop) lines.push('磁盘上未被索引引用的原图:' + (r.orphans ? r.orphans.length : 0) + ' 个') if (!r.desktop) lines.push('未读取磁盘文件(浏览器环境或资料目录未就绪),判定依据仅为 IDB 键集合' + (r.error ? ':' + r.error : '')) lines.push('本次会话原图写入:成功 ' + ((r.session && r.session.origOk) || 0) + ' / 失败 ' + ((r.session && r.session.origFail) || 0)) for (const why of ((r.session && r.session.reasons) || []).slice(0, 5)) lines.push(' · 失败原因 ×' + why.count + ':' + why.reason) lines.push('') lines.push('明细(前 20 条):') for (const row of (Array.isArray(r.rows) ? r.rows : []).slice(0, 20)) { lines.push(' [' + row.label + '] ' + row.name + ' — ' + row.reason + (row.origPath ? ' · ' + row.origPath : '')) } if (!Array.isArray(r.rows) || !r.rows.length) lines.push(' (图库索引为空)') return lines.join('\n') } global.IWBGalleryAudit = { STATES: STATES, LABELS: LABELS, classify: classify, summarize: summarize, run: run, report: report, noteWrite: noteWrite, stats: stats, resetSession: resetSession, } })(typeof window !== 'undefined' ? window : globalThis)