package org.dromara.common.core.utils;

import net.coobird.thumbnailator.Thumbnails;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 获取图片尺寸和截图 工具类
 *
 * @author zhangxiaoyan
 */
public class ImageSizeUtil {
    /**
     * 压缩图片 并返回MultipartFile
     * @param multiFile
     * @return
     * @throws Exception
     */
    public static MultipartFile compressImg(MultipartFile multiFile) {
        long size = multiFile.getSize();
        if (size> 0){
            double fileSizeMb = size / (1024.0 * 1024.0);
            if (fileSizeMb < 5){
                return multiFile;
            }
        }
        // 压缩图片
        InputStream inputStream = null;
        ByteArrayOutputStream bos = null;
        MultipartFile multipartFile = null;
        InputStream fileInput= null;
        try {
            inputStream = multiFile.getInputStream();
            bos = new ByteArrayOutputStream();
            // 压缩图片核心代码 scale设置压缩比例 越小压缩图片越小
            Thumbnails.of(inputStream).scale(0.5f).outputQuality(1f).toOutputStream(bos);
            fileInput = new ByteArrayInputStream(bos.toByteArray());
            // 转换 MultipartFile
            String fieldName = multiFile.getName();
            String fileName = multiFile.getOriginalFilename();
            String contentType = multiFile.getContentType();
            /**
             * filename:文件名
             * file.getName()：原文件名称
             * file.getContentType()：原文件ContentType
             * fileStream：文件输入字节流
             */
            multipartFile = new MockMultipartFile(fieldName, fileName, contentType, fileInput);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null){
                    inputStream.close();
                }
                if (bos != null){
                    bos.close();
                }
                if (fileInput != null){
                    fileInput.close();
                }
            } catch (IOException e) {
                e.printStackTrace();

            }
        }
        return multipartFile;
    }

/*    public static MultipartFile getMulFileByFile(InputStream fis,String fieldName,String contentType,String fileName) {
        FileItem fileItem = createFileItem(fis,fieldName,contentType,fileName);
        MultipartFile mfile = new CommonsMultipartFile(fileItem);
        return mfile;
    }

    public static FileItem createFileItem(InputStream fis,String fieldName,String contentType,String fileName){
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem item = factory.createItem(fieldName, contentType, false,fileName);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        try
        {
            OutputStream os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, 8192))!= -1)
            {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return item;
    }*/
}


