DEBUG: 获取文件
This commit is contained in:
parent
d27ac6d929
commit
404d859878
@ -1,20 +1,26 @@
|
|||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import webp from 'webp-converter'
|
import webp from 'webp-converter'
|
||||||
//import resize from 'resize-img'
|
|
||||||
|
// 如果当前文件夹是 .output 则使用上一级路径
|
||||||
|
const dir = process.cwd()
|
||||||
|
const dirRoot = (path.basename(dir) === '.output') ? path.resolve(dir, '..') : dir
|
||||||
|
const dirPath = path.resolve(dirRoot, 'data/gallery/image')
|
||||||
|
|
||||||
|
if (!fs.existsSync(dirPath)) {
|
||||||
|
fs.mkdirSync(dirPath, { recursive: true })
|
||||||
|
}
|
||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
|
|
||||||
// 检查 webp 文件夹是否存在, 不存在则创建
|
|
||||||
const webpDir = path.join(process.cwd(), 'data/gallery/webp')
|
|
||||||
if (!fs.existsSync(webpDir)) fs.mkdirSync(webpDir)
|
|
||||||
|
|
||||||
// 处理 GET 请求
|
// 处理 GET 请求
|
||||||
if (event.node.req.method === 'GET') {
|
if (event.node.req.method === 'GET') {
|
||||||
const id = decodeURI(event.context.params.id)
|
const file_name = decodeURI(event.context.params.id)
|
||||||
|
const file_path = path.resolve(dirPath, file_name)
|
||||||
|
|
||||||
// 检查文件是否存在
|
// 检查文件是否存在
|
||||||
if (!fs.existsSync(path.join(process.cwd(), 'data/gallery/image', id))) {
|
if (!fs.existsSync(file_path)) {
|
||||||
|
event.node.res.statusCode = 404
|
||||||
return { error: 'file not found' }
|
return { error: 'file not found' }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,34 +29,17 @@ export default defineEventHandler(async event => {
|
|||||||
|
|
||||||
// 请求文件后缀是否 .webp 格式
|
// 请求文件后缀是否 .webp 格式
|
||||||
if (id.match(/\.webp$/)) {
|
if (id.match(/\.webp$/)) {
|
||||||
const filepath = path.join(process.cwd(), 'data/gallery/webp', id)
|
|
||||||
// 如果文件不存在则创建
|
// 如果文件不存在则创建
|
||||||
if (!fs.existsSync(filepath)) {
|
if (!fs.existsSync(file_path)) {
|
||||||
const filename = id.replace(/\.webp$/, '')
|
const filename = id.replace(/\.webp$/, '')
|
||||||
const origin = path.join(process.cwd(), 'data/gallery/image', filename)
|
const origin = path.join(process.cwd(), 'data/gallery/image', filename)
|
||||||
await webp.cwebp(origin, filepath)
|
await webp.cwebp(origin, file_path)
|
||||||
}
|
}
|
||||||
return fs.readFileSync(filepath)
|
return fs.readFileSync(file_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 请求原图
|
// 请求原图
|
||||||
const filepath = path.join(process.cwd(), 'data/gallery/image', `${id}`)
|
return fs.readFileSync(file_path)
|
||||||
if (!fs.existsSync(filepath)) return { error: 'file not found' }
|
|
||||||
return fs.readFileSync(filepath)
|
|
||||||
|
|
||||||
// 有无缩略图(请求参数中有width和height)
|
|
||||||
//if (width && height) {
|
|
||||||
// //const width = parseInt(event.node.req.query.width)
|
|
||||||
// //const height = parseInt(event.node.req.query.height)
|
|
||||||
// //const thumbpath = path.join(process.cwd(), 'data/thumb', `${id}-${width}x${height}.webp`)
|
|
||||||
// //if (fs.existsSync(thumb[...])) {
|
|
||||||
// // return fs.readFileSync(thumb[...])
|
|
||||||
// //} else {
|
|
||||||
// // const img = resize(fs.readFileSync(filepath), { width, height })
|
|
||||||
// // const webp = webp.buffer(img)
|
|
||||||
// // fs.writeFileSync(thumbpath, webp)
|
|
||||||
// // return webp
|
|
||||||
// //}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user