Compare commits
11 Commits
codex/imag
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 71413c173e | |||
| dbe96db34d | |||
| dfaeb9f09d | |||
| 46abb8259a | |||
| 3b3b5bc78a | |||
| db63f38b9c | |||
| 955ad91ae8 | |||
| 36ac3def8f | |||
| 1d40053c7a | |||
| 07451f5558 | |||
| 13684505a6 |
@ -1,5 +1,5 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||
import { isRetiredStorageUrl, normalizeLegacyAliyunStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||
import { auth } from "@/app/(auth)/auth";
|
||||
import { buildThreeAssetUrl } from "@/lib/canvas/three-asset-url";
|
||||
|
||||
@ -182,6 +182,18 @@ async function handleRequest(request: Request, method: "GET" | "HEAD") {
|
||||
return NextResponse.json({ success: false, error: "仅支持 http/https 模型地址" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (normalizeLegacyAliyunStorageUrl(targetUrl.toString())) {
|
||||
try {
|
||||
const { resolveLegacyAliyunStorageUrl } = await import("@/lib/storage/image-archive-db");
|
||||
targetUrl = new URL(await resolveLegacyAliyunStorageUrl(targetUrl.toString()));
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message.includes("HTTP 410")) {
|
||||
return NextResponse.json({ success: false, error: RETIRED_STORAGE_MESSAGE, code: "storage_retired" }, { status: 410 });
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isAllowedModelHost(targetUrl.hostname)) {
|
||||
return NextResponse.json({ success: false, error: "不允许的模型地址域名" }, { status: 403 });
|
||||
}
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
import crypto from "node:crypto";
|
||||
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||
import { isRetiredStorageUrl, normalizeLegacyAliyunStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { formatDownloadFailure, getDownloadFailure } from "@/lib/canvas/download-error";
|
||||
import { createLogger } from "@/lib/observability/logger";
|
||||
import { buildImageProxyUrl, isAlibabaModelImageUrl } from "@/lib/image-delivery";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
const logger = createLogger("image.download");
|
||||
@ -242,6 +243,7 @@ function resolveDirectImageTarget(
|
||||
requestedQuality: number | null,
|
||||
requestedFormat: string | null
|
||||
): URL | null {
|
||||
if (isAlibabaModelImageUrl(target.toString())) return null;
|
||||
if (!isBrowserImageRequest(request)) {
|
||||
return null;
|
||||
}
|
||||
@ -298,6 +300,24 @@ export async function GET(request: NextRequest) {
|
||||
return NextResponse.json({ error: "Only http/https is supported" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (normalizeLegacyAliyunStorageUrl(target.toString())) {
|
||||
try {
|
||||
if (searchParams.get("original") === "1") {
|
||||
const { buildImageProxyUrl } = await import("@/lib/image-delivery");
|
||||
buildImageProxyUrl(target.toString(), { delivery: "original" });
|
||||
}
|
||||
const { resolveLegacyAliyunStorageUrl } = await import("@/lib/storage/image-archive-db");
|
||||
target = new URL(await resolveLegacyAliyunStorageUrl(target.toString()));
|
||||
} catch (error) {
|
||||
if (searchParams.get("original") === "1") return downloadFailure(error);
|
||||
const failure = getDownloadFailure(error);
|
||||
return NextResponse.json({ error: formatDownloadFailure(failure, requestId), failure, requestId,
|
||||
code: failure.code === "STORAGE_RETIRED" ? "storage_retired" : failure.code }, {
|
||||
status: failure.status, headers: { "Cache-Control": "no-store" },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (isBlockedHost(target.hostname)) {
|
||||
return NextResponse.json({ error: "Blocked target host" }, { status: 403 });
|
||||
}
|
||||
@ -318,6 +338,25 @@ export async function GET(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
if (!original && isAlibabaModelImageUrl(target.toString())) {
|
||||
try {
|
||||
const { getArchivedImageUrlMappings } = await import("@/lib/storage/image-archive-db");
|
||||
const mappings = await getArchivedImageUrlMappings([target.toString()]);
|
||||
const archived = mappings[target.toString()];
|
||||
if (archived) {
|
||||
const preview = buildImageProxyUrl(archived, {
|
||||
width: width || undefined,
|
||||
quality,
|
||||
format: ["webp", "avif", "jpeg", "png"].includes(requestedFormat || "") ? requestedFormat as OutputFormat : undefined,
|
||||
});
|
||||
if (/^https?:\/\//i.test(preview)) return redirectToDirectImage(new URL(preview));
|
||||
target = new URL(archived);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn("preview_archive_lookup_failed", error, { requestId });
|
||||
}
|
||||
}
|
||||
|
||||
if (original && searchParams.get("download") === "resolve") {
|
||||
try {
|
||||
const { buildImageProxyUrl } = await import("@/lib/image-delivery");
|
||||
@ -370,7 +409,7 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
const fetchSignal = AbortSignal.any([
|
||||
request.signal,
|
||||
AbortSignal.timeout(original ? 60_000 : width && width <= 256 ? SMALL_PREVIEW_FETCH_TIMEOUT_MS : FETCH_TIMEOUT_MS),
|
||||
AbortSignal.timeout(original ? 60_000 : isAlibabaModelImageUrl(target.toString()) || width && width <= 256 ? SMALL_PREVIEW_FETCH_TIMEOUT_MS : FETCH_TIMEOUT_MS),
|
||||
]);
|
||||
const upstreamFailure = (error: unknown) => {
|
||||
if (request.signal.aborted) return new NextResponse(null, { status: 499 });
|
||||
@ -381,6 +420,22 @@ export async function GET(request: NextRequest) {
|
||||
{ status: timedOut ? 504 : 502 },
|
||||
);
|
||||
};
|
||||
let contentType: string;
|
||||
let originalBuffer: Buffer;
|
||||
if (!original && isAlibabaModelImageUrl(target.toString())) {
|
||||
try {
|
||||
const { fetchImageBytes } = await import("@/lib/storage/image-byte-cache");
|
||||
const data = await fetchImageBytes(target.toString(), { timeoutMs: SMALL_PREVIEW_FETCH_TIMEOUT_MS });
|
||||
originalBuffer = data.buffer;
|
||||
contentType = data.contentType;
|
||||
if (request.signal.aborted) return new NextResponse(null, { status: 499 });
|
||||
if (!contentType.toLowerCase().startsWith("image/")) {
|
||||
return NextResponse.json({ error: "Upstream is not an image" }, { status: 400 });
|
||||
}
|
||||
} catch (error) {
|
||||
return upstreamFailure(error);
|
||||
}
|
||||
} else {
|
||||
let upstreamResponse: Response;
|
||||
try {
|
||||
upstreamResponse = await fetch(target.toString(), {
|
||||
@ -400,19 +455,17 @@ export async function GET(request: NextRequest) {
|
||||
{ status: 502 }
|
||||
);
|
||||
}
|
||||
|
||||
const contentType = upstreamResponse.headers.get("content-type") || "application/octet-stream";
|
||||
contentType = upstreamResponse.headers.get("content-type") || "application/octet-stream";
|
||||
if (!contentType.toLowerCase().startsWith("image/")) {
|
||||
if (original) return downloadFailure("Source is not an image");
|
||||
return NextResponse.json({ error: "Upstream is not an image" }, { status: 400 });
|
||||
}
|
||||
|
||||
let originalBuffer: Buffer;
|
||||
try {
|
||||
originalBuffer = Buffer.from(await upstreamResponse.arrayBuffer());
|
||||
} catch (error) {
|
||||
return upstreamFailure(error);
|
||||
}
|
||||
}
|
||||
if (original) {
|
||||
return buildImageResponse(originalBuffer, {
|
||||
contentType,
|
||||
|
||||
@ -81,7 +81,8 @@ import { canvasService } from "@/services/canvas";
|
||||
import {
|
||||
inferOriginalImageUrl,
|
||||
} from "@/lib/image-url";
|
||||
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
||||
import { buildImageFallbackUrl, buildImageProxyUrl } from "@/lib/image-delivery";
|
||||
import { ProgressiveImagePreview, type LoadedImagePreview } from "@/components/progressive-image-preview";
|
||||
import {
|
||||
isImageGenerationModelDescriptor,
|
||||
isVideoModelDescriptor,
|
||||
@ -3434,6 +3435,7 @@ export function CanvasChatPanel({
|
||||
const [fullscreenPreview, setFullscreenPreview] = useState<{
|
||||
url: string;
|
||||
alt?: string;
|
||||
preview: LoadedImagePreview;
|
||||
} | null>(null);
|
||||
const [downloadProgress, setDownloadProgress] =
|
||||
useState<CanvasImageDownloadProgress | null>(null);
|
||||
@ -3471,16 +3473,6 @@ export function CanvasChatPanel({
|
||||
const [downloadingOriginalUrl, setDownloadingOriginalUrl] = useState("");
|
||||
const [addingClawdImageUrl, setAddingClawdImageUrl] = useState("");
|
||||
const downloadAbortControllerRef = useRef<AbortController | null>(null);
|
||||
const fullscreenPreviewSrc = useMemo(() => {
|
||||
if (!fullscreenPreview?.url) return "";
|
||||
const originalUrl = inferOriginalImageUrl(fullscreenPreview.url);
|
||||
return buildImageProxyUrl(originalUrl || fullscreenPreview.url, {
|
||||
delivery: "proxy",
|
||||
width: 2400,
|
||||
quality: 90,
|
||||
format: "webp",
|
||||
});
|
||||
}, [fullscreenPreview]);
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (composerUploadClearTimeoutRef.current) {
|
||||
@ -15812,8 +15804,8 @@ export function CanvasChatPanel({
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== activeClawdSkillTag.iconUrl) {
|
||||
event.currentTarget.src = activeClawdSkillTag.iconUrl || "";
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(activeClawdSkillTag.iconUrl)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(activeClawdSkillTag.iconUrl);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
@ -16131,8 +16123,8 @@ export function CanvasChatPanel({
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== attachment.url) {
|
||||
event.currentTarget.src = attachment.url;
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(attachment.url)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(attachment.url);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
@ -16208,8 +16200,8 @@ export function CanvasChatPanel({
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== attachment.url) {
|
||||
event.currentTarget.src = attachment.url;
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(attachment.url)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(attachment.url);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
@ -16394,8 +16386,8 @@ export function CanvasChatPanel({
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== tag.iconUrl) {
|
||||
event.currentTarget.src = tag.iconUrl || "";
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(tag.iconUrl)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(tag.iconUrl);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
@ -16695,8 +16687,8 @@ export function CanvasChatPanel({
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== attachment.url) {
|
||||
event.currentTarget.src = attachment.url;
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(attachment.url)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(attachment.url);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
@ -17032,15 +17024,23 @@ export function CanvasChatPanel({
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== url) {
|
||||
event.currentTarget.src = url;
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(url)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(url);
|
||||
}
|
||||
}}
|
||||
onClick={() => setFullscreenPreview({ url })}
|
||||
onClick={(event) => setFullscreenPreview({ url, preview: {
|
||||
src: event.currentTarget.currentSrc || event.currentTarget.src,
|
||||
width: event.currentTarget.naturalWidth,
|
||||
height: event.currentTarget.naturalHeight,
|
||||
} })}
|
||||
onDoubleClick={(e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
setFullscreenPreview({ url });
|
||||
setFullscreenPreview({ url, preview: {
|
||||
src: e.currentTarget.currentSrc || e.currentTarget.src,
|
||||
width: e.currentTarget.naturalWidth,
|
||||
height: e.currentTarget.naturalHeight,
|
||||
} });
|
||||
}}
|
||||
/>
|
||||
<div className="absolute bottom-2 right-2 flex items-center gap-1.5">
|
||||
@ -17286,7 +17286,7 @@ export function CanvasChatPanel({
|
||||
onClick={() => setFullscreenPreview(null)}
|
||||
>
|
||||
<button
|
||||
className="absolute top-4 right-4 p-2 rounded-full bg-white/10 hover:bg-white/20 transition-colors"
|
||||
className="absolute top-4 right-4 z-10 p-2 rounded-full bg-white/10 hover:bg-white/20 transition-colors"
|
||||
onClick={() => setFullscreenPreview(null)}
|
||||
aria-label="关闭预览"
|
||||
>
|
||||
@ -17295,7 +17295,7 @@ export function CanvasChatPanel({
|
||||
</svg>
|
||||
</button>
|
||||
<button
|
||||
className="absolute top-4 right-16 inline-flex items-center gap-1 rounded-full bg-white/10 px-3 py-2 text-sm text-white transition-colors hover:bg-white/20 disabled:cursor-not-allowed disabled:opacity-65"
|
||||
className="absolute top-4 right-16 z-10 inline-flex items-center gap-1 rounded-full bg-white/10 px-3 py-2 text-sm text-white transition-colors hover:bg-white/20 disabled:cursor-not-allowed disabled:opacity-65"
|
||||
disabled={isDownloadingOriginalImage}
|
||||
onClick={(event) => {
|
||||
event.stopPropagation();
|
||||
@ -17312,13 +17312,11 @@ export function CanvasChatPanel({
|
||||
{isDownloadingOriginalImage ? "下载中" : "下载原图"}
|
||||
</button>
|
||||
|
||||
<img
|
||||
src={fullscreenPreviewSrc}
|
||||
<ProgressiveImagePreview
|
||||
src={inferOriginalImageUrl(fullscreenPreview.url) || fullscreenPreview.url}
|
||||
preview={fullscreenPreview.preview}
|
||||
alt={fullscreenPreview.alt || "图片预览"}
|
||||
className="max-w-[95vw] max-h-[95vh] object-contain"
|
||||
decoding="async"
|
||||
draggable={false}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
/>
|
||||
|
||||
<div className="absolute bottom-4 left-1/2 -translate-x-1/2 text-white/60 text-sm">
|
||||
@ -17885,8 +17883,8 @@ export function CanvasChatPanel({
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== slotAttachment.url) {
|
||||
event.currentTarget.src = slotAttachment.url;
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(slotAttachment.url)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(slotAttachment.url);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
@ -18210,8 +18208,8 @@ export function CanvasChatPanel({
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== activeClawdSkillTag.iconUrl) {
|
||||
event.currentTarget.src = activeClawdSkillTag.iconUrl || "";
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(activeClawdSkillTag.iconUrl)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(activeClawdSkillTag.iconUrl);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
@ -18470,8 +18468,8 @@ export function CanvasChatPanel({
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== item.url) {
|
||||
event.currentTarget.src = item.url;
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(item.url)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(item.url);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
@ -18655,8 +18653,8 @@ export function CanvasChatPanel({
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== slotAttachment.url) {
|
||||
event.currentTarget.src = slotAttachment.url;
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(slotAttachment.url)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(slotAttachment.url);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
@ -18723,8 +18721,8 @@ export function CanvasChatPanel({
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== slotAttachment.url) {
|
||||
event.currentTarget.src = slotAttachment.url;
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(slotAttachment.url)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(slotAttachment.url);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
@ -19423,8 +19421,8 @@ export function CanvasChatPanel({
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== tagPreview.url) {
|
||||
event.currentTarget.src = tagPreview.url;
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(tagPreview.url)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(tagPreview.url);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
|
||||
@ -448,7 +448,7 @@ function extractChatMediaUrls(text: string, kind: ChatMediaKind): string[] {
|
||||
}
|
||||
}
|
||||
|
||||
const markdownLinkPattern = /\[([^\]]*)]\((https?:\/\/[^)\s]+)\)/gi;
|
||||
const markdownLinkPattern = /(?<!!)\[([^\]]*)]\((https?:\/\/[^)\s]+)\)/gi;
|
||||
while ((match = markdownLinkPattern.exec(text)) !== null) {
|
||||
const label = String(match[1] || "");
|
||||
const hintedKind = /音频|语音|声音|audio|mp3/i.test(label)
|
||||
@ -491,7 +491,7 @@ export function stripMediaMarkup(text: string): string {
|
||||
.replace(/<audio[\s\S]*?<\/audio>/gi, "")
|
||||
.replace(/<audio\b[^>]*\/?\s*>/gi, "")
|
||||
.replace(/<a[\s\S]*?<\/a>/gi, "")
|
||||
.replace(/\[([^\]]*)]\((https?:\/\/[^)\s]+)\)/gi, (match, label, url) => {
|
||||
.replace(/(?<!!)\[([^\]]*)]\((https?:\/\/[^)\s]+)\)/gi, (match, label, url) => {
|
||||
const hintedMedia = /音频|语音|声音|audio|mp3|视频|影片|播放|video/i.test(
|
||||
String(label || "")
|
||||
);
|
||||
|
||||
@ -60,6 +60,7 @@ import { DetailUiDomRenderer } from "@/components/canvas/DetailUiDomRenderer";
|
||||
import { ReferenceImageToolbar } from "@/components/canvas/ReferenceImageToolbar";
|
||||
import { CanvasToolbarIcon } from "@/components/canvas/CanvasToolbarIcon";
|
||||
import { CanvasImageDownloadProgressDialog } from "@/components/canvas/CanvasImageDownloadProgressDialog";
|
||||
import { ProgressiveImagePreview, type LoadedImagePreview } from "@/components/progressive-image-preview";
|
||||
import {
|
||||
areImageAdjustmentsEqual,
|
||||
buildImageAdjustmentFilterStyle,
|
||||
@ -186,7 +187,7 @@ import {
|
||||
normalizeDetailUiSubjectAnalysis,
|
||||
} from "@/lib/detail-ui-subject-analysis";
|
||||
import type { DetailUiScreenOverlaySnapshot } from "@/lib/detail-ui-surface-adapter";
|
||||
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
||||
import { buildImageFallbackUrl, buildImageProxyUrl } from "@/lib/image-delivery";
|
||||
import {
|
||||
DEFAULT_PUBLIC_THREED_MODEL_ALIAS_CONFIG,
|
||||
getDefaultThreeDModelAliasId,
|
||||
@ -1711,6 +1712,7 @@ export function ImageNode({
|
||||
const [isUploading, setIsUploading] = useState(false);
|
||||
const [annotatorOpen, setAnnotatorOpen] = useState(false);
|
||||
const [fullscreenPreviewOpen, setFullscreenPreviewOpen] = useState(false);
|
||||
const [fullscreenPreviewImage, setFullscreenPreviewImage] = useState<LoadedImagePreview | null>(null);
|
||||
const [downloadProgress, setDownloadProgress] =
|
||||
useState<CanvasImageDownloadProgress | null>(null);
|
||||
const downloadAbortControllerRef = useRef<AbortController | null>(null);
|
||||
@ -8466,8 +8468,8 @@ export function ImageNode({
|
||||
draggable={false}
|
||||
loading="lazy"
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== data.src) {
|
||||
event.currentTarget.src = data.src;
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(data.src)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(data.src);
|
||||
}
|
||||
}}
|
||||
onLoad={handleDisplayImageLoad}
|
||||
@ -8495,8 +8497,8 @@ export function ImageNode({
|
||||
draggable={false}
|
||||
loading="lazy"
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== compareSource) {
|
||||
event.currentTarget.src = compareSource;
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(compareSource)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(compareSource);
|
||||
}
|
||||
}}
|
||||
src={displayCompareSrc}
|
||||
@ -8559,12 +8561,17 @@ export function ImageNode({
|
||||
onDoubleClick={(e) => {
|
||||
if (!isThisNodePicking && !boxCutoutMode && !localEditMode) {
|
||||
e.stopPropagation();
|
||||
setFullscreenPreviewImage({
|
||||
src: e.currentTarget.currentSrc || e.currentTarget.src,
|
||||
width: e.currentTarget.naturalWidth,
|
||||
height: e.currentTarget.naturalHeight,
|
||||
});
|
||||
setFullscreenPreviewOpen(true);
|
||||
}
|
||||
}}
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== data.src) {
|
||||
event.currentTarget.src = data.src;
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(data.src)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(data.src);
|
||||
}
|
||||
}}
|
||||
onLoad={handleDisplayImageLoad}
|
||||
@ -11964,6 +11971,7 @@ export function ImageNode({
|
||||
{/* 双击放大预览弹窗 */}
|
||||
{fullscreenPreviewOpen &&
|
||||
data.src &&
|
||||
fullscreenPreviewImage &&
|
||||
createPortal(
|
||||
<div
|
||||
className="fixed inset-0 z-[9999] flex cursor-zoom-out items-center justify-center bg-black/90"
|
||||
@ -11971,7 +11979,7 @@ export function ImageNode({
|
||||
>
|
||||
{/* 关闭按钮 */}
|
||||
<button
|
||||
className="absolute top-4 right-4 rounded-full bg-slate-950 p-2 transition-colors hover:bg-black"
|
||||
className="absolute top-4 right-4 z-10 rounded-full bg-slate-950 p-2 transition-colors hover:bg-black"
|
||||
onClick={() => setFullscreenPreviewOpen(false)}
|
||||
>
|
||||
<svg
|
||||
@ -11989,7 +11997,7 @@ export function ImageNode({
|
||||
</svg>
|
||||
</button>
|
||||
<button
|
||||
className="absolute top-4 right-16 inline-flex items-center gap-1.5 rounded-full bg-slate-950 px-3 py-2 text-sm text-white transition-colors hover:bg-black disabled:cursor-not-allowed disabled:opacity-60"
|
||||
className="absolute top-4 right-16 z-10 inline-flex items-center gap-1.5 rounded-full bg-slate-950 px-3 py-2 text-sm text-white transition-colors hover:bg-black disabled:cursor-not-allowed disabled:opacity-60"
|
||||
disabled={isDownloadingOriginalImage}
|
||||
onClick={(event) => {
|
||||
event.preventDefault();
|
||||
@ -12008,13 +12016,11 @@ export function ImageNode({
|
||||
</button>
|
||||
|
||||
{/* 图片 */}
|
||||
<img
|
||||
<ProgressiveImagePreview
|
||||
alt={data.prompt || "图片预览"}
|
||||
className="max-h-[95vh] max-w-[95vw] object-contain"
|
||||
decoding="async"
|
||||
draggable={false}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
src={fullscreenPreviewSrc}
|
||||
src={resolveOriginalImageUrl(data.src) || data.src}
|
||||
preview={fullscreenPreviewImage}
|
||||
style={activeImageAdjustmentsStyle}
|
||||
/>
|
||||
|
||||
|
||||
@ -15,7 +15,7 @@ import {
|
||||
splitOpenClawChartBlocks,
|
||||
} from "@/components/elements/openclaw-chart-card";
|
||||
import { ResponsePdfDownloadButton } from "@/components/elements/response-pdf-download";
|
||||
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
||||
import { buildImageFallbackUrl, buildImageProxyUrl } from "@/lib/image-delivery";
|
||||
import { splitMarkdownImages } from "@/lib/canvas/markdown-image-segments";
|
||||
import { stableStreamingMarkdown } from "@/lib/canvas/markdown-image-streaming";
|
||||
import { getGatewayErrorPageMessage } from "@/lib/request-error";
|
||||
@ -442,8 +442,8 @@ function MarkdownTableImagePreview({
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== src) {
|
||||
event.currentTarget.src = src;
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(src)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(src);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
@ -473,8 +473,8 @@ function MarkdownTableImagePreview({
|
||||
decoding="async"
|
||||
onClick={(event) => event.stopPropagation()}
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== originalSrc) {
|
||||
event.currentTarget.src = originalSrc;
|
||||
if (event.currentTarget.src !== buildImageFallbackUrl(originalSrc)) {
|
||||
event.currentTarget.src = buildImageFallbackUrl(originalSrc);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
"use client";
|
||||
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||
import { isRetiredStorageUrl, normalizeLegacyAliyunStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||
|
||||
import dynamic from "next/dynamic";
|
||||
import React, { useState, useEffect } from "react";
|
||||
@ -313,7 +313,7 @@ export function ImagePreview({
|
||||
event.currentTarget.alt = "Generated Image";
|
||||
}}
|
||||
onError={(event) => {
|
||||
if (event.currentTarget.src !== (thumbnail || original)) {
|
||||
if (!normalizeLegacyAliyunStorageUrl(thumbnail || original) && event.currentTarget.src !== (thumbnail || original)) {
|
||||
event.currentTarget.src = thumbnail || original;
|
||||
} else if (!compactGrid) {
|
||||
event.currentTarget.style.aspectRatio = "1";
|
||||
@ -610,7 +610,7 @@ export function ImagePreview({
|
||||
onClick={(e: React.MouseEvent) => e.stopPropagation()}
|
||||
>
|
||||
<img
|
||||
src={original}
|
||||
src={normalizeLegacyAliyunStorageUrl(original) ? buildImageProxyUrl(original) : original}
|
||||
alt="Generated Image - Original"
|
||||
className={`max-h-[90vh] max-w-full rounded-lg object-contain ${
|
||||
isAnnotationMode ? "cursor-crosshair" : ""
|
||||
|
||||
74
components/progressive-image-preview.tsx
Normal file
74
components/progressive-image-preview.tsx
Normal file
@ -0,0 +1,74 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState, type CSSProperties, type MouseEventHandler } from "react";
|
||||
import { buildImageProxyUrl, getFullscreenPreviewSize } from "@/lib/image-delivery";
|
||||
|
||||
export type LoadedImagePreview = { src: string; width: number; height: number };
|
||||
|
||||
/** Keep the already displayed image visible while its larger preview loads. */
|
||||
export function ProgressiveImagePreview({
|
||||
src,
|
||||
preview,
|
||||
alt,
|
||||
className,
|
||||
style,
|
||||
onClick,
|
||||
}: {
|
||||
src: string;
|
||||
preview: LoadedImagePreview;
|
||||
alt: string;
|
||||
className?: string;
|
||||
style?: CSSProperties;
|
||||
onClick?: MouseEventHandler<HTMLImageElement>;
|
||||
}) {
|
||||
const size = getFullscreenPreviewSize({
|
||||
imageWidth: preview.width,
|
||||
imageHeight: preview.height,
|
||||
viewportWidth: typeof window === "undefined" ? 1280 : window.innerWidth,
|
||||
viewportHeight: typeof window === "undefined" ? 900 : window.innerHeight,
|
||||
pixelRatio: typeof window === "undefined" ? 1 : window.devicePixelRatio,
|
||||
});
|
||||
const fullSrc = buildImageProxyUrl(src, {
|
||||
delivery: "proxy",
|
||||
width: size.requestWidth,
|
||||
quality: 86,
|
||||
format: "webp",
|
||||
});
|
||||
const [loadedSrc, setLoadedSrc] = useState("");
|
||||
const [failedSrc, setFailedSrc] = useState("");
|
||||
const ready = fullSrc === preview.src || loadedSrc === fullSrc;
|
||||
const failed = failedSrc === fullSrc;
|
||||
|
||||
useEffect(() => {
|
||||
if (fullSrc === preview.src) return;
|
||||
const image = new Image();
|
||||
image.decoding = "async";
|
||||
image.onload = () => setLoadedSrc(fullSrc);
|
||||
image.onerror = () => setFailedSrc(fullSrc);
|
||||
image.src = fullSrc;
|
||||
return () => {
|
||||
image.onload = null;
|
||||
image.onerror = null;
|
||||
image.removeAttribute("src");
|
||||
};
|
||||
}, [fullSrc, preview.src]);
|
||||
|
||||
return (
|
||||
<div className="relative flex items-center justify-center" onClick={(event) => event.stopPropagation()}>
|
||||
<img
|
||||
alt={alt}
|
||||
className={className}
|
||||
decoding="async"
|
||||
draggable={false}
|
||||
onClick={onClick}
|
||||
src={ready ? fullSrc : preview.src}
|
||||
style={{ width: size.displayWidth, ...style }}
|
||||
/>
|
||||
{!ready && (
|
||||
<div role="status" className="pointer-events-none absolute bottom-4 max-w-full rounded-lg bg-black/80 px-3 py-2 text-center text-sm text-white">
|
||||
{failed ? "高清预览加载失败,已保留当前预览;可关闭后重试或下载原图" : "正在加载高清预览…"}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -14,6 +14,7 @@ import { isAlibabaImageTextToImageModelId } from "@/lib/ai/model-classification"
|
||||
import { buildReferenceImageOrderPrompt } from "@/lib/ai/providers/reference-image-order";
|
||||
import { createDeferredUploadedImage } from "@/lib/storage/image-archive";
|
||||
import { uploadFile } from "@/lib/storage/service";
|
||||
import { normalizeLegacyAliyunStorageUrl } from "@/lib/storage/retired-storage";
|
||||
import {
|
||||
isAlibabaQwenImage3ModelId,
|
||||
resolveAlibabaImageAsyncEndpoint,
|
||||
@ -129,11 +130,16 @@ async function fetchImageBufferWithRetry(
|
||||
if (!match) throw new Error("输入图片数据格式无效");
|
||||
return { buffer: Buffer.from(match[2], "base64"), contentType: match[1] };
|
||||
}
|
||||
let sourceUrl = imageUrl;
|
||||
if (normalizeLegacyAliyunStorageUrl(sourceUrl)) {
|
||||
const { resolveLegacyAliyunStorageUrl } = await import("@/lib/storage/image-archive-db");
|
||||
sourceUrl = await resolveLegacyAliyunStorageUrl(sourceUrl);
|
||||
}
|
||||
let lastError: unknown = null;
|
||||
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
||||
const ctx = createAbortSignal(timeoutMs);
|
||||
try {
|
||||
const response = await fetch(imageUrl, {
|
||||
const response = await fetch(sourceUrl, {
|
||||
cache: "no-store",
|
||||
redirect: "follow",
|
||||
signal: ctx.signal,
|
||||
|
||||
@ -45,6 +45,7 @@ import {
|
||||
import { uploadFile } from "@/lib/storage/service";
|
||||
import { isImageModelBalanceBlocked, resolveConfiguredImageBalanceFallback } from "@/lib/ai/runtime/image-balance";
|
||||
import { isProviderBalanceError } from "@/lib/request-error";
|
||||
import { normalizeLegacyAliyunStorageUrl } from "@/lib/storage/retired-storage";
|
||||
|
||||
export interface CanvasLocalEditCompositeRegion {
|
||||
x: number;
|
||||
@ -4632,10 +4633,10 @@ function findCanvasFailoverModel(params: {
|
||||
}
|
||||
|
||||
export async function generateCanvasImage(
|
||||
request: CanvasGenerateImageRequest
|
||||
input: CanvasGenerateImageRequest
|
||||
): Promise<CanvasGenerateImageResult> {
|
||||
const startedAt = Date.now();
|
||||
if (request.agentImageMode === "pinned") request = { ...request, allowFailover: false };
|
||||
const request = input.agentImageMode === "pinned" ? { ...input, allowFailover: false } : input;
|
||||
const prompt = String(request.prompt || "").trim();
|
||||
if (!prompt) {
|
||||
throw new Error("请输入提示词");
|
||||
@ -4873,11 +4874,16 @@ export async function generateCanvasImage(
|
||||
typeof request.ownerKey === "string" && request.ownerKey.trim()
|
||||
? request.ownerKey.trim()
|
||||
: undefined;
|
||||
const referenceImage =
|
||||
typeof request.referenceImage === "string" ? request.referenceImage : undefined;
|
||||
const referenceImages = Array.isArray(request.referenceImages)
|
||||
const resolveReference = async (url: string) => {
|
||||
if (!normalizeLegacyAliyunStorageUrl(url)) return url;
|
||||
const { resolveLegacyAliyunStorageUrl } = await import("@/lib/storage/image-archive-db");
|
||||
return resolveLegacyAliyunStorageUrl(url);
|
||||
};
|
||||
const referenceImage = typeof request.referenceImage === "string"
|
||||
? await resolveReference(request.referenceImage) : undefined;
|
||||
const referenceImages = await Promise.all((Array.isArray(request.referenceImages)
|
||||
? request.referenceImages
|
||||
: [];
|
||||
: []).map(resolveReference));
|
||||
const debugLabel = String(request.debugLabel || "").trim() || "canvas-image";
|
||||
const debugMeta =
|
||||
request.debugMeta && typeof request.debugMeta === "object"
|
||||
|
||||
@ -1,5 +1,17 @@
|
||||
import { normalizeLegacyAliyunStorageUrl } from "./storage/retired-storage";
|
||||
|
||||
const IMAGE_PROXY_PATH = "/api/image-proxy";
|
||||
|
||||
/** Error fallbacks must keep unavailable legacy sources behind archive resolution. */
|
||||
export function buildImageFallbackUrl(value: string | null | undefined): string {
|
||||
const source = value || "";
|
||||
const legacy = normalizeLegacyAliyunStorageUrl(source);
|
||||
const modelImage = isAlibabaModelImageUrl(source);
|
||||
if (!legacy && !modelImage) return source;
|
||||
const proxy = buildImageProxyUrl(legacy || source, modelImage ? { width: 960, quality: 86, format: "webp" } : {});
|
||||
return typeof window === "undefined" ? proxy : new URL(proxy, window.location.origin).toString();
|
||||
}
|
||||
|
||||
type ImageProxyFormat = "webp" | "avif" | "jpeg" | "png";
|
||||
|
||||
export type ImageProxyOptions = {
|
||||
@ -9,6 +21,26 @@ export type ImageProxyOptions = {
|
||||
delivery?: "display" | "proxy" | "original";
|
||||
};
|
||||
|
||||
export function getFullscreenPreviewSize({
|
||||
imageWidth,
|
||||
imageHeight,
|
||||
viewportWidth,
|
||||
viewportHeight,
|
||||
pixelRatio = 1,
|
||||
}: {
|
||||
imageWidth: number;
|
||||
imageHeight: number;
|
||||
viewportWidth: number;
|
||||
viewportHeight: number;
|
||||
pixelRatio?: number;
|
||||
}) {
|
||||
const ratio = imageWidth > 0 && imageHeight > 0 ? imageWidth / imageHeight : 1;
|
||||
const displayWidth = Math.min(viewportWidth * 0.95, viewportHeight * 0.95 * ratio);
|
||||
const requiredWidth = displayWidth * Math.max(1, pixelRatio || 1);
|
||||
const requestWidth = [640, 960, 1280, 1600, 2048, 2560, 3200, 4096].find((width) => width >= requiredWidth) || 4096;
|
||||
return { displayWidth, requestWidth };
|
||||
}
|
||||
|
||||
/** Preserve the actual response format when naming an original download. */
|
||||
export function getImageDownloadExtension(contentType: string) {
|
||||
const mime = contentType.split(";")[0]?.trim().toLowerCase();
|
||||
@ -59,6 +91,15 @@ function isAliyunOssImageHost(hostname: string): boolean {
|
||||
return normalized.endsWith(".aliyuncs.com") && normalized.includes(".oss-");
|
||||
}
|
||||
|
||||
/** Model result buckets allow downloading objects but deny OSS image processing. */
|
||||
export function isAlibabaModelImageUrl(value: string): boolean {
|
||||
try {
|
||||
return /^dashscope(?:-[a-z0-9-]+)?\.oss-[a-z0-9-]+\.aliyuncs\.com$/i.test(new URL(value).hostname);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function isTrustedDirectStorageImageHost(hostname: string): boolean {
|
||||
const normalized = hostname.trim().toLowerCase();
|
||||
let configuredCdnHost = "";
|
||||
@ -200,7 +241,7 @@ export function buildImageProxyUrl(url: string, options: ImageProxyOptions = {})
|
||||
return trustedDirectStorageUrl;
|
||||
}
|
||||
|
||||
if (options.delivery !== "proxy") {
|
||||
if (options.delivery !== "proxy" && !normalizeLegacyAliyunStorageUrl(source) && !isAlibabaModelImageUrl(source)) {
|
||||
const directDisplayUrl = resolveAliyunOssDisplayUrl(source, options);
|
||||
if (directDisplayUrl) {
|
||||
return directDisplayUrl;
|
||||
|
||||
@ -243,6 +243,9 @@ export function getRequestFailureDetails(
|
||||
if (/content.?polic|moderation|safety|policy_rejected|内容(?:敏感|审核|限制)|不能生成像那样|不能生成那样|违规/i.test(text)) {
|
||||
return detail("CONTENT_POLICY_REJECTED", "模型服务因内容审核限制拒绝了本次请求。", "请调整描述或参考图中的受限内容后再提交;重复提交相同内容通常无法解决。");
|
||||
}
|
||||
if (/旧存储已停用|storage_retired/i.test(text)) {
|
||||
return detail("SOURCE_STORAGE_RETIRED", "参考文件所在的旧存储已停用,且没有可用的归档副本。", "请重新上传原始文件后再提交;切换模型或重复提交无法恢复该文件。");
|
||||
}
|
||||
if (/上传到存储桶失败|存储.*失败|归档.*失败|archive.*failed|storage.*failed/i.test(text)) {
|
||||
return detail("RESULT_STORAGE_FAILED", "处理结果保存失败,生成结果可能已经存在。", "请保留当前页面并联系管理员提供请求编号,先不要重复生成。", "unknown");
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import "server-only";
|
||||
import { isRetiredStorageUrl } from "./retired-storage";
|
||||
import { isRetiredStorageUrl, normalizeLegacyAliyunStorageUrl, RETIRED_STORAGE_MESSAGE } from "./retired-storage";
|
||||
|
||||
import { createHash } from "node:crypto";
|
||||
import {
|
||||
@ -22,6 +22,8 @@ export function getImageArchiveDisplayHoldMs() {
|
||||
}
|
||||
|
||||
export function imageArchiveSourceKey(source: string) {
|
||||
const legacy = normalizeLegacyAliyunStorageUrl(source);
|
||||
if (legacy) return legacy;
|
||||
if (!source.startsWith("data:")) return source;
|
||||
const configured =
|
||||
process.env.NEXT_PUBLIC_APP_URL || process.env.AUTH_URL || process.env.NEXTAUTH_URL;
|
||||
@ -39,7 +41,7 @@ export async function enqueueImageArchiveTask(params: {
|
||||
metadata?: Record<string, unknown> | null;
|
||||
}) {
|
||||
const sourceUrl = String(params.sourceUrl || "").trim();
|
||||
if (!isGeneratedImageSource(sourceUrl) || isRetiredStorageUrl(sourceUrl)) return null;
|
||||
if (!isGeneratedImageSource(sourceUrl) || isRetiredStorageUrl(sourceUrl) || normalizeLegacyAliyunStorageUrl(sourceUrl)) return null;
|
||||
|
||||
const now = new Date();
|
||||
const archiveAfterAt = params.archiveAfterAt || now;
|
||||
@ -312,6 +314,18 @@ export async function getArchivedImageUrlMappings(
|
||||
return replacements;
|
||||
}
|
||||
|
||||
/** Legacy OSS is unavailable; only an existing durable copy can serve its URLs. */
|
||||
export async function resolveLegacyAliyunStorageUrl(source: string): Promise<string> {
|
||||
const legacy = normalizeLegacyAliyunStorageUrl(source);
|
||||
if (!legacy) return source;
|
||||
const mappings = await getArchivedImageUrlMappings([source, legacy]);
|
||||
const target = mappings[source] || mappings[legacy];
|
||||
if (!target || normalizeLegacyAliyunStorageUrl(target) || isRetiredStorageUrl(target)) {
|
||||
throw new Error(`HTTP 410: ${RETIRED_STORAGE_MESSAGE}`);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
// Normalize late writes too: an archive can finish before a task/canvas save arrives.
|
||||
export async function resolveArchivedImageUrls<T>(
|
||||
value: T,
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
// Shared by the server and canvas; no storage or network work happens here.
|
||||
export type ArchivedImageUrlMap = Record<string, string>;
|
||||
const legacyOssUrlPattern = /https?:\/\/demo-sparkai\.oss-cn-shanghai\.aliyuncs\.com\/[^\s"'<>`\\)\]}|]+/gi;
|
||||
|
||||
export function isGeneratedImageSource(value: string) {
|
||||
return (
|
||||
@ -11,6 +12,7 @@ export function collectImageArchiveSources(value: unknown): string[] {
|
||||
const urls = new Set<string>();
|
||||
const visit = (item: unknown) => {
|
||||
if (typeof item === "string") {
|
||||
for (const match of item.matchAll(legacyOssUrlPattern)) urls.add(match[0].replace(/,+$/, ""));
|
||||
if (isGeneratedImageSource(item)) urls.add(item);
|
||||
else
|
||||
for (const match of item.matchAll(/!\[[^\]]*\]\(([^)]+)\)/g)) {
|
||||
@ -35,6 +37,10 @@ export function replaceArchivedImageUrls<T>(value: T, replacements: ArchivedImag
|
||||
.map((part) => replacements[part] || part)
|
||||
.join("|||");
|
||||
return ``;
|
||||
}).replace(legacyOssUrlPattern, (url) => {
|
||||
if (replacements[url]) return replacements[url];
|
||||
const source = url.replace(/,+$/, "");
|
||||
return (replacements[source] || source) + url.slice(source.length);
|
||||
}) as T;
|
||||
}
|
||||
if (!value || typeof value !== "object" || value instanceof Date) return value;
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import "server-only";
|
||||
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "./retired-storage";
|
||||
import { isRetiredStorageUrl, normalizeLegacyAliyunStorageUrl, RETIRED_STORAGE_MESSAGE } from "./retired-storage";
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { mkdir, readFile, writeFile, rename, readdir, stat, statfs, unlink } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
@ -83,13 +83,18 @@ function isMediaResponse(data: ImageBytes) {
|
||||
}
|
||||
export async function fetchImageBytes(url: string, options: { headers?: HeadersInit; timeoutMs?: number; scope?: string; signal?: AbortSignal } = {}) {
|
||||
if (isRetiredStorageUrl(url)) throw new Error(`HTTP 410: ${RETIRED_STORAGE_MESSAGE}`);
|
||||
return onceImageBytes(`source:${options.scope || ""}:${url}`, async () => {
|
||||
if (url.startsWith("data:")) {
|
||||
const match = url.match(/^data:(image\/[a-z0-9.+-]+);base64,([a-z0-9+/=]+)$/i);
|
||||
let sourceUrl = url;
|
||||
if (normalizeLegacyAliyunStorageUrl(url)) {
|
||||
const { resolveLegacyAliyunStorageUrl } = await import("./image-archive-db");
|
||||
sourceUrl = await resolveLegacyAliyunStorageUrl(url);
|
||||
}
|
||||
return onceImageBytes(`source:${options.scope || ""}:${sourceUrl}`, async () => {
|
||||
if (sourceUrl.startsWith("data:")) {
|
||||
const match = sourceUrl.match(/^data:(image\/[a-z0-9.+-]+);base64,([a-z0-9+/=]+)$/i);
|
||||
if (!match) throw new Error("Invalid inline generated image");
|
||||
return { buffer: Buffer.from(match[2], "base64"), contentType: match[1] };
|
||||
}
|
||||
const response = await fetch(url, {
|
||||
const response = await fetch(sourceUrl, {
|
||||
headers: options.headers, cache: "no-store", redirect: "follow",
|
||||
// The deadline covers the body as well as the response headers.
|
||||
signal: options.signal
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import "server-only";
|
||||
import { isRetiredStorageUrl } from "./retired-storage";
|
||||
import { isRetiredStorageUrl, isLegacyAliyunStorageConfig, normalizeLegacyAliyunStorageUrl } from "./retired-storage";
|
||||
|
||||
import { and, asc, desc, eq, inArray, lte, sql } from "drizzle-orm";
|
||||
import { db } from "@/lib/db";
|
||||
@ -19,6 +19,7 @@ function isUsableS3Config(config: StorageConfig | null | undefined): config is S
|
||||
config.isEnabled &&
|
||||
!isRetiredStorageUrl(config.endpoint) &&
|
||||
!isRetiredStorageUrl(config.publicUrl) &&
|
||||
!isLegacyAliyunStorageConfig(config) &&
|
||||
String(config.endpoint || "").trim() &&
|
||||
String(config.bucket || "").trim() &&
|
||||
String(config.accessKeyId || "").trim() &&
|
||||
@ -83,7 +84,7 @@ export async function enqueueStorageMigrationTask({
|
||||
migrateAfterAt: Date;
|
||||
cleanupAfterAt: Date;
|
||||
}) {
|
||||
if (!sourceUrl || !sourcePath || !sourceStorageConfigId || isRetiredStorageUrl(sourceUrl)) return;
|
||||
if (!sourceUrl || !sourcePath || !sourceStorageConfigId || isRetiredStorageUrl(sourceUrl) || normalizeLegacyAliyunStorageUrl(sourceUrl)) return;
|
||||
|
||||
await db
|
||||
.insert(storageMigrationTask)
|
||||
|
||||
@ -2,6 +2,29 @@
|
||||
export const RETIRED_STORAGE_URL_PATTERN = '^https?://s3[.]1080p[.]hk([/:?#]|$)';
|
||||
export const RETIRED_STORAGE_MESSAGE = '旧存储已停用,源文件已无法恢复,请重新上传。';
|
||||
|
||||
export function normalizeLegacyAliyunStorageUrl(value: string | null | undefined): string | null {
|
||||
if (!value) return null;
|
||||
try {
|
||||
let url = new URL(value, 'https://local.invalid');
|
||||
if (url.pathname === '/api/image-proxy' || url.pathname === '/api/canvas/3d/model-proxy') {
|
||||
url = new URL(url.searchParams.get('url') || '');
|
||||
}
|
||||
if (url.hostname !== 'demo-sparkai.oss-cn-shanghai.aliyuncs.com') return null;
|
||||
url.protocol = 'https:';
|
||||
url.hash = '';
|
||||
for (const name of ['Signature', 'OSSAccessKeyId', 'Expires', 'security-token']) url.searchParams.delete(name);
|
||||
url.searchParams.sort();
|
||||
return url.toString();
|
||||
} catch { return null; }
|
||||
}
|
||||
|
||||
export function isLegacyAliyunStorageConfig(config: { bucket?: string | null; endpoint?: string | null; publicUrl?: string | null }): boolean {
|
||||
if (normalizeLegacyAliyunStorageUrl(config.publicUrl)) return true;
|
||||
try {
|
||||
return config.bucket === 'demo-sparkai' && new URL(config.endpoint || '').hostname.endsWith('.aliyuncs.com');
|
||||
} catch { return false; }
|
||||
}
|
||||
|
||||
export function isRetiredStorageUrl(value: string | null | undefined): boolean {
|
||||
if (!value) return false;
|
||||
try {
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import "server-only";
|
||||
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "./retired-storage";
|
||||
import { isRetiredStorageUrl, isLegacyAliyunStorageConfig, RETIRED_STORAGE_MESSAGE } from "./retired-storage";
|
||||
|
||||
import { DeleteObjectCommand, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
||||
import { put } from "@vercel/blob";
|
||||
@ -150,7 +150,7 @@ export function getContentType(filename: string): string {
|
||||
}
|
||||
|
||||
export function createS3Client(config: S3UploadConfig): S3Client {
|
||||
if (isRetiredStorageUrl(config.endpoint) || isRetiredStorageUrl(config.publicUrl)) {
|
||||
if (isRetiredStorageUrl(config.endpoint) || isRetiredStorageUrl(config.publicUrl) || isLegacyAliyunStorageConfig(config)) {
|
||||
throw new Error(RETIRED_STORAGE_MESSAGE);
|
||||
}
|
||||
const normalizedEndpoint = normalizeEndpoint(config.endpoint);
|
||||
@ -343,6 +343,7 @@ export function toDbS3UploadConfig(raw: unknown): S3UploadConfig | null {
|
||||
!value.isEnabled ||
|
||||
isRetiredStorageUrl(endpoint) ||
|
||||
isRetiredStorageUrl(String(value.publicUrl || "")) ||
|
||||
isLegacyAliyunStorageConfig({ bucket, endpoint, publicUrl: String(value.publicUrl || "") }) ||
|
||||
!endpoint ||
|
||||
!bucket ||
|
||||
!accessKeyId ||
|
||||
@ -370,7 +371,8 @@ function buildEnvS3Config(): S3UploadConfig | null {
|
||||
const accessKeyId = process.env.S3_ACCESS_KEY_ID?.trim() ?? "";
|
||||
const secretAccessKey = process.env.S3_SECRET_ACCESS_KEY?.trim() ?? "";
|
||||
|
||||
if (endpoint && !isRetiredStorageUrl(endpoint) && !isRetiredStorageUrl(process.env.S3_PUBLIC_URL) && bucket && accessKeyId && secretAccessKey) {
|
||||
if (endpoint && !isRetiredStorageUrl(endpoint) && !isRetiredStorageUrl(process.env.S3_PUBLIC_URL) &&
|
||||
!isLegacyAliyunStorageConfig({ bucket, endpoint, publicUrl: process.env.S3_PUBLIC_URL }) && bucket && accessKeyId && secretAccessKey) {
|
||||
return {
|
||||
endpoint,
|
||||
region: process.env.S3_REGION?.trim() || "auto",
|
||||
|
||||
@ -3,6 +3,8 @@ import { defineConfig } from "@playwright/test";
|
||||
export default defineConfig({
|
||||
testDir: "./tests",
|
||||
testMatch: [
|
||||
/routes\/legacy-oss-recovery\.test\.ts/,
|
||||
/routes\/retired-storage\.test\.ts/,
|
||||
/routes\/original-image-download\.test\.ts/,
|
||||
/routes\/canvas-download-speed\.test\.ts/,
|
||||
/routes\/canvas-download-recovery\.test\.ts/,
|
||||
|
||||
@ -1,4 +1,15 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
|
||||
test("image metadata containing video or audio paths remains an image", () => {
|
||||
for (const folder of ["video-canvas-demo", "audio-preview"]) {
|
||||
const url = `https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/${folder}/cover.png`;
|
||||
const markdown = ``;
|
||||
expect(extractVideoUrls(markdown)).toEqual([]);
|
||||
expect(extractAudioUrls(markdown)).toEqual([]);
|
||||
expect(extractAssistantImageUrls(markdown)).toContain(url);
|
||||
expect(stripMediaMarkup(markdown)).toBe(markdown);
|
||||
}
|
||||
});
|
||||
import {
|
||||
collectMessageImageUrls,
|
||||
extractAssistantImageUrls,
|
||||
|
||||
@ -2,9 +2,77 @@ import { expect, test } from "@playwright/test";
|
||||
import { NextRequest } from "next/server";
|
||||
import sharp from "sharp";
|
||||
import { GET } from "@/app/api/image-proxy/route";
|
||||
import { buildImageProxyUrl, getImageDownloadExtension } from "@/lib/image-delivery";
|
||||
import { buildImageProxyUrl, getFullscreenPreviewSize, getImageDownloadExtension } from "@/lib/image-delivery";
|
||||
import { serverLoader } from "../helpers/canvas-download-server-loader";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
test.describe("Qiniu image delivery", () => {
|
||||
test("model result buckets use our thumbnail endpoint without forbidden OSS transforms", () => {
|
||||
const source = "https://dashscope-a717.oss-accelerate.aliyuncs.com/output.png";
|
||||
const preview = new URL(buildImageProxyUrl(source, { width: 960, format: "webp" }), "https://app.example");
|
||||
expect(preview.pathname).toBe("/api/image-proxy");
|
||||
expect(preview.searchParams.get("url")).toBe(source);
|
||||
expect(preview.searchParams.has("x-oss-process")).toBe(false);
|
||||
expect(buildImageProxyUrl("https://other-bucket.oss-cn-shanghai.aliyuncs.com/image.png", { width: 960 })).toContain("x-oss-process=");
|
||||
});
|
||||
|
||||
test("a completed archive serves old model-result references from Qiniu immediately", async () => {
|
||||
const source = "https://dashscope-a717.oss-accelerate.aliyuncs.com/archived.png";
|
||||
const route = serverLoader({
|
||||
"next/server": { NextResponse },
|
||||
"@/lib/storage/image-archive-db": { getArchivedImageUrlMappings: async () => ({ [source]: "https://yybb.top/archived.png" }) },
|
||||
fetch: () => { throw new Error("Must use the completed archive"); },
|
||||
})("app/api/image-proxy/route.ts");
|
||||
const request = new URL("https://app.example/api/image-proxy");
|
||||
request.searchParams.set("url", source); request.searchParams.set("w", "960"); request.searchParams.set("f", "webp");
|
||||
const response = await route.GET(new NextRequest(request, { headers: { "sec-fetch-dest": "image" } }));
|
||||
expect(response.status).toBe(307);
|
||||
expect(response.headers.get("location")).toBe("https://yybb.top/archived.png?imageView2/2/w/960/format/webp/q/82");
|
||||
});
|
||||
|
||||
test("unarchived model previews share one original fetch with archiving and return resized WebP", async () => {
|
||||
const source = `https://dashscope-a717.oss-accelerate.aliyuncs.com/${crypto.randomUUID()}.png`;
|
||||
const original = await sharp({ create: { width: 1024, height: 1024, channels: 3, background: "#345678" } }).png().toBuffer();
|
||||
let requests = 0;
|
||||
const load = serverLoader({
|
||||
"next/server": { NextResponse },
|
||||
"@/lib/storage/image-archive-db": { getArchivedImageUrlMappings: async () => ({}) },
|
||||
fetch: async (url: string) => {
|
||||
expect(url).toBe(source); requests++;
|
||||
await new Promise(resolve => setTimeout(resolve, 30));
|
||||
return new Response(original, { headers: { "content-type": "image/png" } });
|
||||
},
|
||||
});
|
||||
const route = load("app/api/image-proxy/route.ts");
|
||||
const archiveRead = load("lib/storage/image-byte-cache.ts").fetchImageBytes(source);
|
||||
const responses = await Promise.all([100, 200].map(width => {
|
||||
const request = new URL("https://app.example/api/image-proxy");
|
||||
request.searchParams.set("url", source); request.searchParams.set("w", String(width)); request.searchParams.set("f", "webp");
|
||||
return route.GET(new NextRequest(request, { headers: { "sec-fetch-dest": "image" } }));
|
||||
}));
|
||||
expect((await archiveRead).buffer.equals(original)).toBe(true);
|
||||
expect(requests).toBe(1);
|
||||
for (let i = 0; i < responses.length; i++) {
|
||||
expect(responses[i].status).toBe(200);
|
||||
expect(responses[i].headers.get("content-type")).toBe("image/webp");
|
||||
expect((await sharp(Buffer.from(await responses[i].arrayBuffer())).metadata()).width).toBe([100, 200][i]);
|
||||
}
|
||||
});
|
||||
test("sizes a fullscreen square to its visible height and reuses the chat preview on standard density screens", () => {
|
||||
const size = getFullscreenPreviewSize({ imageWidth: 2048, imageHeight: 2048, viewportWidth: 1657, viewportHeight: 884 });
|
||||
expect(size.displayWidth).toBeCloseTo(839.8);
|
||||
expect(size.requestWidth).toBe(960);
|
||||
expect(buildImageProxyUrl("https://yybb.top/a.png", { width: size.requestWidth, quality: 86, format: "webp" }))
|
||||
.toBe(buildImageProxyUrl("https://yybb.top/a.png", { width: 960, quality: 86, format: "webp" }));
|
||||
});
|
||||
|
||||
test("preserves screen density and image aspect ratio without requesting oversized fixed previews", () => {
|
||||
expect(getFullscreenPreviewSize({ imageWidth: 2048, imageHeight: 2048, viewportWidth: 1657, viewportHeight: 884, pixelRatio: 2 }).requestWidth).toBe(2048);
|
||||
const portrait = getFullscreenPreviewSize({ imageWidth: 1000, imageHeight: 2000, viewportWidth: 1440, viewportHeight: 900, pixelRatio: 2 });
|
||||
expect(portrait.displayWidth).toBeCloseTo(427.5);
|
||||
expect(portrait.requestWidth).toBe(960);
|
||||
expect(getFullscreenPreviewSize({ imageWidth: 4000, imageHeight: 1000, viewportWidth: 5000, viewportHeight: 2000, pixelRatio: 2 }).requestWidth).toBe(4096);
|
||||
});
|
||||
test("reports a timeout while reading image bytes as a retryable response", async () => {
|
||||
const previousFetch = globalThis.fetch;
|
||||
globalThis.fetch = async () => new Response(new ReadableStream({
|
||||
|
||||
@ -17,6 +17,7 @@ test("known provider and platform failures explain their cause and next action",
|
||||
["generate image failed", "PROVIDER_EXECUTION_FAILED", "稍后重试"],
|
||||
["excessive system load", "PROVIDER_EXECUTION_FAILED", "稍后重试"],
|
||||
["get image url failed", "SOURCE_IMAGE_UNAVAILABLE", "重新上传"],
|
||||
["HTTP 410: 旧存储已停用,源文件已无法恢复,请重新上传。", "SOURCE_STORAGE_RETIRED", "重新上传"],
|
||||
["抠图处理失败", "PROCESSING_FAILED", "核对原任务"],
|
||||
["去水印处理失败", "PROCESSING_FAILED", "核对原任务"],
|
||||
["图片上传到存储桶失败", "RESULT_STORAGE_FAILED", "不要重复生成"],
|
||||
|
||||
137
tests/routes/legacy-oss-recovery.test.ts
Normal file
137
tests/routes/legacy-oss-recovery.test.ts
Normal file
@ -0,0 +1,137 @@
|
||||
import { test, expect } from "@playwright/test";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { serverLoader } from "../helpers/canvas-download-server-loader";
|
||||
import { normalizeLegacyAliyunStorageUrl, isLegacyAliyunStorageConfig, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||
import { collectImageArchiveSources, replaceArchivedImageUrls } from "@/lib/storage/image-archive-urls";
|
||||
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
||||
|
||||
const legacy = "https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/uploads/product.png";
|
||||
const permanent = "https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/uploads/product.png";
|
||||
|
||||
test("legacy identity preserves image variants while dropping expired OSS credentials", () => {
|
||||
const signed = legacy.replace("https:", "http:") + "?Signature=expired&Expires=1&OSSAccessKeyId=old&x-oss-process=image%2Fresize%2Cw_100#preview";
|
||||
expect(normalizeLegacyAliyunStorageUrl(signed)).toBe(legacy + "?x-oss-process=image%2Fresize%2Cw_100");
|
||||
expect(normalizeLegacyAliyunStorageUrl("/api/image-proxy?url=" + encodeURIComponent(signed))).toBe(normalizeLegacyAliyunStorageUrl(signed));
|
||||
for (const source of [permanent, "https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/image.png", "https://other-bucket.oss-cn-shanghai.aliyuncs.com/a.png"]) {
|
||||
expect(normalizeLegacyAliyunStorageUrl(source)).toBeNull();
|
||||
}
|
||||
expect(isLegacyAliyunStorageConfig({ bucket: "demo-sparkai", endpoint: "https://oss-cn-shanghai.aliyuncs.com" })).toBe(true);
|
||||
expect(isLegacyAliyunStorageConfig({ bucket: "other-bucket", endpoint: "https://oss-cn-shanghai.aliyuncs.com" })).toBe(false);
|
||||
});
|
||||
|
||||
test("late canvas saves replace legacy URLs embedded in SVG, HTML and Markdown originals", () => {
|
||||
const content = { svg: `<svg><image href="${legacy}"/></svg>`, html: `<img src='${legacy}'>`, text: `` };
|
||||
expect(collectImageArchiveSources(content)).toContain(legacy);
|
||||
const result = replaceArchivedImageUrls(content, { [legacy]: permanent });
|
||||
expect(JSON.stringify(result)).not.toContain("https://demo-sparkai.");
|
||||
expect(result.svg).toContain(permanent);
|
||||
expect(result.text).toBe(``);
|
||||
});
|
||||
|
||||
test("legacy URLs in comma-separated reference descriptions keep sentence punctuation", () => {
|
||||
const text = `Reference: ${legacy}, next reference`;
|
||||
expect(collectImageArchiveSources(text)).toContain(legacy);
|
||||
expect(replaceArchivedImageUrls(text, { [legacy]: permanent })).toBe(`Reference: ${permanent}, next reference`);
|
||||
});
|
||||
|
||||
test("legacy display URLs go through archive resolution while other Alibaba buckets keep working", () => {
|
||||
const url = buildImageProxyUrl(legacy, { width: 400 });
|
||||
expect(url).toMatch(/^\/api\/image-proxy\?/);
|
||||
expect(new URL(url, "https://app.example").searchParams.get("url")).toBe(legacy);
|
||||
expect(buildImageProxyUrl("https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/result.png", { width: 400 })).toMatch(/^\/api\/image-proxy\?/);
|
||||
});
|
||||
|
||||
test("browser image fallbacks remain on one stable archive URL instead of retrying the old source", () => {
|
||||
const delivery = serverLoader({ window: { location: { origin: "https://app.example" } } })("lib/image-delivery.ts");
|
||||
const fallback = delivery.buildImageFallbackUrl(legacy);
|
||||
expect(fallback).toMatch(/^https:\/\/app\.example\/api\/image-proxy\?/);
|
||||
expect(delivery.buildImageFallbackUrl(fallback)).toBe(fallback);
|
||||
expect(delivery.buildImageFallbackUrl(permanent)).toBe(permanent);
|
||||
});
|
||||
|
||||
function archiveLoader(rows: Array<{ sourceUrl: string; targetUrl: string | null }>) {
|
||||
return serverLoader({
|
||||
"@/lib/db": { db: { select: () => ({ from: () => ({ where: async () => rows }) }), insert: () => { throw new Error("Legacy sources must not be queued"); } } },
|
||||
"@/lib/db/schema": { imageArchiveTask: { sourceUrl: "source", targetUrl: "target" } },
|
||||
})("lib/storage/image-archive-db.ts");
|
||||
}
|
||||
|
||||
test("archive lookup serves signed legacy links from the durable Qiniu alias", async () => {
|
||||
const archive = archiveLoader([{ sourceUrl: legacy, targetUrl: permanent }]);
|
||||
expect(await archive.resolveLegacyAliyunStorageUrl(legacy + "?Expires=1&Signature=expired")).toBe(permanent);
|
||||
expect(await archive.resolveLegacyAliyunStorageUrl(permanent)).toBe(permanent);
|
||||
expect(await archive.enqueueImageArchiveTask({ sourceUrl: legacy })).toBeNull();
|
||||
});
|
||||
|
||||
test("legacy files without a durable copy fail permanently instead of fetching the old bucket", async () => {
|
||||
const archive = archiveLoader([]);
|
||||
await expect(archive.resolveLegacyAliyunStorageUrl(legacy)).rejects.toThrow("HTTP 410");
|
||||
let fetched = 0;
|
||||
const bytes = serverLoader({
|
||||
"./image-archive-db": { resolveLegacyAliyunStorageUrl: archive.resolveLegacyAliyunStorageUrl },
|
||||
fetch: () => { fetched++; throw new Error("Unexpected old-bucket request"); },
|
||||
})("lib/storage/image-byte-cache.ts");
|
||||
await expect(bytes.fetchImageBytes(legacy)).rejects.toThrow(RETIRED_STORAGE_MESSAGE);
|
||||
expect(fetched).toBe(0);
|
||||
});
|
||||
|
||||
test("byte reads use the resolved Qiniu object and preserve its bytes", async () => {
|
||||
const seen: string[] = [];
|
||||
const target = permanent + "?acceptance=" + crypto.randomUUID();
|
||||
const original = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10, 0, 1]);
|
||||
const bytes = serverLoader({
|
||||
"./image-archive-db": { resolveLegacyAliyunStorageUrl: async () => target },
|
||||
fetch: async (url: string) => { seen.push(url); return new Response(original, { headers: { "content-type": "image/png" } }); },
|
||||
})("lib/storage/image-byte-cache.ts");
|
||||
expect((await bytes.fetchImageBytes(legacy)).buffer.equals(original)).toBe(true);
|
||||
expect(seen).toEqual([target]);
|
||||
});
|
||||
|
||||
test("preview requests redirect to Qiniu after resolving a legacy alias", async () => {
|
||||
const route = serverLoader({
|
||||
"next/server": { NextResponse },
|
||||
"@/lib/storage/image-archive-db": { resolveLegacyAliyunStorageUrl: async () => permanent },
|
||||
fetch: () => { throw new Error("Legacy previews must not fetch the old bucket"); },
|
||||
})("app/api/image-proxy/route.ts");
|
||||
const response = await route.GET(new NextRequest("https://app.example/api/image-proxy?url=" + encodeURIComponent(legacy), { headers: { "sec-fetch-dest": "image" } }));
|
||||
expect(response.status).toBe(307);
|
||||
expect(response.headers.get("location")).toBe(permanent);
|
||||
});
|
||||
|
||||
test("original downloads resolve the archived file without reading the legacy source", async () => {
|
||||
const route = serverLoader({
|
||||
"next/server": { NextResponse },
|
||||
"@/lib/storage/image-archive-db": { resolveLegacyAliyunStorageUrl: async () => permanent },
|
||||
"@/lib/storage/original-image-download": { resolveOriginalCdnDownload: async (url: string) => {
|
||||
expect(url).toBe(permanent);
|
||||
return { delivery: "cdn", downloadUrl: permanent + "?attname=original.png", filename: "original.png" };
|
||||
} },
|
||||
fetch: () => { throw new Error("Unexpected source request"); },
|
||||
})("app/api/image-proxy/route.ts");
|
||||
const response = await route.GET(new NextRequest("https://app.example/api/image-proxy?original=1&download=resolve&url=" + encodeURIComponent(legacy)));
|
||||
expect(response.status).toBe(200);
|
||||
expect(await response.json()).toMatchObject({ delivery: "cdn", filename: "original.png" });
|
||||
});
|
||||
|
||||
test("unrecoverable preview and download requests return a cause and recommendation immediately", async () => {
|
||||
let fetched = 0;
|
||||
const route = serverLoader({
|
||||
"next/server": { NextResponse },
|
||||
"@/lib/storage/image-archive-db": { resolveLegacyAliyunStorageUrl: async () => { throw new Error("HTTP 410: " + RETIRED_STORAGE_MESSAGE); } },
|
||||
fetch: () => { fetched++; throw new Error("Unexpected source request"); },
|
||||
})("app/api/image-proxy/route.ts");
|
||||
for (const query of ["", "original=1&download=resolve&"]) {
|
||||
const response = await route.GET(new NextRequest("https://app.example/api/image-proxy?" + query + "url=" + encodeURIComponent(legacy)));
|
||||
const data = await response.json();
|
||||
expect(response.status).toBe(410);
|
||||
expect(data.failure).toMatchObject({ code: "STORAGE_RETIRED", retryable: false });
|
||||
expect(data.failure.recommendation).toContain("重新上传");
|
||||
expect(data.error).toContain(data.requestId);
|
||||
}
|
||||
expect(fetched).toBe(0);
|
||||
});
|
||||
|
||||
test("retired model inputs are reported as missing files rather than a model-service failure", async () => {
|
||||
const { getRequestFailureDetails } = await import("@/lib/request-error");
|
||||
expect(getRequestFailureDetails(new Error("HTTP 410: " + RETIRED_STORAGE_MESSAGE))).toMatchObject({ code: "SOURCE_STORAGE_RETIRED", outcome: "failed" });
|
||||
});
|
||||
@ -18,16 +18,18 @@ test('retired URLs include stored proxy URLs but do not include live storage', (
|
||||
|
||||
test('original image proxy returns permanent failure without redirecting or fetching', async () => {
|
||||
let fetches = 0;
|
||||
const route = serverLoader({ 'next/server': { NextResponse }, fetch: () => { fetches++; throw Error('Unexpected network'); } })('app/api/image-proxy/route.ts');
|
||||
const route = serverLoader({ 'next/server': { NextResponse }, fetch: () => { fetches++; throw new Error('Unexpected network'); } })('app/api/image-proxy/route.ts');
|
||||
const response = await route.GET(new NextRequest('https://ai.yydd.ai/api/image-proxy?original=1&url=' + encodeURIComponent(retired), {headers:{'sec-fetch-dest':'image'}}));
|
||||
expect(response.status).toBe(410);
|
||||
expect(await response.json()).toMatchObject({code:'storage_retired',error:RETIRED_STORAGE_MESSAGE});
|
||||
const data = await response.json();
|
||||
expect(data.failure).toMatchObject({code:'STORAGE_RETIRED',retryable:false});
|
||||
expect(data.error).toContain(data.requestId);
|
||||
expect(fetches).toBe(0);
|
||||
});
|
||||
|
||||
test('byte cache never fetches retired resources', async () => {
|
||||
let fetches = 0;
|
||||
const module = serverLoader({ fetch: () => { fetches++; throw Error('Unexpected network'); } })('lib/storage/image-byte-cache.ts');
|
||||
const module = serverLoader({ fetch: () => { fetches++; throw new Error('Unexpected network'); } })('lib/storage/image-byte-cache.ts');
|
||||
await expect(module.fetchImageBytes(retired)).rejects.toThrow('HTTP 410');
|
||||
expect(fetches).toBe(0);
|
||||
});
|
||||
@ -35,7 +37,7 @@ test('byte cache never fetches retired resources', async () => {
|
||||
test('retired sources cannot enter the archive queue', async () => {
|
||||
let inserts = 0;
|
||||
const module = serverLoader({
|
||||
'@/lib/db': {db:{insert:()=>{inserts++;throw Error('Unexpected insert');}}},
|
||||
'@/lib/db': {db:{insert:()=>{inserts++;throw new Error('Unexpected insert');}}},
|
||||
'@/lib/db/schema': {},
|
||||
})('lib/storage/image-archive-db.ts');
|
||||
expect(await module.enqueueImageArchiveTask({sourceUrl:retired})).toBeNull();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user