ai-chatbot/lib/video-canvas/reference-input.ts

26 lines
1.4 KiB
TypeScript

export type ReferenceAccess = "reachable" | "unreachable" | "unknown";
export async function validateVideoReferences(
input: unknown,
probe: (url: string) => Promise<ReferenceAccess>,
): Promise<{ images: string[]; error?: string; status?: number }> {
if (input === undefined) return { images: [] };
if (!Array.isArray(input) || input.length > 8) return { images: [], error: "请提供不超过 8 张参考图", status: 400 };
const images: string[] = [];
for (const value of input) {
try {
if (typeof value !== "string") throw new Error();
const parsed = new URL(value.trim());
if (!["http:", "https:"].includes(parsed.protocol) || parsed.username || parsed.password) throw new Error();
images.push(value.trim());
} catch {
return { images: [], error: "参考图地址无效,请重新上传", status: 400 };
}
}
const checks = await Promise.all([...new Set(images)].map(url => probe(url).catch(() => "unknown" as const)));
if (checks.includes("unreachable")) return { images: [], error: "参考图片链接已失效或无访问权限,请重新上传", status: 400 };
if (checks.includes("unknown")) return { images: [], error: "参考图片连接暂时超时或服务繁忙,请重试;尚未提交生成任务", status: 503 };
// First and last frames are positional; identical URLs must remain two entries.
return { images };
}