package com.bonus.file.mongodb.util;

import com.bonus.common.entity.file.FileExportVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Base64Utils;

import java.util.Objects;

/**
 * @author：cwchen
 * @date：2024-03-11-15:57
 * @version：1.0
 * @description：mongodb文件转base64工具类
 */
@Slf4j
public class MongodbFileBase64Util {

    /**
     * img图片转base64url
     *
     * @param vo
     * @return String
     * @description
     * @author cwchen
     * @date 2024/3/11 15:59
     */
    public String getImgBase64(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;
    }
}
