175 lines
6.6 KiB
TypeScript
175 lines
6.6 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
|
import { fetchImageBytes, onceImageBytes } from "@/lib/storage/image-byte-cache";
|
|
import { extractImageProxySourceUrl, inferOriginalImageUrl } from "@/lib/image-url";
|
|
import { getDownloadFailure } from "./download-error";
|
|
|
|
import {
|
|
convertImageToPhotoshopPng,
|
|
normalizeCanvasDownloadAsset as normalizeAsset,
|
|
type CanvasDownloadAssetResult,
|
|
type DownloadFileItem,
|
|
} from "./photoshop-download-normalize";
|
|
|
|
export { buildAttachmentContentDisposition, convertImageToPhotoshopPng, sanitizeCanvasDownloadFileName } from "./photoshop-download-normalize";
|
|
export type { CanvasDownloadAssetResult } from "./photoshop-download-normalize";
|
|
|
|
const DEFAULT_FETCH_TIMEOUT_MS = 30_000;
|
|
const DEFAULT_FETCH_RETRY_COUNT = 3;
|
|
|
|
function sleep(ms: number) {
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
}
|
|
|
|
function normalizeDownloadError(error: unknown): string {
|
|
if (error instanceof Error) return error.message || "download failed";
|
|
if (error && typeof error === "object" && "message" in error && typeof error.message === "string") return error.message;
|
|
if (typeof error === "string") return error;
|
|
return "download failed";
|
|
}
|
|
|
|
function buildRemoteFetchHeaders(fileUrl: string, requestOrigin: string, cookie: string): HeadersInit {
|
|
const headers: Record<string, string> = {
|
|
Accept: "image/avif,image/webp,image/apng,image/svg+xml,image/*,video/*,*/*;q=0.8",
|
|
"User-Agent":
|
|
"Mozilla/5.0 (compatible; BuyiCanvasDownloader/1.0; +https://buyi.ai)",
|
|
Referer: requestOrigin,
|
|
};
|
|
|
|
try {
|
|
const targetOrigin = new URL(fileUrl).origin;
|
|
if (targetOrigin === requestOrigin && cookie) {
|
|
headers.cookie = cookie;
|
|
}
|
|
} catch (_error) {
|
|
// URL has already been normalized before this point; keep this defensive.
|
|
}
|
|
|
|
return headers;
|
|
}
|
|
|
|
function resolveHttpUrl(value: string, requestOrigin: string): string {
|
|
const raw = String(value || "").trim();
|
|
if (!raw) return "";
|
|
|
|
try {
|
|
const parsed = new URL(raw, requestOrigin);
|
|
if (!/^https?:$/i.test(parsed.protocol)) return "";
|
|
return parsed.toString();
|
|
} catch {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
export function buildCanvasDownloadSourceCandidates(fileUrl: string, requestOrigin: string) {
|
|
const candidates: string[] = [];
|
|
const seen = new Set<string>();
|
|
|
|
const add = (value: string) => {
|
|
const resolved = resolveHttpUrl(value, requestOrigin);
|
|
if (!resolved || seen.has(resolved)) return;
|
|
seen.add(resolved);
|
|
candidates.push(resolved);
|
|
};
|
|
|
|
const resolvedFileUrl = resolveHttpUrl(fileUrl, requestOrigin);
|
|
const proxySourceUrl = extractImageProxySourceUrl(resolvedFileUrl || fileUrl);
|
|
if (proxySourceUrl) {
|
|
add(inferOriginalImageUrl(proxySourceUrl) || proxySourceUrl);
|
|
add(proxySourceUrl);
|
|
}
|
|
|
|
if (resolvedFileUrl) {
|
|
add(inferOriginalImageUrl(resolvedFileUrl) || resolvedFileUrl);
|
|
add(resolvedFileUrl);
|
|
}
|
|
|
|
return candidates;
|
|
}
|
|
|
|
export async function normalizeCanvasDownloadAsset(params: Parameters<typeof normalizeAsset>[0]) {
|
|
return normalizeAsset(params, async bytes => {
|
|
const digest = createHash("sha256").update(bytes).digest("hex");
|
|
const { buffer } = await onceImageBytes(`photoshop:${digest}`, async () => ({
|
|
buffer: await convertImageToPhotoshopPng(bytes), contentType: "image/png",
|
|
}));
|
|
return buffer;
|
|
});
|
|
}
|
|
|
|
export async function fetchCanvasDownloadAsset(params: {
|
|
file: DownloadFileItem;
|
|
index: number;
|
|
requestOrigin: string;
|
|
cookie: string;
|
|
includeSequencePrefix?: boolean;
|
|
timeoutMs?: number;
|
|
retryCount?: number;
|
|
signal?: AbortSignal;
|
|
}): Promise<CanvasDownloadAssetResult> {
|
|
if (isRetiredStorageUrl(params.file.url)) {
|
|
return { ok: false, url: params.file.url, name: params.file.name, error: RETIRED_STORAGE_MESSAGE };
|
|
}
|
|
let lastError = "";
|
|
const timeoutMs = params.timeoutMs || DEFAULT_FETCH_TIMEOUT_MS;
|
|
const retryCount = params.retryCount || DEFAULT_FETCH_RETRY_COUNT;
|
|
const originalCandidates = buildCanvasDownloadSourceCandidates(
|
|
params.file.url,
|
|
params.requestOrigin
|
|
);
|
|
|
|
for (let attempt = 1; attempt <= retryCount; attempt += 1) {
|
|
params.signal?.throwIfAborted();
|
|
const candidateUrls = [...originalCandidates];
|
|
if (process.env.POSTGRES_URL) {
|
|
// ZIPs and binary fallback also reuse the durable prepared object after the
|
|
// local byte cache expires, instead of converting the original JPEG again.
|
|
const { findImageArchiveDownloadTask } = await import("@/lib/storage/image-archive-db");
|
|
const task = await findImageArchiveDownloadTask(candidateUrls).catch(() => null);
|
|
const asset = (task?.metadata as { downloadAsset?: { status?: string; url?: string; contentType?: string } } | null)?.downloadAsset;
|
|
if (asset?.status === "ready" && asset.url && asset.contentType === "image/png") {
|
|
const readyUrl = resolveHttpUrl(asset.url, params.requestOrigin);
|
|
if (readyUrl && !candidateUrls.includes(readyUrl)) candidateUrls.unshift(readyUrl);
|
|
} else if (task?.targetUrl && !candidateUrls.includes(task.targetUrl)) {
|
|
candidateUrls.unshift(task.targetUrl);
|
|
}
|
|
}
|
|
|
|
for (const candidateUrl of candidateUrls) {
|
|
params.signal?.throwIfAborted();
|
|
try {
|
|
const data = await fetchImageBytes(candidateUrl, {
|
|
timeoutMs,
|
|
signal: params.signal,
|
|
headers: buildRemoteFetchHeaders(candidateUrl, params.requestOrigin, params.cookie),
|
|
// Authenticated local assets must never share a cache across users.
|
|
scope: new URL(candidateUrl).origin === params.requestOrigin && params.cookie
|
|
? createHash("sha256").update(params.cookie).digest("hex") : "",
|
|
});
|
|
return await normalizeCanvasDownloadAsset({
|
|
item: params.file,
|
|
index: params.index,
|
|
bytes: data.buffer,
|
|
contentType: data.contentType,
|
|
includeSequencePrefix: params.includeSequencePrefix !== false,
|
|
});
|
|
} catch (error) {
|
|
params.signal?.throwIfAborted();
|
|
const message = normalizeDownloadError(error);
|
|
if (!lastError || getDownloadFailure(message).retryable || !getDownloadFailure(lastError).retryable) lastError = message;
|
|
}
|
|
}
|
|
// Archiving may have completed during the failed fetch. Resolve aliases
|
|
// again before retrying, rather than spending every attempt on the old URL.
|
|
if (!getDownloadFailure(lastError).retryable) break;
|
|
if (attempt < retryCount) await sleep(350 * attempt);
|
|
}
|
|
|
|
return {
|
|
ok: false,
|
|
url: params.file.url,
|
|
name: params.file.name,
|
|
error: lastError || "图片下载失败",
|
|
};
|
|
}
|