feat: generate video
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { withCors } from '@/lib/middleware'
|
||||
import { compressPics } from '@/lib/schedule/compressPics'
|
||||
// 切换记录的星标状态
|
||||
async function handleGenerateVideo(req: NextRequest) {
|
||||
try {
|
||||
// get from query
|
||||
// ?hostname=DESKTOP-JHHNH9C&startTime=1751104800&endTime=1751108400
|
||||
const { searchParams } = req.nextUrl
|
||||
const hostname = searchParams.get('hostname')
|
||||
const startTimeParam = searchParams.get('startTime')
|
||||
const endTimeParam = searchParams.get('endTime')
|
||||
if (!hostname) {
|
||||
return NextResponse.json({ error: '缺少主机名' }, { status: 400 })
|
||||
}
|
||||
if (!startTimeParam) {
|
||||
return NextResponse.json({ error: '缺少开始时间' }, { status: 400 })
|
||||
}
|
||||
if (!endTimeParam) {
|
||||
return NextResponse.json({ error: '缺少结束时间' }, { status: 400 })
|
||||
}
|
||||
|
||||
// 将时间戳从秒转换为毫秒
|
||||
let startTime = new Date(parseInt(startTimeParam) * 1000)
|
||||
let endTime = new Date(parseInt(endTimeParam) * 1000)
|
||||
|
||||
let buffer = await compressPics(hostname, startTime, endTime)
|
||||
|
||||
if (!buffer) {
|
||||
return NextResponse.json({ error: '没有找到符合条件的截图' }, { status: 404 })
|
||||
}
|
||||
|
||||
return new NextResponse(buffer, {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'video/mp4',
|
||||
'Content-Disposition': `attachment; filename="${hostname}-${startTime.toISOString()}-${endTime.toISOString()}.mp4"`,
|
||||
'Cache-Control': 'no-cache, no-store, must-revalidate',
|
||||
'Pragma': 'no-cache',
|
||||
'Expires': '0'
|
||||
}
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('视频生成失败:', error)
|
||||
return NextResponse.json({ error: '操作失败' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
export const GET = withCors(handleGenerateVideo)
|
||||
Reference in New Issue
Block a user