ai-chatbot/lib/agents/psd-template-format.ts
2026-06-04 13:26:14 +08:00

75 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import "server-only";
export type AgentPsdTemplateFileFormat = "psd" | "psb" | "unknown";
export interface AgentPsdTemplateFileInspection {
signature: string;
version: number;
format: AgentPsdTemplateFileFormat;
channels: number;
height: number;
width: number;
depth: number;
colorMode: number;
}
export const AGENT_PSD_TEMPLATE_UNSUPPORTED_PSB_MESSAGE =
"当前生产模板解析仅支持 Photoshop PSD(.psd)。检测到 PSB / Large Document 格式,请在 Photoshop 中另存为 PSD 后再上传解析。";
export function inspectAgentPsdTemplateFile(
value: ArrayBuffer | Buffer
): AgentPsdTemplateFileInspection {
const buffer = Buffer.isBuffer(value)
? value
: Buffer.from(value);
if (buffer.byteLength < 26) {
return {
signature: "",
version: 0,
format: "unknown",
channels: 0,
height: 0,
width: 0,
depth: 0,
colorMode: 0,
};
}
const signature = buffer.subarray(0, 4).toString("ascii");
const version = buffer.readUInt16BE(4);
const format =
signature === "8BPS" && version === 1
? "psd"
: signature === "8BPS" && version === 2
? "psb"
: "unknown";
return {
signature,
version,
format,
channels: buffer.readUInt16BE(12),
height: buffer.readUInt32BE(14),
width: buffer.readUInt32BE(18),
depth: buffer.readUInt16BE(22),
colorMode: buffer.readUInt16BE(24),
};
}
export function assertSupportedAgentPsdTemplateFile(
value: ArrayBuffer | Buffer
) {
const inspection = inspectAgentPsdTemplateFile(value);
if (inspection.format === "psd") {
return inspection;
}
if (inspection.format === "psb") {
throw new Error(AGENT_PSD_TEMPLATE_UNSUPPORTED_PSB_MESSAGE);
}
throw new Error("模板文件格式不正确,请上传 Photoshop PSD(.psd)文件。");
}