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: 8小时`)
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
uploadId,
|
||
ttl: 8 * 60 * 60 // 8小时(秒)
|
||
})
|
||
} catch (error) {
|
||
console.error('[RefreshSession] 刷新会话失败:', error)
|
||
return NextResponse.json(
|
||
{ error: '刷新会话失败' },
|
||
{ status: 500 }
|
||
)
|
||
}
|
||
}
|