38 lines
1.3 KiB
TypeScript
38 lines
1.3 KiB
TypeScript
|
|
export const ECOMMERCE_SUPPORTED_IMAGE_TYPES = new Set(["image/jpeg", "image/png", "image/webp", "image/gif"]);
|
||
|
|
export const ECOMMERCE_MAX_IMAGE_BYTES = 10 * 1024 * 1024;
|
||
|
|
|
||
|
|
export interface EcommerceImageValidationResult {
|
||
|
|
accepted: File[];
|
||
|
|
rejected: Array<{ name: string; reason: string }>;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function validateEcommerceImageFiles(files: File[]): EcommerceImageValidationResult {
|
||
|
|
const accepted: File[] = [];
|
||
|
|
const rejected: EcommerceImageValidationResult["rejected"] = [];
|
||
|
|
|
||
|
|
files.forEach((file) => {
|
||
|
|
if (!ECOMMERCE_SUPPORTED_IMAGE_TYPES.has(file.type)) {
|
||
|
|
rejected.push({ name: file.name, reason: "不支持的图片格式" });
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (file.size > ECOMMERCE_MAX_IMAGE_BYTES) {
|
||
|
|
rejected.push({ name: file.name, reason: "图片超过 10MB" });
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
accepted.push(file);
|
||
|
|
});
|
||
|
|
|
||
|
|
return { accepted, rejected };
|
||
|
|
}
|
||
|
|
|
||
|
|
export function summarizeRejectedImages(rejected: EcommerceImageValidationResult["rejected"]): string {
|
||
|
|
if (!rejected.length) return "";
|
||
|
|
const first = rejected[0];
|
||
|
|
const suffix = rejected.length > 1 ? ` 等 ${rejected.length} 个文件` : "";
|
||
|
|
return `${first.name}${suffix} 已跳过:${first.reason}`;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function normalizeEcommerceImageMime(type: string): string {
|
||
|
|
return ECOMMERCE_SUPPORTED_IMAGE_TYPES.has(type) ? type : "image/png";
|
||
|
|
}
|