64 lines
1.5 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 { 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) {
try {
// 验证用户身份
const session = await getServerSession(authOptions)
if (!session) {
return NextResponse.json({ error: '请先登录' }, { status: 401 })
}
// 解析请求参数
const { uploadId } = await req.json()
if (!uploadId) {
return NextResponse.json(
{ error: '缺少uploadId参数' },
{ status: 400 }
)
}
// 刷新会话TTL
const success = await refreshUploadSession(uploadId)
if (!success) {
return NextResponse.json(
{ error: '会话不存在或已过期' },
{ status: 404 }
)
}
return NextResponse.json({
success: true,
uploadId,
ttl: 8 * 60 * 60 // 8小时(秒)
})
} catch (error) {
console.error('[RefreshSession] 刷新会话失败:', error)
return NextResponse.json(
{ error: '刷新会话失败' },
{ status: 500 }
)
}
}