diff --git a/server/api/gallery/[id].js b/server/api/gallery/[id].js index ce6e79c..8101ba5 100644 --- a/server/api/gallery/[id].js +++ b/server/api/gallery/[id].js @@ -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') diff --git a/server/api/gallery/index.js b/server/api/gallery/index.js index b1cf65a..760bff3 100644 --- a/server/api/gallery/index.js +++ b/server/api/gallery/index.js @@ -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)