chenpei cd6e40d9d2 优化:网络传输性能和稳定性 - TCP缓冲区+并发控制优化
主要改动:
1. TCP缓冲区优化(16MB)- 解决公网上传速度慢问题
2. 并发数降低到1-3 - 避免4并发导致ERR_CONNECTION_RESET
3. 动态并发控制 - 根据网络环境自动调整
4. Nginx超时增加到120s - 支持大分片上传
5. 上传进度修复 - 修复剩余时间计算
6. 新增任务-页面上传器和详情组件
7. 统一部署文档为deploy-remote-build.ps1

问题修复:
- 公网用户上传从200-900KB/s提升到预期3-5MB/s
- 内网用户上传更稳定,无连接重置错误
- UploadProgress组件剩余时间计算修正

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-27 09:33:09 +08:00

99 lines
3.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'
import { processPdfUpload } from '@/lib/pdf-upload-handler'
import { readFile } from 'fs/promises'
export const runtime = 'nodejs'
export const dynamic = 'force-dynamic'
export async function POST(request: NextRequest) {
try {
// 验证管理员权限(支持API密钥或会话)
const apiKey = request.headers.get('x-api-key')
const session = await getServerSession(authOptions)
// API密钥或会话任一有效即可
const isValidApiKey = apiKey === process.env.ADMIN_API_KEY
const isValidSession = session && (session.user as any).id
if (!isValidApiKey && !isValidSession) {
return NextResponse.json({ error: '未授权' }, { status: 401 })
}
const body = await request.json()
const { materialId } = body
if (!materialId) {
return NextResponse.json({ error: '缺少 materialId' }, { status: 400 })
}
console.log('[Regenerate PDF Preview] Material ID:', materialId)
// 获取素材详情
const material = await prisma.material.findUnique({
where: { id: materialId },
})
if (!material) {
return NextResponse.json({ error: '素材不存在' }, { status: 404 })
}
if (material.materialType !== '品牌VI灵感') {
return NextResponse.json({ error: '此API仅适用于品牌VI灵感类型素材' }, { status: 400 })
}
// 检查本地PDF文件是否存在
if (!material.zipFilePath) {
return NextResponse.json({ error: '本地PDF文件路径不存在' }, { status: 400 })
}
try {
// 检查文件是否存在
await readFile(material.zipFilePath)
} catch {
return NextResponse.json({ error: `PDF文件不存在: ${material.zipFilePath}` }, { status: 404 })
}
console.log('[Regenerate PDF Preview] PDF file path:', material.zipFilePath)
// 提取PDF预览图
const pdfResult = await processPdfUpload(material.zipFilePath, '')
console.log('[Regenerate PDF Preview] PDF预览图提取结果:', {
success: pdfResult.success,
hasPreviewUrl: !!pdfResult.previewUrl,
previewUrl: pdfResult.previewUrl,
error: pdfResult.error
})
if (!pdfResult.success || !pdfResult.previewUrl) {
const errorMsg = pdfResult.error || 'PDF预览图提取失败'
console.error('[Regenerate PDF Preview] ❌ PDF预览图提取失败:', errorMsg)
return NextResponse.json({ error: errorMsg }, { status: 500 })
}
// 更新素材的 previewUrl
await prisma.material.update({
where: { id: materialId },
data: {
previewUrl: pdfResult.previewUrl,
},
})
console.log('[Regenerate PDF Preview] ✅ Updated material.previewUrl:', pdfResult.previewUrl)
return NextResponse.json({
success: true,
previewUrl: pdfResult.previewUrl,
})
} catch (error) {
console.error('[Regenerate PDF Preview] Error:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : '服务器错误' },
{ status: 500 }
)
}
}