first commit
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getFileByObjectName } from '@/lib/fileStorage'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { withCors } from '@/lib/middleware'
|
||||
|
||||
async function handleScreenshotFile(req: NextRequest) {
|
||||
try {
|
||||
const pathSegments = req.nextUrl.pathname.split('/')
|
||||
const fileIdIndex = pathSegments.indexOf('screenshots') + 1
|
||||
const fileId = pathSegments[fileIdIndex]
|
||||
|
||||
if (!fileId) {
|
||||
return NextResponse.json({ error: '缺少文件ID' }, { status: 400 })
|
||||
}
|
||||
|
||||
// 从数据库查找 objectName
|
||||
const screenshot = await prisma.screenshot.findFirst({
|
||||
where: { fileId },
|
||||
select: { objectName: true }
|
||||
})
|
||||
|
||||
if (!screenshot) {
|
||||
return NextResponse.json({ error: '截图不存在' }, { status: 404 })
|
||||
}
|
||||
|
||||
const file = await getFileByObjectName(screenshot.objectName)
|
||||
if (!file) {
|
||||
return NextResponse.json({ error: '文件不存在' }, { status: 404 })
|
||||
}
|
||||
|
||||
return new NextResponse(file.buffer, {
|
||||
headers: {
|
||||
'Content-Type': file.contentType || 'image/webp',
|
||||
'Cache-Control': 'public, max-age=31536000',
|
||||
}
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('获取截图失败:', error)
|
||||
return NextResponse.json({ error: '获取截图失败' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
export const GET = withCors(handleScreenshotFile)
|
||||
Reference in New Issue
Block a user