UploadUtil.java 3.09 KB
package com.ruoyi.utils;

import cn.hutool.core.codec.Base64;
import com.ruoyi.common.core.domain.ResponseResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MinioConfig;
import com.ruoyi.config.BsthSystemConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.util.Objects;

/**
 * @author liujun
 * @date 2024年07月25日 14:13
 */
@Slf4j
@Component
public class UploadUtil {

    @Autowired
    private BsthSystemConfig bsthSystemConfig;
    @Autowired
    private MinioConfig minioConfig;

    /**
     * 上传图片接口
     *
     * @param content 图片内容
     * @param path    图片路径(相对路径)
     * @param type    图片类型
     * @return com.ruoyi.common.core.domain.ResponseResult<java.lang.Boolean>
     * @author liujun
     * @date 2024/7/25 14:31
     */
    public ResponseResult<Boolean> uploadImageOfBase64(String content, String path, String type) {
        if (StringUtils.isEmpty(content)) {
            return ResponseResult.error("上传的图片内容为空");
        }

        ByteArrayInputStream inputStream = null;
        try {
            content = FileUploadUtils.choosePrictureContent(content);
            byte[] bytes = Base64.decode(content);
            inputStream = new ByteArrayInputStream(bytes);

            minioConfig.upload(inputStream, path, "application/" + type);

            return ResponseResult.success(Boolean.TRUE);
        } catch (Exception e) {
            log.error("上传图片异常:[{}]", path, e);
            return ResponseResult.error("上传图片异常");
        } finally {
            if (Objects.nonNull(inputStream)) {
                IOUtils.closeQuietly(inputStream);
            }
        }
    }

    /**
     * 上传视频文件
     *
     * @param content
     * @param path
     * @return com.ruoyi.common.core.domain.ResponseResult<java.lang.Boolean>
     * @author liujun
     * @date 2024/7/25 14:37
     */
    public ResponseResult<Boolean> uploadVideoOfBase64(String content, String path) {
        if (StringUtils.isEmpty(content)) {
            return ResponseResult.error("上传的视频内容为空");
        }

        FileOutputStream outputStream = null;
        ByteArrayInputStream inputStream = null;
        try {
            content = FileUploadUtils.choosePrictureContent(content);
            byte[] bytes = Base64.decode(content);
            inputStream = new ByteArrayInputStream(bytes);

            minioConfig.upload(inputStream, path, "application/mp4");

            return ResponseResult.success(Boolean.TRUE);
        } catch (Exception e) {
            log.error("上传视频异常:[{}]", path, e);
            return ResponseResult.error("上传视频异常");
        } finally {
            if (Objects.nonNull(outputStream)) {
                IOUtils.closeQuietly(outputStream);
            }
        }
    }
}