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,
|
||||
|
||||
Reference in new issue
Block a user