URI安全编码解码

This commit is contained in:
2023-12-21 19:01:35 +08:00
parent 9287e3dbc7
commit a2c8eedb2f
2 changed files with 8 additions and 2 deletions

View File

@ -11,7 +11,12 @@ export default defineEventHandler(async event => {
// 处理 GET 请求
if (event.node.req.method === 'GET') {
const { id } = event.context.params
const id = decodeURI(event.context.params.id)
// 检查文件是否存在
if (!fs.existsSync(path.join(process.cwd(), 'data/gallery/image', id))) {
return { error: 'file not found' }
}
// 浏览器缓存一个月
event.node.res.setHeader('Cache-Control', 'max-age=2629746,immutable')

View File

@ -23,8 +23,9 @@ export default defineEventHandler(async event => {
const dirpath = await fs.promises.readdir(path.join(process.cwd(), 'data/gallery/image'))
const list = await Promise.all(dirpath.map(async file => {
const file_path = path.join(process.cwd(), 'data/gallery/image', file)
console.log('file_path:', file_path)
const dimensions = imageSize(await fs.promises.readFile(file_path))
const image = '/api/gallery/' + file
const image = encodeURI('/api/gallery/' + file)
return { image, ...dimensions }
}))
console.log('list:', list)