package com.yx.face.controller.FileUpload;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yx.face.boot.uitls.DateUtils;
import com.yx.face.boot.uitls.MiniOUtils;
import io.minio.MinioClient;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Decoder;


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @ProjectName: fyzd-phone
 * @Package: com.yx.face.controller.FileUpload
 * @ClassName: UploadController
 * @Author: 崔哥
 * @Description:
 * @Date: 2021/11/22 16:01
 * @Version: 1.0
 */
@Slf4j
@Api(tags = "B 文件上传")
@RestController
@RequestMapping("/fyzd")
public class UploadController {

    @Autowired
    private MiniOUtils miniOUtils;

    /**
     * 文件上传
     *
     * @param data
     * @return
     */
    @PostMapping("/file/upload")
    public String upload(@RequestBody String data) {
        log.info("日志" + data);
        try {
            log.info("日志" + data);
            JSONObject jsonObject = JSON.parseObject(data);
            String filename = jsonObject.getString("name");
            String base64 = jsonObject.getString("base64");
            filename = filename.replace("-", "");
            InputStream inputStream = null;
            try {
                byte[] bytes = new BASE64Decoder().decodeBuffer(base64);  //将字符串转换为byte数组，这里的content是那一串base64密文 /9j/4AAQ.................，不包含（data:img/jpg;base64,）注意标点符号
                inputStream = new ByteArrayInputStream(bytes);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("上传失败");
            }
            String dateTime = DateUtils.dateTime().concat("/");
            String objectName = "photo/" + dateTime + filename;
            String url = miniOUtils.fileUpload2MiniO(inputStream, objectName);
            return url;
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @PostMapping("/file/logImg")
    public String logImg(@RequestBody String data) {
        log.info("日志" + data);
        try {
            log.info("日志" + data);
            JSONObject jsonObject = JSON.parseObject(data);
            String filename = jsonObject.getString("name");
            String base64 = jsonObject.getString("base64");
            filename = filename.replace("-", "");
            InputStream inputStream = null;
            try {
                byte[] bytes = new BASE64Decoder().decodeBuffer(base64);  //将字符串转换为byte数组，这里的content是那一串base64密文 /9j/4AAQ.................，不包含（data:img/jpg;base64,）注意标点符号
                inputStream = new ByteArrayInputStream(bytes);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("上传失败");
            }
            String dateTime = DateUtils.dateTime().concat("/");
            String objectName = "log/" + dateTime + filename;
            String url = miniOUtils.fileUpload2MiniO(inputStream, objectName);
            return url;
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    /**
     * 下载oss对应文件
     *
     * @param fileName 储存的文件名
     * @return
     */
    @GetMapping("/file/downLoad")
    public String downloadFile(String fileName) {
        // 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        byte[] data = null;
        // 读取图片字节数组
        InputStream is = miniOUtils.downLoadFile("log" + fileName.split("log")[1]);
        try {
            data = IOUtils.toByteArray(is);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (data == null) {
            return "";
        }
        return new String(Base64.encodeBase64(data));
    }

    /**
     * 下载oss对应文件
     *
     * @param fileName 储存的文件名
     * @return
     */
    @GetMapping("/file/photo")
    public String downloadPhoto(String fileName) {
        // 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        byte[] data = null;
        // 读取图片字节数组
        InputStream is = miniOUtils.downLoadFile("photo" + fileName.split("photo")[1]);
        try {
            data = IOUtils.toByteArray(is);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (data == null) {
            return "";
        }
        return new String(Base64.encodeBase64(data));
    }
}
