zhongrj
2024-02-20 9575aee99a9beccba3a66273b9ee45ad706d3f42
新增文件上传接口(可以根据前缀进行上传)
2 files modified
1 files added
109 ■■■■■ changed files
src/main/java/org/springblade/modules/resource/builder/oss/OssBuilder.java 37 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/resource/endpoint/OssEndpoint.java 37 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/resource/rule/MyOssRule.java 35 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/resource/builder/oss/OssBuilder.java
@@ -32,7 +32,9 @@
import org.springblade.core.tool.utils.StringPool;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.core.tool.utils.WebUtil;
import org.springblade.modules.resource.endpoint.OssEndpoint;
import org.springblade.modules.resource.entity.Oss;
import org.springblade.modules.resource.rule.MyOssRule;
import org.springblade.modules.resource.service.IOssService;
import java.util.Map;
@@ -169,4 +171,39 @@
        }
    }
    /**
     * 获取template
     * @param prefixPath 上传文件前缀路径
     * @return OssTemplate
     */
    public OssTemplate templateByPrefixPath(String prefixPath) {
        return templateByPath(StringPool.EMPTY,prefixPath);
    }
    /**
     * 自定义路径前缀获取template
     *
     * @param code 资源编号
     * @param prefixPath 上传文件前缀路径
     * @return OssTemplate
     */
    public OssTemplate templateByPath(String code,String prefixPath) {
        String tenantId = AuthUtil.getTenantId();
        Oss oss = getOss(tenantId, code);
        OssTemplate template = templatePool.get(tenantId);
        template = templatePool.get(tenantId);
        OssRule ossRule = new MyOssRule(Boolean.FALSE,prefixPath);
        if (oss.getCategory() == OssEnum.MINIO.getCategory()) {
            template = MinioOssBuilder.template(oss, ossRule);
        } else if (oss.getCategory() == OssEnum.QINIU.getCategory()) {
            template = QiniuOssBuilder.template(oss, ossRule);
        } else if (oss.getCategory() == OssEnum.ALI.getCategory()) {
            template = AliOssBuilder.template(oss, ossRule);
        } else if (oss.getCategory() == OssEnum.TENCENT.getCategory()) {
            template = TencentOssBuilder.template(oss, ossRule);
        }
        templatePool.put(tenantId, template);
        ossPool.put(tenantId, oss);
        return template;
    }
}
src/main/java/org/springblade/modules/resource/endpoint/OssEndpoint.java
@@ -206,7 +206,7 @@
     */
    @SneakyThrows
    @PostMapping("/put-file-attach-by-name")
    public R<BladeFile> putFileAttach(@RequestParam String fileName, @RequestParam MultipartFile file) {
    public R<BladeFile> putFileAttach(@RequestParam(required = false) String fileName, @RequestParam MultipartFile file) {
        BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
        Long attachId = buildAttach(fileName, file.getSize(), bladeFile);
        bladeFile.setAttachId(attachId);
@@ -214,6 +214,41 @@
    }
    /**
     * 自定义前缀上传文件
     *
     * @param file 文件
     * @param prefixPath 文件
     * @return ObjectStat
     */
    @SneakyThrows
    @PostMapping("/put-file-by-prefix-path")
    public R<BladeFile> putFileByPrefixPath(@RequestParam MultipartFile file,@RequestParam(required = false) String prefixPath) {
        BladeFile bladeFile = ossBuilder.templateByPrefixPath(prefixPath).putFile(file.getOriginalFilename(), file.getInputStream());
        // 修改link
        changeLink(bladeFile);
        return R.data(bladeFile);
    }
    /**
     * 自定义前缀上传文件并保存至附件表
     *
     * @param file 文件
     * @param prefixPath 文件
     * @return ObjectStat
     */
    @SneakyThrows
    @PostMapping("/put-file-attach-by-prefix-path")
    public R<BladeFile> putFileAttachByPrefixPath(@RequestParam MultipartFile file,@RequestParam(required = false) String prefixPath) {
        String fileName = file.getOriginalFilename();
        BladeFile bladeFile = ossBuilder.templateByPrefixPath(prefixPath).putFile(file.getOriginalFilename(), file.getInputStream());
        Long attachId = buildAttach(fileName, file.getSize(), bladeFile);
        bladeFile.setAttachId(attachId);
        // 修改link
        changeLink(bladeFile);
        return R.data(bladeFile);
    }
    /**
     * 构建附件表
     *
     * @param fileName  文件名
src/main/java/org/springblade/modules/resource/rule/MyOssRule.java
New file
@@ -0,0 +1,35 @@
package org.springblade.modules.resource.rule;
import org.apache.logging.log4j.util.Strings;
import org.springblade.core.oss.rule.BladeOssRule;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.FileUtil;
import org.springblade.core.tool.utils.StringUtil;
public class MyOssRule extends BladeOssRule {
    private String prefixPath;
    @Override
    public String bucketName(String bucketName) {
        return super.bucketName(bucketName);
    }
    public MyOssRule(Boolean tenantMode) {
        super(tenantMode);
    }
    public MyOssRule(Boolean tenantMode, String prefixPath) {
        super(tenantMode);
        if (!Strings.isBlank(prefixPath)) {
            this.prefixPath = prefixPath;
        }else {
            this.prefixPath = "upload";
        }
    }
    @Override
    public String fileName(String originalFilename) {
        return this.prefixPath + "/" + DateUtil.today() + "/" + StringUtil.randomUUID() + "." + FileUtil.getFileExtension(originalFilename);
    }
}