package com.copy.trade.util;

import lombok.Data;
import org.apache.tika.Tika;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class FileUtils {
    public final static List<String> IMAGE_TYPE_LIST = new ArrayList<>();
    static {
        IMAGE_TYPE_LIST.add("png");
        IMAGE_TYPE_LIST.add("jpg");
        IMAGE_TYPE_LIST.add("jpeg");
        IMAGE_TYPE_LIST.add("svg");
        IMAGE_TYPE_LIST.add("bmp");
    }

    public static FileInfo getFileInfo(MultipartFile file) throws IOException{
        Tika tika = new Tika();
        String detect = tika.detect(file.getInputStream());
        String[] split = detect.split("/");

        FileInfo fileInfo = new FileUtils().new FileInfo();
        if(split.length>1){
            fileInfo.setFileType(split[1]);
        }else{
            fileInfo.setFileType(split[0]);
        }

        if(IMAGE_TYPE_LIST.contains(fileInfo.getFileType())){
            fileInfo.setIsPicture(true);
        }
        return fileInfo;
    }

    @Data
    public class FileInfo{
        private String fileType;
        private Boolean isPicture = false;
    }
}
