diff --git a/lib/ai/aos-image-transfer.ts b/lib/ai/aos-image-transfer.ts new file mode 100644 index 000000000..1fe567060 --- /dev/null +++ b/lib/ai/aos-image-transfer.ts @@ -0,0 +1,51 @@ +import "server-only"; + +import { buildImageProxyUrl } from "@/lib/image-delivery"; +import { createLogger } from "@/lib/observability/logger"; +import { archiveImageThroughQiniu } from "@/lib/storage/qiniu-image-archive"; + +const logger = createLogger("aos-image-transfer"); +const imageFormats = ["jpeg", "png", "webp", "tiff"]; + +export async function readAosQiniuInput(imageUrl: string) { + const cdnOrigin = process.env.NEXT_PUBLIC_CANVAS_CDN_ORIGIN; + if (!cdnOrigin) return null; + try { + const source = new URL(imageUrl); + if (source.protocol !== "https:" || source.origin !== new URL(cdnOrigin).origin || + source.username || source.password || source.search || source.hash || + !source.pathname.startsWith("/uploads/")) return null; + + const response = await fetch(`${source.href}?imageInfo`, { + cache: "no-store", + redirect: "error", + signal: AbortSignal.timeout(4000), + }); + if (!response.ok) return null; + const info = await response.json(); + const { width, height, size } = info; + if (!["jpeg", "png", "webp"].includes(info.format) || + ![width, height, size].every(value => Number.isSafeInteger(value) && value > 0) || + size > 20 * 1024 * 1024) return null; + return { url: source.href, width, height, bytes: size } as { + url: string; width: number; height: number; bytes: number; + }; + } catch { + return null; + } +} + +export async function archiveAosResult(imageUrl: unknown, archiveId: string) { + if (typeof imageUrl !== "string" || !/^https:\/\//i.test(imageUrl)) return null; + try { + const image = await archiveImageThroughQiniu(imageUrl, archiveId); + if (!image || !imageFormats.some(format => image.contentType === `image/${format}`)) return null; + return { + original: image.url, + thumbnail: buildImageProxyUrl(image.url, { width: 480, format: "webp" }), + }; + } catch (error) { + logger.warn("archive_failed", { archiveId, errorName: error instanceof Error ? error.name : undefined }); + return null; + } +} diff --git a/lib/ai/rmbg/service.ts b/lib/ai/rmbg/service.ts index 201f15569..299577338 100644 --- a/lib/ai/rmbg/service.ts +++ b/lib/ai/rmbg/service.ts @@ -4,6 +4,7 @@ import { getSystemSetting, upsertMediaAsset, } from "@/lib/db/queries"; +import { archiveAosResult, readAosQiniuInput } from "@/lib/ai/aos-image-transfer"; import { executeConfiguredVip98kTask } from "@/lib/ai/vip98k-runtime"; import { db } from "@/lib/db"; import { generatedImage } from "@/lib/db/schema"; @@ -30,20 +31,21 @@ type RmbgConfig = { export type ExecuteRmbgJobParams = { requestId?: string; userId: string; - imageBuffer: Buffer; imageBase64?: string; + includeBase64?: boolean; foregroundType?: "" | "person" | "object" | "stamp"; prompt?: string; chatId?: string | null; messageId?: string | null; skipGallerySave?: boolean; - // 仅当 imageBuffer 与 URL 对应的输入文件一致时提供。 - inputImageUrl?: string | null; sourceImageUrl?: string | null; -}; +} & ( + | { imageBuffer: Buffer; inputImageUrl?: string | null } + | { imageBuffer?: undefined; inputImageUrl: string } +); export type ExecuteRmbgJobResult = { - image_base64: string; + image_base64?: string; thumbnail_url: string; original_url: string; processing_time: number; @@ -181,6 +183,15 @@ function getAosFriendlyErrorMessage(detail: string): string | null { return null; } +function assertAosImageResolution(width?: number, height?: number) { + if (width && height && (width > AOS_MAX_RESOLUTION || height > AOS_MAX_RESOLUTION)) { + throw new RmbgServiceError( + `抠图参考图分辨率超限(${width}×${height} 像素),当前 AOS 上游要求宽、高均不超过 ${AOS_MAX_RESOLUTION} 像素。`, + 400 + ); + } +} + async function prepareImageForAos( imageBuffer: Buffer ): Promise<{ buffer: Buffer; mimeType: string; filename: string }> { @@ -201,13 +212,7 @@ async function prepareImageForAos( return { buffer: imageBuffer, mimeType: "application/octet-stream", filename: "image" }; } - const { width, height } = metadata; - if (width && height && (width > AOS_MAX_RESOLUTION || height > AOS_MAX_RESOLUTION)) { - throw new RmbgServiceError( - `抠图参考图分辨率超限(${width}×${height} 像素),当前 AOS 上游要求宽、高均不超过 ${AOS_MAX_RESOLUTION} 像素。`, - 400 - ); - } + assertAosImageResolution(metadata.width, metadata.height); const format = metadata.format || ""; if (["gif", "svg", "heif", "avif", "jp2", "jxl"].includes(format)) { @@ -374,31 +379,42 @@ async function processWithPhotoRoom( } async function processWithAosSegmentation( - imageBuffer: Buffer, + input: Pick, config: { apiKey: string; apiBaseUrl?: string; foregroundType?: "" | "person" | "object" | "stamp"; crop?: 0 | 1; format?: "png" | "jpg"; - inputImageUrl?: string | null; requestId?: string; } -): Promise<{ success: boolean; imageBuffer?: Buffer; processing_time?: number; error?: string }> { +): Promise<{ + success: boolean; + imageBuffer?: Buffer; + archived?: NonNullable>>; + processing_time?: number; + error?: string; +}> { const startTime = Date.now(); const baseUrl = (config.apiBaseUrl || "https://techsz.aoscdn.com").replace(/\/+$/, ""); const createUrl = `${baseUrl}/api/tasks/visual/segmentation`; const headers = { "X-API-KEY": config.apiKey }; let stage = "create"; let providerTaskId: string | undefined; - const sourceImage = await prepareImageForAos(imageBuffer); + const directInput = !input.imageBuffer && input.inputImageUrl + ? await readAosQiniuInput(input.inputImageUrl) + : null; + if (directInput) assertAosImageResolution(directInput.width, directInput.height); + const imageBuffer = input.imageBuffer ?? (directInput ? null : await fetchImageBufferFromUrl(input.inputImageUrl!)); + const inputBytes = directInput?.bytes ?? imageBuffer!.length; + const sourceImage = imageBuffer ? await prepareImageForAos(imageBuffer) : null; try { const createForm = new FormData(); - let publicSourceUrl: string | null = null; - if (config.inputImageUrl && imageBuffer.length <= AOS_MAX_URL_INPUT_BYTES) { + let publicSourceUrl = directInput?.url ?? null; + if (!publicSourceUrl && input.inputImageUrl && inputBytes <= AOS_MAX_URL_INPUT_BYTES) { try { - const source = new URL(config.inputImageUrl); + const source = new URL(input.inputImageUrl); const cdn = new URL(process.env.NEXT_PUBLIC_CANVAS_CDN_ORIGIN || ""); if (["http:", "https:"].includes(source.protocol) && source.hostname === cdn.hostname) { publicSourceUrl = source.toString(); @@ -408,10 +424,10 @@ async function processWithAosSegmentation( if (publicSourceUrl) { createForm.append("image_url", publicSourceUrl); } else { - const imageBlob = new Blob([new Uint8Array(sourceImage.buffer)], { - type: sourceImage.mimeType, + const imageBlob = new Blob([new Uint8Array(sourceImage!.buffer)], { + type: sourceImage!.mimeType, }); - createForm.append("image_file", imageBlob, sourceImage.filename); + createForm.append("image_file", imageBlob, sourceImage!.filename); } createForm.append("sync", "0"); createForm.append("return_type", "1"); @@ -423,7 +439,7 @@ async function processWithAosSegmentation( } const createStartedAt = Date.now(); - logger.info("aos_task_create_started", { requestId: config.requestId, sourceMode: publicSourceUrl ? "url" : "file", inputBytes: imageBuffer.length, uploadBytes: publicSourceUrl ? 0 : sourceImage.buffer.length }); + logger.info("aos_task_create_started", { requestId: config.requestId, sourceMode: publicSourceUrl ? "url" : "file", inputBytes, uploadBytes: publicSourceUrl ? 0 : inputBytes }); const createResponse = await withTimeout( (signal) => @@ -497,6 +513,12 @@ async function processWithAosSegmentation( taskId, }); stage = "download"; + if (input.includeBase64 === false) { + const archived = await archiveAosResult(imageUrl, `aos-rmbg-${taskId}`); + if (archived) { + return { success: true, archived, processing_time: (Date.now() - startTime) / 1000 }; + } + } const imageResponse = await readAosResult( async (signal) => { const response = await fetch(imageUrl, { @@ -544,7 +566,7 @@ async function processWithAosSegmentation( return { success: false, error: `AOS查询任务超时,结果尚未确认(任务 ${taskId})` }; } catch (error) { if (error instanceof Error && error.name === "AbortError") { - logger.warn("aos_request_timeout", { requestId: config.requestId, stage, taskId: providerTaskId, inputBytes: imageBuffer.length }); + logger.warn("aos_request_timeout", { requestId: config.requestId, stage, taskId: providerTaskId, inputBytes }); const message = stage === "create" ? "AOS创建任务超时(120秒),结果尚未确认" : stage === "poll" @@ -653,6 +675,10 @@ export async function fetchImageBufferFromUrl(imageUrl: string): Promise ); } +export function executeRmbgJob( + params: ExecuteRmbgJobParams & { includeBase64?: true } +): Promise; +export function executeRmbgJob(params: ExecuteRmbgJobParams): Promise; export async function executeRmbgJob( params: ExecuteRmbgJobParams ): Promise { @@ -665,14 +691,12 @@ async function runRmbgJob( const { requestId = crypto.randomUUID(), userId, - imageBuffer, - imageBase64 = imageBuffer.toString("base64"), + includeBase64 = true, foregroundType, prompt = "抠图处理", chatId = null, messageId = null, skipGallerySave = false, - inputImageUrl = null, sourceImageUrl = null, } = params; @@ -682,17 +706,21 @@ async function runRmbgJob( hasChatId: Boolean(chatId), hasMessageId: Boolean(messageId), hasSourceImageUrl: Boolean(sourceImageUrl), - inputBytes: imageBuffer.length, + inputBytes: params.imageBuffer?.length, provider, skipGallerySave, }); - let resultBuffer: Buffer; - let resultBase64: string; + let resultBuffer: Buffer | undefined; + let resultBase64: string | undefined; + let archived: NonNullable>> | undefined; let processingTime: number; + const imageBuffer = provider === "aoscdn" + ? null + : params.imageBuffer ?? await fetchImageBufferFromUrl(params.inputImageUrl!); if (provider === "vip98k") { - const result = await processWithVip98k(imageBuffer); + const result = await processWithVip98k(imageBuffer!); if (!result.success || !result.imageBuffer) { logger.warn("job_provider_failed", { provider, @@ -701,10 +729,9 @@ async function runRmbgJob( throw new RmbgServiceError("抠图处理失败", 500, result.error); } resultBuffer = result.imageBuffer; - resultBase64 = resultBuffer.toString("base64"); processingTime = result.processing_time || 0; } else if (provider === "photoroom") { - const result = await processWithPhotoRoom(imageBuffer, config.photoroomApiKey!); + const result = await processWithPhotoRoom(imageBuffer!, config.photoroomApiKey!); if (!result.success || !result.imageBuffer) { logger.warn("job_provider_failed", { provider, @@ -713,20 +740,18 @@ async function runRmbgJob( throw new RmbgServiceError("抠图处理失败", 500, result.error); } resultBuffer = result.imageBuffer; - resultBase64 = resultBuffer.toString("base64"); processingTime = result.processing_time || 0; } else if (provider === "aoscdn") { - const result = await processWithAosSegmentation(imageBuffer, { + const result = await processWithAosSegmentation(params, { apiKey: config.aosApiKey!, apiBaseUrl: config.aosApiBaseUrl, foregroundType: foregroundType ?? config.aosForegroundType ?? "", crop: config.aosCrop === 1 ? 1 : 0, format: config.aosFormat === "jpg" ? "jpg" : "png", - inputImageUrl, requestId, }); - if (!result.success || !result.imageBuffer) { + if (!result.success || !(result.imageBuffer || result.archived)) { const detail = result.error || "AOS处理失败"; const friendlyMessage = getAosFriendlyErrorMessage(detail); logger.warn("job_provider_failed", { @@ -742,10 +767,10 @@ async function runRmbgJob( } resultBuffer = result.imageBuffer; - resultBase64 = resultBuffer.toString("base64"); + archived = result.archived; processingTime = result.processing_time || 0; } else { - const result = await processWithRMBG(imageBase64, { + const result = await processWithRMBG(params.imageBase64 ?? imageBuffer!.toString("base64"), { apiUrl: config.apiUrl!, apiKey: config.apiKey, }); @@ -763,7 +788,8 @@ async function runRmbgJob( processingTime = result.processing_time || 0; } - const uploadResult = await uploadBufferToS3(resultBuffer, "image/png"); + const uploadResult = archived ?? await uploadBufferToS3(resultBuffer!, "image/png"); + resultBase64 = includeBase64 ? resultBase64 ?? resultBuffer!.toString("base64") : undefined; const thumbnailUrl = uploadResult.thumbnail; const originalUrl = uploadResult.original; diff --git a/lib/canvas/capabilities/headless-executor.ts b/lib/canvas/capabilities/headless-executor.ts index 2fcf0790f..5f46f2567 100644 --- a/lib/canvas/capabilities/headless-executor.ts +++ b/lib/canvas/capabilities/headless-executor.ts @@ -4,7 +4,6 @@ import { executeLayerSplitJob } from "@/lib/ai/layer-split/service"; import { executeResizeJob } from "@/lib/ai/resize/service"; import { executeRmbgJob, - fetchImageBufferFromUrl, RmbgServiceError, } from "@/lib/ai/rmbg/service"; import type { CanvasCapabilityClientCommand } from "@/lib/canvas/capabilities/client-command"; @@ -126,12 +125,11 @@ async function executeForTarget( }; if (command.capabilityId === "image.remove_background") { - const imageBuffer = await fetchImageBufferFromUrl(target.sourceImageUrl); const result = await executeRmbgJob({ requestId: command.requestId, userId: params.userId, - imageBuffer, inputImageUrl: target.sourceImageUrl, + includeBase64: false, sourceImageUrl: target.sourceImageUrl, prompt: prompt || "抠图处理", skipGallerySave: false, @@ -257,10 +255,12 @@ async function executeForTarget( imageUrl: target.sourceImageUrl, prompt: prompt || "图像增强处理", scaleFactor: asFiniteNumber(command.args.scaleFactor), + includeBase64: false, }, pollingParams: { prompt: prompt || "图像增强处理", sourceImageUrl: target.sourceImageUrl, + includeBase64: "false", }, timeoutMs: 10 * 60 * 1000, headers: getCookieHeaders(params.requestHeaders), diff --git a/lib/canvas/operations/enhance.ts b/lib/canvas/operations/enhance.ts index 99d82257f..c8ebd8420 100644 --- a/lib/canvas/operations/enhance.ts +++ b/lib/canvas/operations/enhance.ts @@ -16,6 +16,7 @@ import { import { uploadFile } from "@/lib/storage/service"; import { db } from "@/lib/db"; import { generatedImage } from "@/lib/db/schema"; +import { archiveAosResult, readAosQiniuInput } from "@/lib/ai/aos-image-transfer"; import { executeConfiguredVip98kTask } from "@/lib/ai/vip98k-runtime"; import { createFalClient } from "@fal-ai/client"; @@ -506,6 +507,8 @@ function buildEnhanceTaskFailureResponse(error: EnhanceTerminalError) { async function finalizeEnhanceTaskResult(params: { userId: string; taskResult: any; + taskId?: string; + includeBase64?: boolean; prompt?: string; chatId?: string | null; messageId?: string | null; @@ -516,8 +519,17 @@ async function finalizeEnhanceTaskResult(params: { throw new Error(params.taskResult?.message || "未知错误"); } - const completedImage = await readCompletedEnhanceImage(params.taskResult.data); - const uploadResult = await uploadEnhanceImage(completedImage.buffer, completedImage.mimeType); + const data = params.taskResult.data; + const includeBase64 = params.includeBase64 !== false; + let uploadResult = !includeBase64 && params.taskId + ? await archiveAosResult(data.image, `aos-enhance-${params.taskId}`) + : null; + let imageBase64: string | undefined; + if (!uploadResult) { + const completedImage = await readCompletedEnhanceImage(data); + uploadResult = await uploadEnhanceImage(completedImage.buffer, completedImage.mimeType); + if (includeBase64) imageBase64 = completedImage.buffer.toString("base64"); + } const finalPrompt = params.prompt?.trim() || "图片变清晰处理"; let saved = false; @@ -607,7 +619,7 @@ async function finalizeEnhanceTaskResult(params: { return { success: true, - image_base64: completedImage.buffer.toString("base64"), + image_base64: imageBase64, thumbnail_url: uploadResult.thumbnail, original_url: uploadResult.original, processing_time: processingTime, @@ -724,6 +736,8 @@ export async function executeEnhanceGET(request: Request, trustedUserId: string) const responsePayload = await finalizeEnhanceTaskResult({ userId: session.user.id, taskResult, + taskId, + includeBase64: searchParams.get("includeBase64") !== "false", prompt, chatId, messageId, @@ -805,7 +819,9 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string const contentType = request.headers.get("content-type") || ""; console.log("[Enhance API] Content-Type:", contentType); - let imageBase64: string; + let imageBase64 = ""; + let includeBase64 = true; + let directImageUrl: string | undefined; let prompt = ""; // 用户提示词(可选) let chatId: string | null = null; // 聊天ID(可选) let messageId: string | null = null; // 消息ID(可选) @@ -815,6 +831,7 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string if (contentType.includes("application/json")) { console.log("[Enhance API] 解析JSON请求体..."); const body = await request.json(); + includeBase64 = body.includeBase64 !== false; prompt = body.prompt || "图片变清晰处理"; chatId = body.chatId || null; messageId = body.messageId || null; @@ -827,26 +844,29 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string // 优先使用 imageUrl,其次使用 image_base64 if (body.imageUrl) { sourceImageUrl = String(body.imageUrl); - // 从 URL 获取图片并转换为 Base64 - console.log(`[Enhance API] 从 URL 获取图片: ${body.imageUrl}`); - try { - const imageResponse = await fetch(body.imageUrl); - if (!imageResponse.ok) { + if (!includeBase64 && !useVip98k && !useBackupApi) { + const input = await readAosQiniuInput(sourceImageUrl); + if (input && input.width <= AOS_MAX_RESOLUTION && input.height <= AOS_MAX_RESOLUTION) { + directImageUrl = input.url; + } + } + if (!directImageUrl) { + try { + const imageResponse = await fetch(sourceImageUrl); + if (!imageResponse.ok) { + return NextResponse.json( + { error: `无法获取图片: ${imageResponse.status} ${imageResponse.statusText}` }, + { status: 400 } + ); + } + imageBase64 = Buffer.from(await imageResponse.arrayBuffer()).toString("base64"); + } catch (fetchError) { + console.error("[Enhance API] 获取图片失败:", fetchError); return NextResponse.json( - { error: `无法获取图片: ${imageResponse.status} ${imageResponse.statusText}` }, + { error: "无法获取图片,请检查图片链接是否有效" }, { status: 400 } ); } - const arrayBuffer = await imageResponse.arrayBuffer(); - const buffer = Buffer.from(arrayBuffer); - imageBase64 = buffer.toString("base64"); - console.log(`[Enhance API] 图片已转换为 Base64,大小: ${buffer.length} bytes`); - } catch (fetchError) { - console.error("[Enhance API] 获取图片失败:", fetchError); - return NextResponse.json( - { error: "无法获取图片,请检查图片链接是否有效" }, - { status: 400 } - ); } } else if (body.image_base64) { console.log("[Enhance API] JSON请求体解析完成,image_base64长度:", body.image_base64?.length || 0); @@ -1080,21 +1100,20 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string const enhanceUrl = `${apiUrl}/api/tasks/visual/scale`; console.log("[Enhance API] API URL:", enhanceUrl); - // 将Base64转换为Buffer - console.log("[Enhance API] 转换Base64为Buffer,长度:", imageBase64.length); - const rawImageBuffer = Buffer.from(imageBase64, "base64"); - const imageBuffer = await ensureAosResolutionLimit(rawImageBuffer, "Enhance API"); - console.log("[Enhance API] Buffer创建完成,大小:", imageBuffer.length, "bytes"); - // 创建FormData(使用form-data包,Node.js环境) console.log("[Enhance API] 创建FormData..."); const FormDataLib = await import("form-data"); const formData = new FormDataLib.default(); console.log("[Enhance API] FormData创建完成"); - formData.append("image_file", imageBuffer, { - filename: "image.jpg", - contentType: "image/jpeg", - }); + if (directImageUrl) { + formData.append("image_url", directImageUrl); + } else { + const imageBuffer = await ensureAosResolutionLimit(Buffer.from(imageBase64, "base64"), "Enhance API"); + formData.append("image_file", imageBuffer, { + filename: "image.jpg", + contentType: "image/jpeg", + }); + } // 使用异步模式(sync=0),根据官方示例 formData.append("sync", "0"); formData.append("type", config.type || "clean"); // 默认通用清晰化 @@ -1178,7 +1197,7 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string } catch (fetchError) { clearTimeout(timeoutId); lastFetchError = fetchError; - const canRetry = attempt <= ENHANCE_CREATE_RETRY_COUNT && isRetryableNetworkError(fetchError); + const canRetry = !directImageUrl && attempt <= ENHANCE_CREATE_RETRY_COUNT && isRetryableNetworkError(fetchError); if (canRetry) { console.warn( `[Enhance API] 创建任务失败,准备重试: attempt=${attempt}, error=${fetchError instanceof Error ? fetchError.message : String(fetchError)}` @@ -1196,6 +1215,10 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string if (!createResponse.ok) { const errorText = await createResponse.text(); + if (directImageUrl && createResponse.status >= 400 && createResponse.status < 500 && createResponse.status !== 408) { + const friendlyMessage = getAosFriendlyErrorMessage(errorText); + return NextResponse.json({ error: friendlyMessage || "变清晰任务提交失败" }, { status: createResponse.status }); + } throw new Error(`创建任务失败: ${errorText || createResponse.statusText}`); } @@ -1215,6 +1238,13 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string processingTime, }); + if (directImageUrl) { + return NextResponse.json( + { error: "变清晰任务提交结果尚未确认,请核对原任务,避免重复提交。" }, + { status: 504 } + ); + } + try { const fallbackPayload = await executeFalEsrganFallback({ userId, @@ -1317,6 +1347,8 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string const responsePayload = await finalizeEnhanceTaskResult({ userId, taskResult, + taskId, + includeBase64, prompt, chatId, messageId, diff --git a/lib/canvas/operations/rmbg.ts b/lib/canvas/operations/rmbg.ts index 96802bf36..e44136dd6 100644 --- a/lib/canvas/operations/rmbg.ts +++ b/lib/canvas/operations/rmbg.ts @@ -22,7 +22,7 @@ export async function executeRemoveBackgroundPOST(request: Request, trustedUserI const contentType = request.headers.get("content-type") || ""; - let imageBuffer: Buffer; + let imageBuffer: Buffer | undefined; let imageBase64: string | undefined; let prompt = "抠图处理"; let chatId: string | null = null; @@ -30,9 +30,11 @@ export async function executeRemoveBackgroundPOST(request: Request, trustedUserI let skipGallerySave = false; let sourceImageUrl: string | null = null; let inputImageUrl: string | null = null; + let includeBase64 = true; if (contentType.includes("application/json")) { const body = await request.json(); + includeBase64 = body.includeBase64 !== false; prompt = body.prompt || "抠图处理"; chatId = body.chatId || null; messageId = body.messageId || null; @@ -44,7 +46,7 @@ export async function executeRemoveBackgroundPOST(request: Request, trustedUserI if (imageUrl) { sourceImageUrl = String(imageUrl); inputImageUrl = sourceImageUrl; - imageBuffer = await fetchImageBufferFromUrl(sourceImageUrl); + if (includeBase64) imageBuffer = await fetchImageBufferFromUrl(sourceImageUrl); } else if (body.image_base64) { imageBase64 = String(body.image_base64); imageBuffer = Buffer.from(imageBase64, "base64"); @@ -100,13 +102,13 @@ export async function executeRemoveBackgroundPOST(request: Request, trustedUserI const result = await executeRmbgJob({ requestId, userId: session.user.id, - imageBuffer, + ...(imageBuffer ? { imageBuffer, inputImageUrl } : { inputImageUrl: inputImageUrl! }), imageBase64, + includeBase64, prompt, chatId, messageId, skipGallerySave, - inputImageUrl, sourceImageUrl, }); diff --git a/lib/video-canvas/image-operation.ts b/lib/video-canvas/image-operation.ts index e2e65ef85..6d0413873 100644 --- a/lib/video-canvas/image-operation.ts +++ b/lib/video-canvas/image-operation.ts @@ -65,7 +65,7 @@ export async function executeImageOperation( vertical_angle: input.verticalAngle, zoom: input.zoom, } - : { imageUrl: input.imageUrl, scaleFactor: input.scaleFactor }; + : { imageUrl: input.imageUrl, scaleFactor: input.scaleFactor, includeBase64: false }; const handler = { enhance: handlers.enhance, extend: handlers.extend, @@ -109,7 +109,7 @@ export async function executeImageOperation( if (error instanceof CanvasTaskLeaseLostError) throw error; throw new ImageOperationUncertainError("高清任务已受理,但任务标识保存失败,请勿重复提交"); } - const query = new URLSearchParams({ taskId, sourceImageUrl: input.imageUrl }); + const query = new URLSearchParams({ taskId, sourceImageUrl: input.imageUrl, includeBase64: "false" }); for (let i = 0; i < (options.maxPolls ?? 30); i++) { await (options.wait || ((ms) => new Promise((resolve) => setTimeout(resolve, ms))))(1200); await options.assertLease?.(); diff --git a/playwright.helpers.config.ts b/playwright.helpers.config.ts index 78300ddba..549ba6103 100644 --- a/playwright.helpers.config.ts +++ b/playwright.helpers.config.ts @@ -3,6 +3,7 @@ import { defineConfig } from "@playwright/test"; export default defineConfig({ testDir: "./tests", testMatch: [ + /routes\/aos-image-transfer\.test\.ts/, /routes\/rmbg-delivery\.test\.ts/, /routes\/legacy-oss-recovery\.test\.ts/, /routes\/retired-storage\.test\.ts/, diff --git a/tests/routes/aos-image-transfer.test.ts b/tests/routes/aos-image-transfer.test.ts new file mode 100644 index 000000000..c5f64200d --- /dev/null +++ b/tests/routes/aos-image-transfer.test.ts @@ -0,0 +1,95 @@ +import { expect, test } from "@playwright/test"; +import { serverLoader } from "../helpers/canvas-download-server-loader"; + +const cdn = "https://assets.example"; +const input = `${cdn}/uploads/original.png`; +const output = "https://result.example/result.png"; +const archived = `${cdn}/uploads/archived-result`; +const aos = "https://api.example"; + +function testImageTransfer(operation: "scale" | "segmentation") { + const endpoint = `${aos}/api/tasks/visual/${operation}`; + const calls: string[] = []; + const load = serverLoader({ + "next/server": { NextResponse: Response }, + "@/lib/db/queries": { + getSystemSetting: async () => ({ value: { + provider: "aoscdn", apiUrl: aos, apiKey: "test-key", aosApiBaseUrl: aos, aosApiKey: "test-key", + } }), + getMediaAssetsByUserAndUrls: async () => [], + upsertMediaAsset: async () => null, + }, + "@/lib/db": { db: { insert: () => ({ values: () => ({ returning: async () => [{ id: "saved" }] }) }) } }, + "@/lib/db/schema": { generatedImage: {} }, + "@/lib/ai/vip98k-runtime": {}, + "@/lib/storage/service": {}, + "@/lib/storage/qiniu-image-archive": { + archiveImageThroughQiniu: async (url: string, key: string) => { + expect(url).toBe(output); + calls.push(key); + return { url: archived, contentType: "image/png" }; + }, + }, + "@/lib/observability/active-work": { trackRuntimeWork: (fn: () => unknown) => fn() }, + setTimeout: (callback: () => void, ms: number) => setTimeout(callback, ms <= 2000 ? 0 : ms), + fetch: async (rawUrl: string, options?: RequestInit) => { + const url = String(rawUrl); + calls.push(url); + if (url === `${input}?imageInfo`) { + return Response.json({ width: 1024, height: 768, size: 100_000, format: "png" }); + } + if (url === endpoint && options?.method === "POST") { + const body = options.body instanceof FormData + ? options.body + : await new Request(endpoint, options).formData(); + expect(body.get("image_url")).toBe(input); + expect(body.has("image_file")).toBe(false); + expect(body.get("return_type")).toBe("1"); + expect(body.get(operation === "scale" ? "scale_factor" : "format")) + .toBe(operation === "scale" ? "2" : "png"); + return Response.json({ data: { task_id: "test-aos-task" } }); + } + if (url === `${endpoint}/test-aos-task`) { + return Response.json({ status: 200, data: { state: 1, image: output } }); + } + throw Error(`Unexpected request to ${url}`); + }, + }, { NEXT_PUBLIC_CANVAS_CDN_ORIGIN: cdn }); + return { endpoint, calls, load }; +} + +test("canvas enhancement sends a stored input URL and returns a stored result URL after polling", async () => { + const { load, calls, endpoint } = testImageTransfer("scale"); + const enhance = load("lib/canvas/operations/enhance.ts"); + const checkpoint: string[] = []; + const response = await enhance.executeEnhancePOST( + new Request("https://app.example/enhance", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ imageUrl: input, scaleFactor: 2, includeBase64: false }), + }), + "test-user", + async (id: string) => { checkpoint.push(id); }, + ); + expect(response.status).toBe(202); + expect(checkpoint).toEqual(["test-aos-task"]); + const poll = await enhance.executeEnhanceGET( + new Request("https://app.example/enhance?taskId=test-aos-task&includeBase64=false"), "test-user", + ); + expect(poll.ok).toBe(true); + const result = await poll.json(); + expect(result.original_url).toBe(archived); + expect(result).not.toHaveProperty("image_base64"); + expect(calls).toEqual([`${input}?imageInfo`, endpoint, `${endpoint}/test-aos-task`, "aos-enhance-test-aos-task"]); +}); + +test("canvas cutout keeps the archived PNG as the original and skips production image transfers", async () => { + const { load, calls, endpoint } = testImageTransfer("segmentation"); + const rmbg = load("lib/ai/rmbg/service.ts"); + const result = await rmbg.executeRmbgJob({ + userId: "test-user", inputImageUrl: input, includeBase64: false, skipGallerySave: true, + }); + expect(result.original_url).toBe(archived); + expect(result.image_base64).toBeUndefined(); + expect(calls).toEqual([`${input}?imageInfo`, endpoint, `${endpoint}/test-aos-task`, "aos-rmbg-test-aos-task"]); +});