18 lines
691 B
TypeScript
18 lines
691 B
TypeScript
const allowedImageTypes: Readonly<Record<string, ReadonlySet<string>>> = {
|
|
'image/jpeg': new Set(['jpg', 'jpeg']),
|
|
'image/png': new Set(['png']),
|
|
}
|
|
|
|
export const PRODUCT_IMAGE_MAX_SIZE = 5 * 1024 * 1024
|
|
|
|
export function validateProductImageFile(file: Pick<File, 'name' | 'size' | 'type'>) {
|
|
const extension = file.name.split('.').pop()?.toLowerCase() ?? ''
|
|
const allowedExtensions = allowedImageTypes[file.type.toLowerCase()]
|
|
if (!allowedExtensions?.has(extension)) {
|
|
return '产品图片仅支持 JPG、JPEG、PNG 格式'
|
|
}
|
|
if (file.size <= 0) return '产品图片内容为空'
|
|
if (file.size > PRODUCT_IMAGE_MAX_SIZE) return '产品图片不能超过 5MB'
|
|
return ''
|
|
}
|