fix(canvas): 图片工具 - 修复裁剪和尺寸调整的跨域导出
This commit is contained in:
1 parent
e70fe1b9c9
commit
8de62b1a49
4 files changed
+184
-16
No files matched your search
@@ -18,6 +18,7 @@ import type { PanoramaGenerateConfig } from "@/components/canvas/canvas-panorama
|
||||
import type { CanvasVideoFrameParams } from "@/components/canvas/canvas-video-frame-dialog";
|
||||
import { NODE_DEFAULT_SIZE } from "@/constant/canvas";
|
||||
import { cropDataUrl, splitDataUrl, upscaleDataUrl } from "@/lib/canvas/canvas-image-data";
|
||||
import { prepareRasterSource } from "@/lib/image-raster";
|
||||
import { isValidGridSplit, layoutGridSplitCells } from "@/lib/canvas/canvas-grid-split";
|
||||
import { audioMetadata, buildSourceImageLayerNodeResult, imageMetadata, videoMetadata } from "@/lib/canvas/canvas-generation-task-sync";
|
||||
import { commitProducedModel } from "@/lib/canvas/produced-model";
|
||||
@@ -227,11 +228,9 @@ export function useCanvasMediaTools({
|
||||
|
||||
const cropImageNode = useCallback(async (node: CanvasNodeData, crop: CanvasImageCropRect) => {
|
||||
if (!node.metadata?.content) return;
|
||||
// 云端图片地址通常不带 CORS 头,直接画到 canvas 会被判定为跨域而无法导出。
|
||||
// 优先用本地缓存里的 Blob 构造同源地址,裁剪才能读取像素。
|
||||
let releaseSource = () => {};
|
||||
try {
|
||||
const source = await resolveCroppableImageSource(node);
|
||||
const source = await resolveProcessableImageSource(node);
|
||||
releaseSource = source.release;
|
||||
const cropped = await cropDataUrl(source.url, crop);
|
||||
const image = await uploadImage(cropped);
|
||||
@@ -647,8 +646,11 @@ export function useCanvasMediaTools({
|
||||
|
||||
const splitImageNode = useCallback(async (node: CanvasNodeData, params: CanvasImageSplitParams) => {
|
||||
if (!node.metadata?.content || !isValidGridSplit(params)) return;
|
||||
let releaseSource = () => {};
|
||||
try {
|
||||
const pieces = await splitDataUrl(node.metadata.content, params);
|
||||
const source = await resolveProcessableImageSource(node);
|
||||
releaseSource = source.release;
|
||||
const pieces = await splitDataUrl(source.url, params);
|
||||
const sizedPieces = await Promise.all(pieces.map(async (piece) => {
|
||||
const image = await uploadImage(piece.dataUrl);
|
||||
return { piece, image, size: fitNodeSize(image.width, image.height) };
|
||||
@@ -675,6 +677,8 @@ export function useCanvasMediaTools({
|
||||
message.success(`已切分为 ${childNodes.length} 个子节点`);
|
||||
} catch (error) {
|
||||
message.error(error instanceof Error ? `切分失败:${error.message}` : "图片切分失败,请重试");
|
||||
} finally {
|
||||
releaseSource();
|
||||
}
|
||||
}, [message, persistMediaNodes, setConnections, setDialogNodeId, setNodes, setSelectedConnectionId, setSelectedNodeIds]);
|
||||
|
||||
@@ -1507,8 +1511,11 @@ export function useCanvasMediaTools({
|
||||
const upscaleImageNode = useCallback(async (node: CanvasNodeData, params: CanvasImageUpscaleParams) => {
|
||||
if (!node.metadata?.content) return;
|
||||
setUpscaleNodeId(null);
|
||||
let releaseSource = () => {};
|
||||
try {
|
||||
const upscaled = await upscaleDataUrl(node.metadata.content, params);
|
||||
const source = await resolveProcessableImageSource(node);
|
||||
releaseSource = source.release;
|
||||
const upscaled = await upscaleDataUrl(source.url, params);
|
||||
const image = await uploadImage(upscaled);
|
||||
const size = fitNodeSize(image.width, image.height);
|
||||
const childId = nanoid();
|
||||
@@ -1520,6 +1527,8 @@ export function useCanvasMediaTools({
|
||||
await persistMediaNodes([child]);
|
||||
} catch (error) {
|
||||
message.error(error instanceof Error ? `放大失败:${error.message}` : "图片放大失败,请重试");
|
||||
} finally {
|
||||
releaseSource();
|
||||
}
|
||||
}, [message, persistMediaNodes, setConnections, setDialogNodeId, setNodes, setSelectedNodeIds]);
|
||||
|
||||
@@ -1747,17 +1756,11 @@ export function useCanvasMediaTools({
|
||||
};
|
||||
}
|
||||
|
||||
// 裁剪、切分等像素级操作要求图片同源可读:云端地址若不带 CORS 头,
|
||||
// canvas 会被标记为跨域,toDataURL 直接抛 SecurityError。
|
||||
// 这里优先用本地缓存 Blob 构造同源 objectURL,取不到时再回退原始地址。
|
||||
async function resolveCroppableImageSource(node: CanvasNodeData): Promise<{ url: string; release: () => void }> {
|
||||
// 资源 Blob 读取使用 browser-process 授权,复用缓存;读取失败时保留错误,不退回展示地址。
|
||||
async function resolveProcessableImageSource(node: CanvasNodeData): Promise<{ url: string; release: () => void }> {
|
||||
const content = node.metadata?.content ?? "";
|
||||
if (content.startsWith("data:") || content.startsWith("blob:")) return { url: content, release: () => {} };
|
||||
const storageKey = node.metadata?.storageKey;
|
||||
if (!storageKey) return { url: content, release: () => {} };
|
||||
if (!storageKey) return prepareRasterSource(content);
|
||||
const readBlob = storageKey.startsWith("image:") || storageKey.startsWith("generation-image:") ? getImageBlob : getMediaBlob;
|
||||
const blob = await readBlob(storageKey).catch(() => null);
|
||||
if (!blob) return { url: content, release: () => {} };
|
||||
const url = URL.createObjectURL(blob);
|
||||
return { url, release: () => URL.revokeObjectURL(url) };
|
||||
return prepareRasterSource(content, () => readBlob(storageKey));
|
||||
}
|
||||
Reference in new issue
Block a user