package com.hxhq.common.core.utils.file;

import java.io.File;
import java.util.Objects;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件类型工具类
 *
 * @author hxhq
 */
public class FileTypeUtils
{
    /**
     * 获取文件类型
     * <p>
     * 例如: hxhq.txt, 返回: txt
     * 
     * @param file 文件名
     * @return 后缀（不含".")
     */
    public static String getFileType(File file)
    {
        if (null == file)
        {
            return StringUtils.EMPTY;
        }
        return getFileType(file.getName());
    }

    /**
     * 获取文件类型
     * <p>
     * 例如: hxhq.txt, 返回: txt
     *
     * @param fileName 文件名
     * @return 后缀（不含".")
     */
    public static String getFileType(String fileName)
    {
        int separatorIndex = fileName.lastIndexOf(".");
        if (separatorIndex < 0)
        {
            return "";
        }
        return fileName.substring(separatorIndex + 1).toLowerCase();
    }

    /**
     * 获取文件名的后缀
     * 
     * @param file 表单文件
     * @return 后缀名
     */
    public static final String getExtension(MultipartFile file)
    {
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        if (StringUtils.isEmpty(extension))
        {
            extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
        }
        return extension;
    }

    /**
     * 获取文件类型
     * 
     * @param photoByte 文件字节码
     * @return 后缀（不含".")
     */
    public static String getFileExtendName(byte[] photoByte)
    {
        String strFileExtendName = "JPG";
        int a1 = 0;
        int a2 = 71;
        int a3 = 1;
        int a4 = 73;
        int a5 = 2;
        int a6 = 70;
        int a7 = 3;
        int a8 = 56;
        int a9 = 4;
        int a10 = 55;
        int a11 = 57;
        int a12 = 5;
        int a13 = 97;


        int a14 = 6;
        int a15 = 74;
        int a16 = 7;
        int a17 = 70;
        int a18 = 8;
        int a19 = 73;
        int a20 = 9;
        int a21 = 70;

        int a22 = 66;
        int a23 = 1;
        int a24 = 77;
        int a25 = 1;
        int a26 = 80;
        int a27 = 2;
        int a28 = 78;
        int a29 = 3;
        int a30 = 71;

        Boolean a = (photoByte[a1] == a2) && (photoByte[a3] == a4) && (photoByte[a5] == a6) && (photoByte[a7] == a8)
                && ((photoByte[a9] == a10) || (photoByte[a9] == a11)) && (photoByte[a12] == a13);
        Boolean b = (photoByte[a14] == a15) && (photoByte[a16] == a17) && (photoByte[a18] == a19) && (photoByte[a20] == a21);
        if (a)
        {
            strFileExtendName = "GIF";
        }
        else if (b)
        {
            strFileExtendName = "JPG";
        }
        else if ((photoByte[a1] == a22) && (photoByte[a23] == a24))
        {
            strFileExtendName = "BMP";
        }
        else if ((photoByte[a25] == a26) && (photoByte[a27] == a28) && (photoByte[a29] == a30))
        {
            strFileExtendName = "PNG";
        }
        return strFileExtendName;
    }
}