26 lines
1.5 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { isAllowedCorsOrigin } from '@/lib/cors-origin'
import { isSuperAdmin } from '@/lib/department-permission'
import { collectXiaoyiDeliveryGarbage } from '@/lib/xiaoyi-delivery-gc'
export async function POST(request: NextRequest) {
const session = await getServerSession(authOptions)
const userId = (session?.user as { id?: string } | undefined)?.id
if (!userId) return NextResponse.json({ success: false, error: '未登录' }, { status: 401 })
const origin = request.headers.get('origin')
if (!await isSuperAdmin(userId) || !origin || request.headers.get('sec-fetch-site') === 'cross-site'
|| !isAllowedCorsOrigin(request, origin)) {
return NextResponse.json({ success: false, error: '仅超级管理员可从素材库操作' }, { status: 403 })
}
const body = await request.json().catch(() => null)
if (!body || (body.kind !== 'deliveries' && body.kind !== 'uploads')
|| (body.dryRun !== undefined && typeof body.dryRun !== 'boolean')
|| (body.after !== undefined && (typeof body.after !== 'string' || !/^[A-Za-z0-9_/-]{1,265}$/.test(body.after)))) {
return NextResponse.json({ success: false, error: '清理参数无效' }, { status: 400 })
}
const result = await collectXiaoyiDeliveryGarbage(body)
return NextResponse.json({ success: true, ...result }, { headers: { 'Cache-Control': 'no-store' } })
}