import "server-only"; import { getDefaultStorageConfig } from "@/lib/db/queries"; import { ensureStoredImageDownloadTask, findImageArchiveDownloadTask } from "./image-archive-db"; import { extractImageProxySourceUrl, inferOriginalImageUrl } from "@/lib/image-url"; import { prepareOriginalImageArchive } from "./original-image-download"; export async function resolveCanvasCdnDownload(urls: string[], filename: string) { const sources = Array.from(new Set(urls.flatMap(url => { const source = extractImageProxySourceUrl(url) || url; return [source, inferOriginalImageUrl(source) || source]; }))); let task = await findImageArchiveDownloadTask(sources, true); const config = await getDefaultStorageConfig(); if (config?.type !== "qiniu-kodo" || !config.publicUrl || !config.isEnabled) return { task, direct: null, canPrepareDirect: false }; const base = new URL(config.publicUrl); const origin = process.env.CANVAS_QINIU_ORIGIN_URL; if (!task && base.protocol === "https:") { for (const candidate of sources) { const stored = new URL(candidate); if (origin && stored.origin === new URL(origin).origin && !stored.search) { stored.host = base.host; stored.protocol = base.protocol; } if (stored.origin === base.origin && !stored.search && !stored.hash && stored.pathname.startsWith(base.pathname.replace(/\/$/, "") + "/uploads/")) { task = await ensureStoredImageDownloadTask(stored.toString()); break; } } } if (!task) task = await prepareOriginalImageArchive(sources[0]); const asset = (task?.metadata as { downloadAsset?: { status?: string; url?: string; contentType?: string; bytes?: number } } | null)?.downloadAsset; const ready = asset?.status === "ready" && asset.url && asset.contentType === "image/png"; const source = ready ? asset.url : task?.targetUrl; if (!source) return { task, direct: null, canPrepareDirect: false }; const url = new URL(source); if (origin && url.origin === new URL(origin).origin) url.host = base.host; if (base.protocol !== "https:" || url.origin !== base.origin || !(url.pathname.startsWith(base.pathname.replace(/\/$/, "") + "/"))) return { task, direct: null, canPrepareDirect: false }; if (!ready) return { task, direct: null, canPrepareDirect: true }; // attname is interpreted by Kodo/CDN as Content-Disposition, including Chinese // filenames. Keep all processing/auth parameters intact for correct cache keys. url.searchParams.set("attname", filename); return { task, direct: { delivery: "cdn", downloadUrl: url.toString(), filename, bytes: asset?.bytes || 0 }, canPrepareDirect: true }; }