52 lines
2.0 KiB
TypeScript
52 lines
2.0 KiB
TypeScript
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;
|
|
}
|
|
}
|