package com.bonus.common.core.utils;


import org.springframework.web.multipart.MultipartFile;
import sun.net.www.content.image.jpeg;

import java.util.Arrays;
import java.util.Locale;

/**
 * 文件上传校验的公共方法
 *
 * @author：cwchen
 * @date：2024-03-11-10:11
 * @version：1.0
 * @description：文件上传校验的公共方法
 */
public class UploadCheckUtils {

    /**
     * 文件大小 10MB(可用于图片和视频区分)
     */
    public static final long IMG_FILE_SIZE = 10 * 1024 * 1024;
    /**
     * EXCEL文件大小 100MB
     */
    public static final long EXCEL_FILE_SIZE = 100 * 1024 * 1024;

    /**
     * 只支持图片格式
     */
    public static final String[] YES_IMAGE_SUPPORT = {".jpg", ".jpeg", ".png"};

    public static final String[] YES_EXCEL_SUPPORT = {".xlsx", ".xls"};

    /**
     * 只支持视频格式
     */
    public static final String[] YES_VIDEO_SUPPORT = {".mp4", ".avi", ".mp3"};

    /**
     * 只支持音频格式
     */
    public static final String[] YES_AUDIO_SUPPORT = {".mp3"};

    /**
     * 只支持文件格式
     */
    public static final String[] YES_FILE_SUPPORT = {".xlsx", ".xls", ".doc", ".docx", ".txt", ".csv"};


    /**
     * 全部文件(普通文件,图片, 视频,音频)后缀 支持的类型
     */
    public static final String[] FILE_SUFFIX_SUPPORT = {".xlsx", ".xls", ".doc", ".docx", ".txt", ".csv",
            ".jpg", ".jpeg", ".png", ".mp4", ".avi", ".mp3"};

    /**
     * 文件名字 需要排除的字符
     * 废弃:  "(", ")","",".", "——", "_","-"
     */
    public static final String[] FILE_NAME_EXCLUDE = {
            "`", "!", "@", "#", "$", "%", "^", "&", "*", "=", "+",
            "~", "·", "！", "￥", "……", "（", "）",
            "?", ",", "<", ">", ":", ";", "[", "]", "{", "}", "/", "\\", "|",
            "？", "，", "。", "《", "》", "：", "；", "【", "】", "、"
    };

    public static final String SUFFIX = ".";

    /**
     * 图片上传文件校验大小、名字、后缀
     *
     * @param multipartFile multipartFile
     */
    public static String uploadImgVerify(MultipartFile multipartFile) {
        // 校验文件是否为空
        if (multipartFile == null) {
            return "上传图片不能为空";
        }
        // 校验文件名字
        String originalFilename = multipartFile.getOriginalFilename();
        if (originalFilename == null) {
            return "上传图片名字不能为空";
        }
        for (String realKey : FILE_NAME_EXCLUDE) {
            if (originalFilename.contains(realKey)) {
                return "上传图片名称不允许出现"+realKey+"关键字";
            }
        }
        // 校验文件后缀
        if (!originalFilename.contains(SUFFIX)) {
            return "图片不能没有后缀";
        }
        String suffix = originalFilename.substring(originalFilename.lastIndexOf('.'));
        /*校验: 文件格式是否符合要求*/
        if (!Arrays.asList(YES_IMAGE_SUPPORT).contains(suffix.toLowerCase(Locale.ROOT))) {
            return "图片格式仅支持" + Arrays.asList(YES_IMAGE_SUPPORT).toString();
        }
        Long fileSize = multipartFile.getSize();
        if (fileSize > IMG_FILE_SIZE) {
            return "图片大小仅支持10MB以内";
        }
        return null;
    }

    public static String uploadExcelVerify(MultipartFile multipartFile) {
        // 校验文件是否为空
        if (multipartFile == null) {
            return "上传文件不能为空";
        }
        // 校验文件名字
        String originalFilename = multipartFile.getOriginalFilename();
        if (originalFilename == null) {
            return "上传文件名字不能为空";
        }
        for (String realKey : FILE_NAME_EXCLUDE) {
            if (originalFilename.contains(realKey)) {
                return "上传文件名称不允许出现"+realKey+"关键字";
            }
        }
        // 校验文件后缀
        if (!originalFilename.contains(SUFFIX)) {
            return "上传文件不能没有后缀";
        }
        String suffix = originalFilename.substring(originalFilename.lastIndexOf('.'));
        /*校验: 文件格式是否符合要求*/
        if (!Arrays.asList(YES_EXCEL_SUPPORT).contains(suffix.toLowerCase(Locale.ROOT))) {
            return "上传文件格式仅支持" + Arrays.asList(YES_EXCEL_SUPPORT).toString();
        }
        Long fileSize = multipartFile.getSize();
        if (fileSize > EXCEL_FILE_SIZE) {
            return "上传文件大小仅支持100MB以内";
        }
        return null;
    }

}
