diff --git a/.gitignore b/.gitignore index 4f2257d..56645de 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ node_modules/ .next/ out/ -uploads/ +/uploads/ *.log .DS_Store @@ -9,3 +9,4 @@ uploads/ # 仅忽略本地覆盖文件 .env.local .env.*.local +.vercel diff --git a/app/api/uploads/[name]/route.js b/app/api/uploads/[name]/route.js new file mode 100644 index 0000000..536dc55 --- /dev/null +++ b/app/api/uploads/[name]/route.js @@ -0,0 +1,37 @@ +// GET /api/uploads/[name] — 提供上传的图片文件(本地存储模式) +// (next.config.js 中 rewrites 把 /uploads/* 指向这里,URL 保持 /uploads/... 不变) +// S3 模式下走 S3_PUBLIC_URL 直接访问,此路由不生效(rewrite 仍指向但返回 404 前可跳过) +import path from 'path'; +import { getObject, STORAGE_TYPE } from '@/lib/storage'; + +export const runtime = 'nodejs'; + +const MIME = { + '.png': 'image/png', + '.jpg': 'image/jpeg', + '.jpeg': 'image/jpeg', + '.gif': 'image/gif', + '.webp': 'image/webp', +}; + +export async function GET(req, { params }) { + const name = params.name; + // 防止路径穿越 + if (!/^[\w-]+\.\w+$/.test(name)) { + return new Response('Not Found', { status: 404 }); + } + // S3 模式下文件不在本地,由 S3_PUBLIC_URL 提供 + if (STORAGE_TYPE === 's3') { + return new Response('Not Found', { status: 404 }); + } + try { + const data = await getObject(name); + if (!data) return new Response('Not Found', { status: 404 }); + const ext = path.extname(name).toLowerCase(); + return new Response(new Uint8Array(data), { + headers: { 'Content-Type': MIME[ext] || 'application/octet-stream' }, + }); + } catch { + return new Response('Not Found', { status: 404 }); + } +} diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000..2d49ae4 --- /dev/null +++ b/vercel.json @@ -0,0 +1,9 @@ +{ + "framework": "nextjs", + "regions": ["hkg1"], + "buildCommand": "next build", + "outputDirectory": ".next", + "env": { + "NODE_ENV": "production" + } +}