package com.bonus.sgzb.common.core.utils.file;

import java.io.File;
import java.util.Objects;

import com.bonus.sgzb.common.core.utils.GlobalConstants;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

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

    /**
     * 获取文件类型
     * <p>
     * 例如: ruoyi.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";
        if ((photoByte[0] == GlobalConstants.INT_71) && (photoByte[1] == GlobalConstants.INT_73)) {
            if ((photoByte[GlobalConstants.INT_2] == GlobalConstants.INT_70) && (photoByte[GlobalConstants.INT_3] == GlobalConstants.INT_56)) {
                if (((photoByte[GlobalConstants.INT_4] == GlobalConstants.INT_55) || (photoByte[GlobalConstants.INT_4] == GlobalConstants.INT_57))) {
                    if ((photoByte[GlobalConstants.INT_5] == GlobalConstants.INT_97)) {
                        strFileExtendName = "GIF";
                    }
                }
            }
        } else if ((photoByte[GlobalConstants.INT_6] == GlobalConstants.INT_74) && (photoByte[GlobalConstants.INT_7] == GlobalConstants.INT_70) && (photoByte[GlobalConstants.INT_8] == GlobalConstants.INT_73) && (photoByte[GlobalConstants.INT_9] == GlobalConstants.INT_70)) {
            strFileExtendName = "JPG";
        } else if ((photoByte[0] == GlobalConstants.INT_66) && (photoByte[1] == GlobalConstants.INT_77)) {
            strFileExtendName = "BMP";
        } else if ((photoByte[1] == GlobalConstants.INT_80) && (photoByte[GlobalConstants.INT_2] == GlobalConstants.INT_78) && (photoByte[GlobalConstants.INT_3] == GlobalConstants.INT_71)) {
            strFileExtendName = "PNG";
        }
        return strFileExtendName;
    }
}