package com.ruoyi.common.utils;

import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;

/**
 * 获取图片尺寸和截图 工具类
 *
 * @author zhangxiaoyan
 */
public class ImageSizeUtil {
    /**
     * 压缩图片 并返回MultipartFile
     * @param multiFile
     * @return
     * @throws Exception
     */
    public static MultipartFile compressImg(MultipartFile multiFile) {
        // 压缩图片
        InputStream inputStream = null;
        ByteArrayOutputStream bos = null;
        MultipartFile multipartFile = null;
        InputStream fileInput= null;
        try {
            inputStream = multiFile.getInputStream();
            bos = new ByteArrayOutputStream();
            // 压缩图片核心代码
            Thumbnails.of(inputStream).scale(0.3f).outputQuality(1f).toOutputStream(bos);
            fileInput = new ByteArrayInputStream(bos.toByteArray());
            // 转换 MultipartFile
            String fieldName = multiFile.getName();
            String fileName = multiFile.getOriginalFilename();
            String contentType = multiFile.getContentType();
            multipartFile = getMulFileByFile(fileInput, fieldName, contentType, fileName);
        } 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;
    }
}


