package org.jeecg.common.util;

import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.io.IOUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;


@Slf4j
public class FileUtils {
    // 通过Url将图片转为MultipartFile
    public static MultipartFile uploadImgUrlToMultipartFile(String url) {
        byte[] bytes = null;
        try {
            bytes = HttpUtil.downloadBytes(url);
        } catch (Exception e) {
            log.info("下载文件出错", e);
        }
        String name = "imageFile" + url.substring(url.lastIndexOf("."));
        MultipartFile multipartFile = getMultipartFile(name, bytes);
        return multipartFile;
    }

    /**
     * 将图片路径转化为 MultipartFile，与 uploadImgUrlToMultipartFile 方法的区别在于本方法创建对象的 contentType 为 image/*
     * uploadImgUrlToMultipartFile创建对象的 contentType 为 text/plain
     *
     * @param url 图片路径
     * @return 文件对象，如果图片路径无法访问则返回null
     */
    public static MultipartFile imgUrlToMultipartFile(String url) {
        byte[] bytes = null;
        try {
            bytes = HttpUtil.downloadBytes(url);
        } catch (Exception e) {
            return null;
        }
        if (bytes == null) {
            return null;
        }
        String name = "imageFile" + url.substring(url.lastIndexOf("."));
        String contentType = "image/" + url.substring(url.lastIndexOf(".") + 1);

        MultipartFile multipartFile;
        try {
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            FileItemFactory factory = new DiskFileItemFactory(16, null);
            FileItem fileItem = factory.createItem("mainFile", contentType, false, name);
            IOUtils.copy(new ByteArrayInputStream(bytes), fileItem.getOutputStream());
            multipartFile = new CommonMultipartFile(fileItem);
            in.close();
        } catch (Exception e) {
            log.info("图片路径转为文件出错，图片路径：" + url);
            e.printStackTrace();
            return null;
        }
        return multipartFile;
    }

    // // 获取图片二进制
    // private static byte[] downloadPicture(String url) {
    //     URL urlConnection = null;
    //     HttpURLConnection httpURLConnection = null;
    //     try {
    //         urlConnection = new URL(url);
    //         httpURLConnection = (HttpURLConnection) urlConnection.openConnection();
    //         httpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.76");
    //         InputStream in = httpURLConnection.getInputStream();
    //         byte[] buffer = new byte[1024];
    //         int len = 0;
    //         ByteArrayOutputStream out = new ByteArrayOutputStream();
    //         while ((len = in.read(buffer)) != -1) {
    //             out.write(buffer, 0, len);
    //         }
    //         in.close();
    //         out.close();
    //         return out.toByteArray();
    //     } catch (Exception e) {
    //         e.printStackTrace();
    //     } finally {
    //         httpURLConnection.disconnect();
    //     }
    //     return null;
    // }

    // 二进制文件转换MultipartFile
    public static MultipartFile getMultipartFile(String name, byte[] bytes) {
        MultipartFile mfile = null;
        ByteArrayInputStream in = null;
        try {
            in = new ByteArrayInputStream(bytes);
            FileItemFactory factory = new DiskFileItemFactory(16, null);
            FileItem fileItem = factory.createItem("mainFile", "text/plain", false, name);
            IOUtils.copy(new ByteArrayInputStream(bytes), fileItem.getOutputStream());
            mfile = new CommonMultipartFile(fileItem);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return mfile;
    }
}
