fix(images): 缩略图 MIME 魔数嗅探 + 取消在用 objectURL 撤销 + 缩略图回收(v3.25.78/79)
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 25s
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 25s
This commit is contained in:
1 parent
f02b3dcec2
commit
2860faacbb
17 files changed
+170
-40
No files matched your search
+74
-11
@@ -897,14 +897,37 @@
|
||||
}
|
||||
|
||||
// 画布显示缩略图(768px)落盘:重启后直接命中,不用重新解码 4K 原图
|
||||
// v3.25.78:缩略图可能是 webp(新)或 jpg(老),按扩展名决定 MIME。
|
||||
// 旧代码把 WebP 字节写进 _768.jpg 名字里 ⇒ dataURL 声明成 image/jpeg,属于格式错配。
|
||||
// v3.25.78:缩略图文件名曾把 WebP 写成 .jpg(老 bug),所以 MIME 必须按**魔数**判断,
|
||||
// 不能只看扩展名,否则 <img> 拿到错 MIME 的 blob 可能渲染成黑块。
|
||||
function sniffMime (u8) {
|
||||
try {
|
||||
if (!u8 || u8.length < 12) return ''
|
||||
if (u8[0] === 0xFF && u8[1] === 0xD8 && u8[2] === 0xFF) return 'image/jpeg'
|
||||
if (u8[0] === 0x89 && u8[1] === 0x50 && u8[2] === 0x4E && u8[3] === 0x47) return 'image/png'
|
||||
if (u8[0] === 0x52 && u8[1] === 0x49 && u8[2] === 0x46 && u8[3] === 0x46 &&
|
||||
u8[8] === 0x57 && u8[9] === 0x45 && u8[10] === 0x42 && u8[11] === 0x50) return 'image/webp'
|
||||
if (u8[0] === 0x47 && u8[1] === 0x49 && u8[2] === 0x46) return 'image/gif'
|
||||
} catch (e) { /* ignore */ }
|
||||
return ''
|
||||
}
|
||||
|
||||
async function thumbFileOf (hash) {
|
||||
const base = await join(dir, IMG_DIR, 'thumbs', String(hash) + '_768')
|
||||
for (const ext of ['.webp', '.jpg']) {
|
||||
try { if (await fs().exists(base + ext)) return { path: base + ext, mime: ext === '.webp' ? 'image/webp' : 'image/jpeg' } } catch (e) { /* ignore */ }
|
||||
}
|
||||
return null
|
||||
}
|
||||
async function readDisplayThumb(hash) {
|
||||
try {
|
||||
if (!enabled() || !dir || !hash) return ''
|
||||
const abs = await join(dir, IMG_DIR, 'thumbs', String(hash) + '_768.jpg')
|
||||
if (!(await fs().exists(abs))) return ''
|
||||
const u8 = await fs().readFile(abs)
|
||||
const f = await thumbFileOf(hash)
|
||||
if (!f) return ''
|
||||
const u8 = await fs().readFile(f.path)
|
||||
if (!u8 || !u8.length) return ''
|
||||
return bytesToDataUrl('image/jpeg', u8)
|
||||
return bytesToDataUrl(sniffMime(u8) || f.mime, u8)
|
||||
} catch (e) { return '' }
|
||||
}
|
||||
// v3.25.73:缩略图以 Blob + objectURL 交给显示层(避免 dataURL 常驻 JS 堆)
|
||||
@@ -914,21 +937,61 @@
|
||||
if (!enabled() || !dir || !hash) return ''
|
||||
const hit = thumbUrlCache.get(String(hash))
|
||||
if (hit) return hit
|
||||
const abs = await join(dir, IMG_DIR, 'thumbs', String(hash) + '_768.jpg')
|
||||
if (!(await fs().exists(abs))) return ''
|
||||
const u8 = await fs().readFile(abs)
|
||||
const f = await thumbFileOf(hash)
|
||||
if (!f) return ''
|
||||
const u8 = await fs().readFile(f.path)
|
||||
if (!u8 || !u8.length) return ''
|
||||
const url = URL.createObjectURL(new Blob([u8], { type: 'image/jpeg' }))
|
||||
const url = URL.createObjectURL(new Blob([u8], { type: sniffMime(u8) || f.mime }))
|
||||
thumbUrlCache.set(String(hash), url)
|
||||
if (thumbUrlCache.size > 400) { const k = thumbUrlCache.keys().next().value; try { URL.revokeObjectURL(thumbUrlCache.get(k)) } catch (e) {} ; thumbUrlCache.delete(k) }
|
||||
// 不 revoke:这个 URL 可能正在被画布上的 <img> 使用,撤销会变成黑块/破图。
|
||||
if (thumbUrlCache.size > 600) { const k = thumbUrlCache.keys().next().value; try { thumbUrlCache.delete(k) } catch (e) { /* ignore */ } }
|
||||
return url
|
||||
} catch (e) { return '' }
|
||||
}
|
||||
// v3.25.78:缩略图没有回收机制,历史上会无限增长(实测 470 个 / 67MB)。
|
||||
// 这里按总大小封顶(默认 300MB,超了删最老的 20%),缩略图可再生,删掉不影响数据。
|
||||
async function pruneThumbs (maxMB) {
|
||||
try {
|
||||
if (!enabled() || !dir) return 0
|
||||
const cap = Math.max(50, Number(maxMB) || 300) * 1024 * 1024
|
||||
const tdir = await join(dir, IMG_DIR, 'thumbs')
|
||||
if (!(await fs().exists(tdir))) return 0
|
||||
const names = await fs().readDir(tdir)
|
||||
const files = []
|
||||
let total = 0
|
||||
for (const e of names) {
|
||||
const name = e && (e.name || e)
|
||||
if (!name || !/_768\.(webp|jpg)$/i.test(String(name))) continue
|
||||
const abs = await join(tdir, String(name))
|
||||
try {
|
||||
const st = await fs().stat(abs)
|
||||
const size = (st && st.size) || 0
|
||||
total += size
|
||||
files.push({ abs, size, mtime: (st && st.mtime) ? new Date(st.mtime).getTime() : 0 })
|
||||
} catch (err) { /* ignore */ }
|
||||
}
|
||||
if (total <= cap) return 0
|
||||
files.sort((a, b) => a.mtime - b.mtime)
|
||||
const target = cap * 0.8
|
||||
let removed = 0
|
||||
for (const f of files) {
|
||||
if (total <= target) break
|
||||
try { await fs().remove(f.abs); total -= f.size; removed++ } catch (e) { /* ignore */ }
|
||||
}
|
||||
return removed
|
||||
} catch (e) { return 0 }
|
||||
}
|
||||
|
||||
async function writeDisplayThumbBlob(hash, blob) {
|
||||
try {
|
||||
if (!enabled() || !dir || !hash || !blob) return false
|
||||
const abs = await join(dir, IMG_DIR, 'thumbs', String(hash) + '_768.jpg')
|
||||
const isWebp = String(blob.type || '').indexOf('webp') >= 0
|
||||
const abs = await join(dir, IMG_DIR, 'thumbs', String(hash) + '_768' + (isWebp ? '.webp' : '.jpg'))
|
||||
if (await fs().exists(abs)) return true
|
||||
try {
|
||||
const other = await join(dir, IMG_DIR, 'thumbs', String(hash) + '_768' + (isWebp ? '.jpg' : '.webp'))
|
||||
if (await fs().exists(other)) return true
|
||||
} catch (e) { /* ignore */ }
|
||||
await fs().mkdir(await join(dir, IMG_DIR, 'thumbs'), { recursive: true })
|
||||
const buf = new Uint8Array(await blob.arrayBuffer())
|
||||
await fs().writeFile(abs, buf)
|
||||
@@ -1034,7 +1097,7 @@
|
||||
}
|
||||
|
||||
global.IWBStorageFile = {
|
||||
enabled, ready: init, flush, runtime, pickDir, switchTo, migrateTo, gcImages, rotateTrash, galleryThumb, readDisplayThumb, readDisplayThumbUrl, writeDisplayThumb, writeDisplayThumbBlob,
|
||||
enabled, ready: init, flush, runtime, pickDir, switchTo, migrateTo, gcImages, rotateTrash, galleryThumb, readDisplayThumb, readDisplayThumbUrl, writeDisplayThumb, writeDisplayThumbBlob, pruneThumbs,
|
||||
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 },
|
||||
storage: () => storage,
|
||||
|
||||
@@ -41,23 +41,23 @@
|
||||
// 外观实时预览:draft 变化 → 立刻写 CSS 变量 + 通知画布重渲染;关闭未保存则回滚
|
||||
function useAppearancePreview (opts) {
|
||||
const o = opts || {}
|
||||
const savedRef = React.useRef((o.savedConfig && o.savedConfig.canvas && o.savedConfig.canvas.appearance) || null)
|
||||
const savedRef = React.useRef(o.savedConfig || o.config || null)
|
||||
const themeRef = React.useRef(o.dark ? 'dark' : 'light')
|
||||
React.useEffect(() => { themeRef.current = o.dark ? 'dark' : 'light' }, [o.dark])
|
||||
React.useEffect(() => {
|
||||
if (!o.enabled) return
|
||||
const A = global.IWBUIApearance
|
||||
if (!A || typeof A.apply !== 'function') return
|
||||
try { A.apply(o.appearance, !!(o.dark)) } catch (e) { /* ignore */ }
|
||||
try { A.apply(o.config, !!(o.dark)) } catch (e) { /* ignore */ }
|
||||
try { global.dispatchEvent(new Event('iwb:appearance')) } catch (e) { /* ignore */ }
|
||||
}, [o.enabled, o.dark, o.appearance])
|
||||
}, [o.enabled, o.dark, o.config])
|
||||
React.useEffect(() => () => {
|
||||
const A = global.IWBUIApearance
|
||||
if (!A || typeof A.apply !== 'function') return
|
||||
try { A.apply(savedRef.current, themeRef.current === 'dark') } catch (e) { /* ignore */ }
|
||||
try { global.dispatchEvent(new Event('iwb:appearance')) } catch (e) { /* ignore */ }
|
||||
}, [])
|
||||
return { markSaved: () => { savedRef.current = o.appearance || null } }
|
||||
return { markSaved: () => { savedRef.current = o.config || null } }
|
||||
}
|
||||
|
||||
global.IWBUIModalDrag = { useDraggableModal, useAppearancePreview }
|
||||
|
||||
@@ -692,7 +692,7 @@
|
||||
return el('div', { className: 'iwb-node-body iwb-ref-body' },
|
||||
hasImg
|
||||
? el('div', { className: 'iwb-ref-show' },
|
||||
el('img', { className: 'iwb-ref-single', loading: 'lazy', decoding: 'async', fetchPriority: 'low', src: wPreviewUrlOf((typeof wImgSrcForNode === 'function' ? wImgSrcForNode : wDisplayThumbOf)(wThumbSrc(src), Math.min(320, (props.node && props.node.w) || 280))), alt: '图片', draggable: false, title: '双击放大 · 单击可拖动' }),
|
||||
el('img', { className: 'iwb-ref-single', decoding: 'async', fetchPriority: 'low', src: wPreviewUrlOf((typeof wImgSrcForNode === 'function' ? wImgSrcForNode : wDisplayThumbOf)(wThumbSrc(src), Math.min(320, (props.node && props.node.w) || 280))), alt: '图片', draggable: false, onError: (e) => { try { const ov = wThumbSrc(src); if (ov && e.currentTarget && e.currentTarget.src !== ov) e.currentTarget.src = ov } catch (err) { /* ignore */ } }, title: '双击放大 · 单击可拖动' }),
|
||||
(typeof d.url === 'string' && d.url) ? el('div', { className: 'iwb-ref-url-current', title: d.url }, 'URL 图片') : null,
|
||||
)
|
||||
: el('div', { className: 'iwb-ref-emptybox', title: '单击选中 · 双击上传图片', onDoubleClick: (e) => { e.stopPropagation(); if (refFileInput) refFileInput.click() } },
|
||||
@@ -830,7 +830,7 @@
|
||||
// React 会按下标复用同一个 DOM 节点,先显示上一张、再等新图解码 ⇒ 肉眼看到「闪一下旧图/空图」。
|
||||
// url 稳定 ⇒ 节点复用(不重建、不重新解码);url 变了才新建,内容永远对得上。
|
||||
return el('div', { key: String(r.url || '') + '|' + ri, className: 'iwb-out-cell' },
|
||||
el('img', { key: 'out-img-' + cur + '-' + String(r.url || '').slice(-48), className: 'iwb-out-cell-img', loading: 'lazy', decoding: 'async', fetchPriority: 'low', referrerPolicy: 'no-referrer', src: wPreviewUrlOf((typeof wImgSrcForNode === 'function' ? wImgSrcForNode : wDisplayThumbOf)(thumbOf(r), Math.max(120, Math.min(360, (node && node.w ? node.w - 40 : 240))))), draggable: false, style: { aspectRatio: (r.width && r.height) ? String(r.width) + ' / ' + String(r.height) : undefined }, title: r.idb && r.data ? '缩略图 · 点击查看原图' : '点击放大(可左右切换)', onError: (e) => { if (e.currentTarget && e.currentTarget.src !== r.url) e.currentTarget.src = r.url }, onClick: () => openPreviewAt(cur) }),
|
||||
el('img', { key: 'out-img-' + cur + '-' + String(r.url || '').slice(-48), className: 'iwb-out-cell-img', decoding: 'async', fetchPriority: 'low', referrerPolicy: 'no-referrer', src: wPreviewUrlOf((typeof wImgSrcForNode === 'function' ? wImgSrcForNode : wDisplayThumbOf)(thumbOf(r), Math.max(120, Math.min(360, (node && node.w ? node.w - 40 : 240))))), draggable: false, style: { aspectRatio: (r.width && r.height) ? String(r.width) + ' / ' + String(r.height) : undefined }, title: r.idb && r.data ? '缩略图 · 点击查看原图' : '点击放大(可左右切换)', onError: (e) => { if (e.currentTarget && e.currentTarget.src !== r.url) e.currentTarget.src = r.url }, onClick: () => openPreviewAt(cur) }),
|
||||
el('div', { className: 'iwb-out-cell-meta' }, (r.width || '?') + '×' + (r.height || '?')),
|
||||
el('div', { className: 'iwb-out-cell-actions' },
|
||||
el('button', { className: 'iwb-btn iwb-btn-sm', title: '插入为参考图节点', onClick: () => props.onCopyRef && props.onCopyRef(r.url), onPointerDown: stop }, I('copy')),
|
||||
@@ -1152,7 +1152,7 @@
|
||||
if (src) {
|
||||
return el('div', { className: 'iwb-node-body iwb-node-body-lod iwb-lod-lite iwb-lod-img' },
|
||||
// 缩略图不参与点击(节点本体负责选中/拖动),所以不挂事件;alt 留空由 CSS 兜底尺寸
|
||||
el('img', { className: 'iwb-lod-thumb', src: wDisplayThumbOf(src), loading: 'lazy', decoding: 'async', draggable: false, alt: '' }),
|
||||
el('img', { className: 'iwb-lod-thumb', src: wDisplayThumbOf(src), decoding: 'async', draggable: false, alt: '' }),
|
||||
)
|
||||
}
|
||||
return el('div', { className: 'iwb-node-body iwb-node-body-lod iwb-lod-lite iwb-lod-img' }, null)
|
||||
|
||||
@@ -562,12 +562,12 @@ function SettingsModal(props) {
|
||||
// 可拖动窗口 + 外观实时预览(实现见 src/ui-modal-drag.js)
|
||||
const MD = (typeof window !== 'undefined') ? window.IWBUIModalDrag : null
|
||||
const dragKit = (MD && MD.useDraggableModal) ? MD.useDraggableModal() : null
|
||||
const appearanceKit = (MD && MD.useAppearancePreview) ? MD.useAppearancePreview({ enabled: tab === 'appearance', appearance: draft && draft.canvas ? draft.canvas.appearance : null, dark: theme === 'dark', savedConfig: props.config || null }) : null
|
||||
const appearanceKit = (MD && MD.useAppearancePreview) ? MD.useAppearancePreview({ enabled: tab === 'appearance', config: draft, dark: theme === 'dark', savedConfig: props.config || null }) : null
|
||||
const winStyle = dragKit ? dragKit.style : undefined
|
||||
const onHeadPointerDown = dragKit ? dragKit.onHeadPointerDown : null
|
||||
// 顺序:通用放最前(最常用:目录/外观/图片图库/生成),其它按使用频率排
|
||||
const tabs = [['general', '通用'], ['appearance', '外观'], ['gen', '生图服务'], ['llm', 'LLM 服务'], ['img', '图片传输'], ['sizes', '尺寸预设'], ['hotkeys', '快捷键'], ['backup', '备份与还原'], ['diag', '诊断'], ['plugins', '插件']]
|
||||
return el('div', { className: 'iwb-modal-mask', onWheel: (e) => e.stopPropagation(), onPointerDown: (e) => { if (e.target === e.currentTarget) props.onClose() } },
|
||||
return el('div', { className: 'iwb-modal-mask iwb-mask-ghost', onWheel: (e) => e.stopPropagation(), onPointerDown: (e) => { if (e.target === e.currentTarget) props.onClose() } },
|
||||
el('div', { className: 'iwb-modal iwb-settings-modal' + ((dragKit && dragKit.pos) ? ' iwb-modal-floating' : ''), style: winStyle },
|
||||
el('div', { className: 'iwb-modal-head iwb-modal-draghead', onPointerDown: onHeadPointerDown, title: '按住拖动窗口' },
|
||||
el('span', { className: 'iwb-toolbar-title' }, '⚙️ 设置'),
|
||||
|
||||
Reference in new issue
Block a user