package com.securitycontrol.files.mongodb.util;

import com.securitycontrol.entity.file.FileExportVo;
import com.securitycontrol.files.mongodb.service.FileUploadService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Base64Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author：cwchen
 * @date：2024-03-11-15:55
 * @version：1.0
 * @description：mongodb文件上传-工具类
 */
@Slf4j
public class MongodbFileUtil {

    @Resource(name = "fileMongoServiceImpl")
    private FileUploadService mongoService;

    @Autowired
    private Base64Util base64Util;

    /**
     * 获取文件的base64地址
     *
     * @param vo
     * @return String
     * @description
     * @author cwchen
     * @date 2024/3/12 11:00
     */
    public static String getBase64(FileExportVo vo) {
        try {
            byte[] bytes = vo.getData();
            if (!Objects.isNull(bytes)) {
                String suffix = vo.getSuffix().replace(".", "");
                return "data:image/" + suffix + ";base64," + Base64Utils.encodeToString(bytes);
            }
        } catch (Exception e) {
            log.error("byte流转base64", e);
        }
        return null;
    }

    /**
     * 获取文件的base64地址
     *
     * @param vo
     * @return String
     * @description
     * @author cwchen
     * @date 2024/3/12 11:00
     */
    public static String getBase64File(FileExportVo vo) {
        try {
            byte[] bytes = vo.getData();
            if (!Objects.isNull(bytes)) {
                String suffix = vo.getSuffix().replace(".", "");
                return  Base64Utils.encodeToString(bytes);
            }
        } catch (Exception e) {
            log.error("byte流转base64", e);
        }
        return null;
    }

}
