chenpei 5ef70bce4e fix: 转码按正式目录找源文件,失败只给用户可重试提示
把 /uploads 网址路径落到 UPLOAD_DIR,避免误判文件不存在。
页面和接口不再回传路径或堆栈,只提示稍后重试。
2026-08-17 15:24:13 +08:00

92 lines
2.7 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'
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 })
}
}