import { RcFile } from "antd/es/upload";
import { DageUploadType } from "./types";

/**
 * 可选文件类型
 */
export const ACCEPT: Record<string, string> = {
  [DageUploadType.IMG]: ".jpg,.jpeg,.png,.gif",
  [DageUploadType.MOBILE_MODEL]: ".zip",
  [DageUploadType.MODEL]: ".4dage",
  [DageUploadType.VIDEO]: ".mp4",
  [DageUploadType.AUDIO]: ".mp3",
};

/**
 * 校验文件类型
 */
export const validateFileType = (file: File, accept: string) => {
  const fileName = file.name.toLowerCase();
  const fileExtension = fileName.substring(fileName.lastIndexOf("."));

  return accept.split(",").includes(fileExtension);
};

export async function getImageSize(
  file: RcFile
): Promise<{ width: number; height: number }> {
  return new Promise((resolve, reject) => {
    const img = new Image();

    img.onload = () => {
      resolve({ width: img.width, height: img.height });
    };

    img.onerror = (err) => {
      reject(err);
    };

    img.src = URL.createObjectURL(file);
  });
}
