import { NextRequest, NextResponse } from 'next/server' import { getServerSession } from 'next-auth' import { authOptions } from '@/lib/auth' import { transcodeQueue } from '@/lib/video-transcode-queue' export const dynamic = 'force-dynamic' const DEFAULT_PREVIEW_LIMIT = 20 const MAX_PREVIEW_LIMIT = 100 export async function GET(request: NextRequest) { try { const session = await getServerSession(authOptions) const role = (session?.user as unknown as { role?: string } | null)?.role if (!session || role !== 'ADMIN') { return NextResponse.json({ error: '无权限' }, { status: 403 }) } const { searchParams } = new URL(request.url) const limitParam = searchParams.get('limit') let previewLimit = Number.parseInt(limitParam || '', 10) if (Number.isNaN(previewLimit) || previewLimit <= 0) { previewLimit = DEFAULT_PREVIEW_LIMIT } else { previewLimit = Math.min(previewLimit, MAX_PREVIEW_LIMIT) } const status = transcodeQueue.getStatus() const processingPreview = status.processingIds.slice(0, previewLimit) const waitingPreview = status.waiting.slice(0, previewLimit) const progressSnapshot = Array.from(status.progress.entries()) .slice(0, previewLimit) .map(([materialId, value]) => ({ materialId, progress: value, stage: status.stages.get(materialId) ?? null, })) return NextResponse.json({ queueLength: status.queueLength, processingCount: status.processingCount, maxConcurrent: status.maxConcurrent, processingPreview, waitingPreview, previewLimit, timestamp: new Date().toISOString(), progress: progressSnapshot, }) } catch (error) { console.error('[Admin] Video transcode queue status error:', error) return NextResponse.json({ error: '获取转码队列状态失败' }, { status: 500 }) } }