#!/usr/bin/env node /* 图片存储巡检(只读):回答「图存在哪 / 节点里为什么是链接 / 有没有孤儿文件」。 * * 用法: * node scripts/audit-image-storage.js # 默认读 ~/Documents/ImageWorkbench * node scripts/audit-image-storage.js --dir /path # 指定资料目录 * * 判读口径见 docs/standards/image-storage-map.md。本脚本**不修改任何文件**, * 存在的意义是:存储结论必须由实际数据支撑,不能由注释或默认值推断(2026-11-24 误判的教训)。 */ 'use strict' const fs = require('fs') const path = require('path') const os = require('os') const args = process.argv.slice(2) const dirArg = (() => { const i = args.indexOf('--dir') if (i >= 0 && args[i + 1]) return args[i + 1] return process.env.IWB_DATA_DIR || path.join(os.homedir(), 'Documents', 'ImageWorkbench') })() const kb = (n) => (n >= 1048576 ? (n / 1048576).toFixed(1) + 'MB' : (n / 1024).toFixed(0) + 'KB') const readJson = (p, fb) => { try { return JSON.parse(fs.readFileSync(p, 'utf8')) } catch (e) { return fb } } const dirSize = (p) => { let n = 0 try { for (const f of fs.readdirSync(p)) { const abs = path.join(p, f) try { const st = fs.statSync(abs) n += st.isDirectory() ? dirSize(abs) : st.size } catch (e) { /* 单文件失败忽略 */ } } } catch (e) { return 0 } return n } if (!fs.existsSync(dirArg)) { console.log('资料目录不存在:' + dirArg) console.log('(说明:这是网页版/未初始化环境,图片字节在浏览器 IndexedDB,不在磁盘上)') process.exit(0) } console.log('资料目录:' + dirArg) console.log('总体积:' + kb(dirSize(dirArg))) console.log('') // ---- ① data.json 登记表 ---- const data = readJson(path.join(dirArg, 'data.json'), {}) const assets = (data && data.assets) || {} const keys = Object.keys(assets) const regByCat = {} const regByKind = {} const registeredFiles = new Set() for (const k of keys) { const a = assets[k] || {} const parts = String(a.f || '').split('/') const cat = parts[0] === 'images' ? (parts[1] || '?') : '(非图片)' regByCat[cat] = (regByCat[cat] || 0) + 1 regByKind[a.k || '?'] = (regByKind[a.k || '?'] || 0) + 1 if (a.f) registeredFiles.add(a.f) } console.log('① 登记表 data.json:' + kb(fs.statSync(path.join(dirArg, 'data.json')).size) + ' · assets ' + keys.length + ' 条 · imgblob_ ' + keys.filter((k) => k.indexOf('imgblob_') === 0).length + ' 条') console.log(' 按目录:' + JSON.stringify(regByCat)) console.log(' 按类型:' + JSON.stringify(regByKind)) console.log('') // ---- ② 目录实际文件 vs 登记(孤儿) ---- const imgRoot = path.join(dirArg, 'images') let orphanAll = 0 if (fs.existsSync(imgRoot)) { console.log('② images/ 实际文件(未登记 = 孤儿;_768 显示版属已知例外)') for (const cat of fs.readdirSync(imgRoot)) { const abs = path.join(imgRoot, cat) let st = null try { st = fs.statSync(abs) } catch (e) { continue } if (!st.isDirectory()) continue const files = fs.readdirSync(abs) let bytes = 0 let reg = 0 let orphan = 0 let display = 0 const samples = [] for (const f of files) { const rel = 'images/' + cat + '/' + f let sz = 0 try { sz = fs.statSync(path.join(abs, f)).size } catch (e) { /* ignore */ } bytes += sz if (/_768\.(jpg|webp)$/i.test(f) && !registeredFiles.has(rel)) { display++; continue } if (registeredFiles.has(rel)) reg++ else { orphan++; if (samples.length < 2) samples.push(f) } } orphanAll += orphan console.log(' ' + cat.padEnd(8) + String(files.length).padStart(4) + ' 个 ' + kb(bytes).padStart(8) + ' · 已登记 ' + reg + ' · 孤儿 ' + orphan + (display ? ' · 显示版 ' + display : '') + (samples.length ? ' · 例 ' + samples.join(', ') : '')) } console.log('') } // ---- ③ 图库清单 ---- const gi = readJson(path.join(dirArg, 'library', 'gallery', 'index.json'), null) const gList = Array.isArray(gi) ? gi : (gi && Array.isArray(gi.files) ? gi.files : null) if (gList) { const scheme = {} let withOrig = 0 for (const f of gList) { if (f && f.origIdb) withOrig++ const u = f && f.url const s = typeof u === 'string' && u ? (u.slice(0, 5).toLowerCase() === 'https' ? 'https' : (u.indexOf('data:') === 0 ? 'dataURL' : 'other')) : '(空)' scheme[s] = (scheme[s] || 0) + 1 } console.log('③ 图库清单 library/gallery/index.json:' + gList.length + ' 条') console.log(' 有 origIdb(本地原图在 images/gallery/):' + withOrig + '/' + gList.length) console.log(' url 形态:' + JSON.stringify(scheme)) console.log('') } else { console.log('③ 图库清单不存在或为空\n') } // ---- ④ 画布节点引用形态 ---- const cvDir = path.join(dirArg, 'canvases') if (fs.existsSync(cvDir)) { const files = fs.readdirSync(cvDir).filter((f) => f.endsWith('.json')) const shape = (v) => { if (!v) return '空' if (typeof v === 'string') { if (/^https?:/i.test(v)) return 'http 链接' if (v.indexOf('data:') === 0) return '内联 base64' return '其它字符串' } if (v.__blob) return '{__blob} → images/canvas' if (v.__idb) return '{__idb} → images/*' if (v.__cache) return '{__cache}' return '其它对象' } const inUrl = {} const inData = {} let refs = 0 const host = {} for (const f of files) { const j = readJson(path.join(cvDir, f), null) if (!j) continue const ws = Array.isArray(j) ? j : (j.workspaces || [j]) for (const w of ws) { for (const n of (w && w.nodes) || []) { if (!n || n.type !== 'ref') continue refs++ const d = n.data || {} const a = shape(d.url); inUrl[a] = (inUrl[a] || 0) + 1 const b = shape(d.data); inData[b] = (inData[b] || 0) + 1 if (typeof d.url === 'string' && /^https?:/i.test(d.url)) { let h = '?' try { h = new URL(d.url).host } catch (e) { /* ignore */ } host[h] = (host[h] || 0) + 1 } } } } console.log('④ 画布存档 ' + files.length + ' 个 · 参考图节点 ' + refs + ' 个') console.log(' data.url :' + JSON.stringify(inUrl)) console.log(' data.data:' + JSON.stringify(inData)) const hosts = Object.keys(host).sort((a, b) => host[b] - host[a]).slice(0, 6) if (hosts.length) console.log(' data.url 远程域名 top:' + hosts.map((h) => h + '×' + host[h]).join(' · ')) console.log('') } console.log('结论提示:') console.log(' · data.url 里 http 占多数 ⇒ 这些节点不能标注/吃不到上游,需要"素材化"落本地') console.log(' · data.data 里 {__blob} ⇒ 桌面端字节在 images/canvas/,由 wCleanIdb 按引用集回收') console.log(' · 图库条目 origIdb 覆盖率高 ⇒ 插入"原图档"可零拷贝引用,无需再复制一份') if (orphanAll) console.log(' · 存在 ' + orphanAll + ' 个孤儿文件(可被 wCleanIdb 回收,或由用户手动清理)')