export function checkFileType(file: File, accepts: string[]) {
  const newTypes = accepts.join('|');
  // const reg = /\.(jpg|jpeg|png|gif|txt|doc|docx|xls|xlsx|xml)$/i;
  const reg = new RegExp('\\.(' + newTypes + ')$', 'i');

  return reg.test(file.name);
}

export function checkImgType(file: File) {
  return isImgTypeByName(file.name);
}

export function isImgTypeByName(name: string) {
  console.log('isImgTypeByName',name)
  if(!name){
    return false
  }
  // return /\.(jpg|jpeg|png|gif)$/i.test(name);
  // return /\.(jpg|jpeg|png|gif)$/i.test(name.split('?').shift());
  return /\.(jpg|jpeg|png|gif)$/i.test(name.split('?').shift());
}

// 校验内容是否包含base64格式的图片
export function validateBase64(str){
  let imgReg = RegExp(/data:image\/.*;base64,/)
  const res = imgReg.test(str)
  return res
}

export function getBase64WithFile(file: File) {
  return new Promise<{
    result: string;
    file: File;
  }>((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve({ result: reader.result as string, file });
    reader.onerror = (error) => reject(error);
  });
}
