主要改动: 1. 统一使用50MB分片(内网公网一致) - 请求数减少80%(1GB文件从100个减少到20个) - TCP利用率更高,网络开销更低 2. 延长会话TTL:5分钟 → 4小时 - 支持220GB大文件上传 - 解决大文件上传中途失败问题 3. 优化超时时间:支持50MB分片(5-10分钟) 4. 新增会话刷新机制(不清空进度) - 新增 /api/materials/upload/refresh API - 会话过期后保留已上传进度 5. 新增TUS协议迁移方案文档 - docs/TUS协议迁移方案.md Co-Authored-By: Claude <noreply@anthropic.com>
72 lines
2.0 KiB
TypeScript
72 lines
2.0 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
||
import { getServerSession } from 'next-auth'
|
||
import { authOptions } from '@/lib/auth'
|
||
import { refreshUploadSession } from '@/lib/upload-sessions'
|
||
|
||
// Configure route
|
||
export const runtime = 'nodejs'
|
||
export const dynamic = 'force-dynamic'
|
||
|
||
/**
|
||
* 刷新上传会话API
|
||
* 用于会话过期后恢复上传,不清空已上传的分片记录
|
||
*
|
||
* POST /api/materials/upload/refresh
|
||
* Body: { uploadId: string }
|
||
*
|
||
* 返回:
|
||
* - success: true/false
|
||
* - uploadId: string
|
||
* - ttl: number (秒)
|
||
*/
|
||
export async function POST(req: NextRequest) {
|
||
console.log(`[RefreshSession] 收到会话刷新请求 at ${new Date().toISOString()}`)
|
||
|
||
try {
|
||
// 验证用户身份
|
||
const session = await getServerSession(authOptions)
|
||
if (!session) {
|
||
console.log(`[RefreshSession] 用户未登录`)
|
||
return NextResponse.json({ error: '请先登录' }, { status: 401 })
|
||
}
|
||
|
||
// 解析请求参数
|
||
const { uploadId } = await req.json()
|
||
|
||
if (!uploadId) {
|
||
console.log(`[RefreshSession] 缺少uploadId参数`)
|
||
return NextResponse.json(
|
||
{ error: '缺少uploadId参数' },
|
||
{ status: 400 }
|
||
)
|
||
}
|
||
|
||
console.log(`[RefreshSession] 尝试刷新会话: ${uploadId}, 用户: ${(session.user as any).id}`)
|
||
|
||
// 刷新会话TTL
|
||
const success = await refreshUploadSession(uploadId)
|
||
|
||
if (!success) {
|
||
console.log(`[RefreshSession] 会话不存在或已过期: ${uploadId}`)
|
||
return NextResponse.json(
|
||
{ error: '会话不存在或已过期' },
|
||
{ status: 404 }
|
||
)
|
||
}
|
||
|
||
console.log(`[RefreshSession] 会话刷新成功: ${uploadId}, TTL: 4小时`)
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
uploadId,
|
||
ttl: 4 * 60 * 60 // 4小时(秒)
|
||
})
|
||
} catch (error) {
|
||
console.error('[RefreshSession] 刷新会话失败:', error)
|
||
return NextResponse.json(
|
||
{ error: '刷新会话失败' },
|
||
{ status: 500 }
|
||
)
|
||
}
|
||
}
|