package com.ruoyi.common.utils;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.uuid.Seq;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Base64;

/**
 * ImageUtil
 *
 * @Auther: mtx
 * @Date: 2023/08/17 20:19
 */
public class ImageUtil {

    /**
     * 图片转Base64字符串
     *
     * @param imageFileName /profile/upload/2023/08/15/1 (2) - 副本_20230815143351A004.jpg
     * @return
     */
    public static String convertImageToBase64Str(String imageFileName) throws IOException {
        String url = RuoYiConfig.getProfile() + StringUtils.substringAfter(imageFileName, Constants.RESOURCE_PREFIX);
//        String url = "C:\\test\\gnhz\\shuangkong_linux\\" + StringUtils.substringAfter(imageFileName, Constants.RESOURCE_PREFIX);
//        File file = new File(url + imageFileName);
        File file = new File(url);
        InputStream fileImageInputStream = Files.newInputStream(file.toPath());
        byte[] bytes = IOUtils.toByteArray(fileImageInputStream);
        String encoded = Base64.getEncoder().encodeToString(bytes);
        String mimeType = Files.probeContentType(file.toPath());
        return "data:" + mimeType + ";" + "base64," + encoded;
    }

    /**
     * Base64字符串转图片
     *
     * @param base64String  base64的字符串
     * @param imageFileName 文件名称带后缀  例如：【aaa.jpg】
     */
    public static String convertBase64StrToImage(String base64String, String imageFileName) throws IOException {
        ByteArrayInputStream bais = null;
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            //获取图片类型
            String suffix = imageFileName.substring(imageFileName.lastIndexOf(".") + 1);
            String fileName = StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
                    FilenameUtils.getBaseName(imageFileName), Seq.getId(Seq.uploadSeqType), suffix);
            //获取JDK8里的解码器Base64.Decoder,将base64字符串转为字节数组
            byte[] bytes = Base64.getDecoder().decode(base64String);
            //构建字节数组输入流
            bais = new ByteArrayInputStream(bytes);
            //通过ImageIO把字节数组输入流转为BufferedImage
            BufferedImage bufferedImage = ImageIO.read(bais);
            //构建文件
            File imageFile = FileUploadUtils.getAbsoluteFile(filePath, fileName);
            //写入生成文件
            ImageIO.write(bufferedImage, suffix, imageFile);

            return FileUploadUtils.getPathFileName(filePath, fileName);
        } finally {
            try {
                if (bais != null) {
                    bais.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
