This commit is contained in:
Last
2025-03-07 02:26:17 +08:00
parent 8c8115ff2d
commit b0c1c39ad1

View File

@@ -32,7 +32,7 @@ export default defineEventHandler(async event => {
const img = await event.context.db.Gallery.findOne({ where: { id } }) const img = await event.context.db.Gallery.findOne({ where: { id } })
event.node.res.setHeader('Cache-Control', 'max-age=2629746,immutable') event.node.res.setHeader('Cache-Control', 'max-age=2629746,immutable')
event.node.res.setHeader('Content-Type', img.mimetype) event.node.res.setHeader('Content-Type', img.mimetype)
return fs.readFileSync(path.resolve(dirRoot, img.path)) return fs.readFileSync(img.path)
} }
// 请求缩略图 // 请求缩略图
@@ -42,13 +42,13 @@ export default defineEventHandler(async event => {
// 如果缩略图不存在则生成 // 如果缩略图不存在则生成
if (!fs.existsSync(path.resolve(tmpPath, name))) { if (!fs.existsSync(path.resolve(tmpPath, name))) {
const img = await event.context.db.Gallery.findOne({ where: { id } }) const img = await event.context.db.Gallery.findOne({ where: { id } })
await webp.cwebp(path.resolve(dirRoot, img.path), path.resolve(tmpPath, name), `-lossless -resize ${w} 0`) await webp.cwebp(img.path, path.resolve(tmpPath, name), `-lossless -resize ${w} 0`)
// 如果缩略图比原图文件大则替换为原图 // 如果缩略图比原图文件大则替换为原图
const originSize = fs.statSync(path.resolve(dirRoot, img.path)).size const originSize = fs.statSync(img.path).size
const thumbSize = fs.statSync(path.resolve(tmpPath, name)).size const thumbSize = fs.statSync(path.resolve(tmpPath, name)).size
if (thumbSize > originSize) { if (thumbSize > originSize) {
fs.unlinkSync(path.resolve(tmpPath, name)) fs.unlinkSync(path.resolve(tmpPath, name))
fs.copyFileSync(path.resolve(dirRoot, img.path), path.resolve(tmpPath, name)) fs.copyFileSync(img.path, path.resolve(tmpPath, name))
} }
} }
event.node.res.setHeader('Cache-Control', 'max-age=2629746,immutable') event.node.res.setHeader('Cache-Control', 'max-age=2629746,immutable')
@@ -105,7 +105,7 @@ export default defineEventHandler(async event => {
const rest = img.destroy() const rest = img.destroy()
// 后删除源文件和缩略图缓存 // 后删除源文件和缩略图缓存
fs.unlinkSync(path.resolve(dirRoot, img.path)) fs.unlinkSync(img.path)
// 正则查找 img.id 开头 .webp 结尾的缩略图, 例如 1@w200.webp // 正则查找 img.id 开头 .webp 结尾的缩略图, 例如 1@w200.webp
const thumb = fs.readdirSync(tmpPath).filter(x => { const thumb = fs.readdirSync(tmpPath).filter(x => {