This commit is contained in:
freedakgmail
2025-11-24 16:07:24 +08:00
parent 3d075c6076
commit b25f4bea02
426 changed files with 3619 additions and 4468 deletions
+49
View File
@@ -0,0 +1,49 @@
import { NextRequest, NextResponse } from 'next/server'
import { readFile } from 'fs/promises'
import { join } from 'path'
import { existsSync } from 'fs'
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ filename: string }> }
) {
try {
const { filename } = await params
// 构建文件路径
const filePath = join(process.cwd(), 'public', 'uploads', filename)
// 检查文件是否存在
if (!existsSync(filePath)) {
return new NextResponse('File not found', { status: 404 })
}
// 读取文件
const fileBuffer = await readFile(filePath)
// 根据文件扩展名设置 Content-Type
const ext = filename.split('.').pop()?.toLowerCase()
const contentTypeMap: Record<string, string> = {
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'png': 'image/png',
'gif': 'image/gif',
'webp': 'image/webp',
'svg': 'image/svg+xml',
}
const contentType = contentTypeMap[ext || ''] || 'application/octet-stream'
// 返回文件
return new NextResponse(fileBuffer, {
status: 200,
headers: {
'Content-Type': contentType,
'Cache-Control': 'public, max-age=31536000, immutable',
},
})
} catch (error) {
console.error('读取文件失败:', error)
return new NextResponse('Internal Server Error', { status: 500 })
}
}