73 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextRequest, NextResponse } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { prisma } from '@/lib/prisma'
// 重试转码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
}
})
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
}
})
// TODO: 这里应该触发实际的转码任务
// 由于转码服务是后台运行的,这里只是更新状态
// 实际的转码任务会由后台服务自动检测并处理
return NextResponse.json({
success: true,
message: '已重新提交转码任务'
})
} catch (error) {
console.error('重试转码失败:', error)
return NextResponse.json({ error: '重试转码失败' }, { status: 500 })
}
}