92 lines
2.7 KiB
TypeScript
92 lines
2.7 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
||
import { getServerSession } from 'next-auth'
|
||
import { authOptions } from '@/lib/auth'
|
||
import { prisma } from '@/lib/prisma'
|
||
import { enqueueVideoTranscodeTask, USER_TRANSCODE_FAILURE_MESSAGE } from '@/lib/video-transcode-task'
|
||
|
||
// 重试转码API
|
||
export async function POST(
|
||
request: NextRequest,
|
||
{ params }: { params: { id: string } }
|
||
) {
|
||
try {
|
||
const session = await getServerSession(authOptions)
|
||
if (!session?.user || !(session.user as any).id) {
|
||
return NextResponse.json({ error: '请先登录' }, { status: 401 })
|
||
}
|
||
|
||
const materialId = params.id
|
||
|
||
// 检查素材是否存在
|
||
const material = await prisma.material.findUnique({
|
||
where: { id: materialId },
|
||
select: {
|
||
id: true,
|
||
userId: true,
|
||
videoProcessingStatus: true,
|
||
videoSourceUrl: true,
|
||
videoCoverUrl: true,
|
||
}
|
||
})
|
||
|
||
if (!material) {
|
||
return NextResponse.json({ error: '素材不存在' }, { status: 404 })
|
||
}
|
||
|
||
// 检查权限:只有素材上传者或管理员可以重试转码
|
||
const userId = (session.user as any).id;
|
||
const user = await prisma.user.findUnique({
|
||
where: { id: userId },
|
||
select: { role: true }
|
||
})
|
||
|
||
if (material.userId !== userId && user?.role !== 'ADMIN') {
|
||
return NextResponse.json({ error: '没有权限重试转码' }, { status: 403 })
|
||
}
|
||
|
||
// 检查是否有视频源文件
|
||
if (!material.videoSourceUrl) {
|
||
return NextResponse.json({ error: '该素材没有视频源文件' }, { status: 400 })
|
||
}
|
||
|
||
// 更新转码状态为processing,重置错误信息
|
||
await prisma.material.update({
|
||
where: { id: materialId },
|
||
data: {
|
||
videoProcessingStatus: 'processing',
|
||
transcodeLastError: null,
|
||
transcodeAttempts: 0
|
||
}
|
||
})
|
||
|
||
try {
|
||
await enqueueVideoTranscodeTask(
|
||
materialId,
|
||
material.videoSourceUrl,
|
||
!material.videoCoverUrl
|
||
)
|
||
} catch (enqueueError) {
|
||
console.error('[RetryTranscode] Enqueue failed:', {
|
||
materialId,
|
||
error: enqueueError,
|
||
})
|
||
await prisma.material.update({
|
||
where: { id: materialId },
|
||
data: {
|
||
videoProcessingStatus: 'failed',
|
||
transcodeLastError: USER_TRANSCODE_FAILURE_MESSAGE,
|
||
},
|
||
})
|
||
return NextResponse.json({ error: USER_TRANSCODE_FAILURE_MESSAGE }, { status: 500 })
|
||
}
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
message: '已重新提交转码任务'
|
||
})
|
||
} catch (error) {
|
||
console.error('重试转码失败:', error)
|
||
return NextResponse.json({ error: '重试转码失败' }, { status: 500 })
|
||
}
|
||
}
|