flnxi-Wu/app/api/ai/edit/route.ts
2026-05-28 16:08:49 +08:00

76 lines
2.2 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 { featureFlags } from '@/lib/config'
import { prisma } from '@/lib/prisma'
import { canManageMaterial, getMaterialPermissionActor } from '@/lib/material-permissions'
async function ensureAiEditEnabled() {
if (featureFlags.aiEditEnabled) return true
const settings = await prisma.systemSettings.findFirst({
select: { enableAiImageEdit: true },
})
return !!settings?.enableAiImageEdit
}
export async function POST(req: NextRequest) {
try {
const session = await getServerSession(authOptions)
if (!session) {
return NextResponse.json({ error: '未登录' }, { status: 401 })
}
const isEnabled = await ensureAiEditEnabled()
if (!isEnabled) {
return NextResponse.json({ error: 'AI改图功能未启用' }, { status: 501 })
}
const body = await req.json().catch(() => ({}))
const { materialId, operation, params } = body || {}
if (!materialId || !operation) {
return NextResponse.json({ error: '缺少必要参数(materialId/operation)' }, { status: 400 })
}
const material = await prisma.material.findFirst({
where: {
id: materialId,
deletedAt: null,
},
include: {
user: {
select: {
id: true,
department: true,
},
},
},
})
if (!material) {
return NextResponse.json({ error: '素材不存在' }, { status: 404 })
}
const actor = await getMaterialPermissionActor(session)
if (!(await canManageMaterial(material, actor))) {
return NextResponse.json({ error: '您没有权限操作此素材' }, { status: 403 })
}
// TODO: 接入外部AI服务(基于你提供的API),并以任务形式异步执行
return NextResponse.json({
message: 'AI改图任务创建成功(占位)',
taskId: 'mock_task_id',
materialId,
operation,
params: params || {},
status: 'queued',
})
} catch (error) {
console.error('AI Edit Error:', error)
return NextResponse.json({ error: '服务异常' }, { status: 500 })
}
}