package com.docus.server.common.download;

import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@Slf4j
public class Base64Utils {
    private static Base64Utils utils =null;
    private Base64Utils(){

    }
    public static Base64Utils getInstance(){
        if(utils==null){
            synchronized (Base64Utils.class){
                if(utils == null){
                    utils = new Base64Utils();
                }
            }
        }
        return utils;
    }

    public String imageToBase64Str(String imgFile) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }


    public byte[] base64StrToBytes(String imgStr) {
        if (imgStr == null) {
            return null;
        }
        String[] s = imgStr.split(",");
        if(s.length>0){
            imgStr=s[1];
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // 解密
            byte[] b = decoder.decodeBuffer(imgStr);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            return b;
        } catch (Exception e) {
            log.error("Base64 转 byte[] 出错啦！",e);
            return null;
        }
    }


    public boolean base64StrToImage(byte[] bytes, String path) {
        if (bytes == null) {
            return false;
        }
        try {
            //文件夹不存在则自动创建
            File tempFile = new File(path);
            if (!tempFile.getParentFile().exists()) {
                tempFile.getParentFile().mkdirs();
            }
            OutputStream out = new FileOutputStream(tempFile);
            out.write(bytes);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            log.error("图片文件下载失败："+e.getMessage());
            return false;
        }
    }

    public boolean base64StrToImage(String imgStr, String path) {
        if (imgStr == null) {
            return false;
        }
        String[] s = imgStr.split(",");
        if(s.length>0){
            imgStr=s[1];
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // 解密
            byte[] b = decoder.decodeBuffer(imgStr);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            //文件夹不存在则自动创建
            File tempFile = new File(path);
            if (!tempFile.getParentFile().exists()) {
                tempFile.getParentFile().mkdirs();
            }
            OutputStream out = new FileOutputStream(tempFile);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            log.error("图片文件下载失败："+e.getMessage());
            return false;
        }
    }

    public int getFileSize(File inFile){
        InputStream in  =null;
        try{
            in= new FileInputStream(inFile);
            int len =in.available();
            return len;
        }
        catch (Exception e){

        }
        finally {
            try{
                in.close();
            }
            catch (IOException e){
                e.printStackTrace();
            }
        }
        return -1;
    }


    public static void main(String[] args) {
        System.out.println(Base64Utils.getInstance().imageToBase64Str("e:\\1.jpg"));
    }
}
