lin
2024-03-14 c4e2c196aad8af656d94bc47e9f1a80bc504b5d7
src/main/java/org/springblade/modules/resource/endpoint/OssEndpoint.java
@@ -19,6 +19,7 @@
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springblade.common.utils.ImageUtils;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.oss.model.OssFile;
@@ -28,8 +29,11 @@
import org.springblade.core.tool.constant.RoleConstant;
import org.springblade.core.tool.utils.FileUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.ImageUtil;
import org.springblade.modules.resource.builder.oss.OssBuilder;
import org.springblade.modules.resource.entity.Attach;
import org.springblade.modules.resource.entity.AttachData;
import org.springblade.modules.resource.service.IAttachDataService;
import org.springblade.modules.resource.service.IAttachService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@@ -55,6 +59,11 @@
    * 附件表服务
    */
   private final IAttachService attachService;
   /**
    * 附件数据表服务
    */
   private final IAttachDataService attachDataService;
   /**
    * 创建存储桶
@@ -206,10 +215,46 @@
    */
   @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);
      return R.data(bladeFile);
   }
   /**
    * 自定义前缀上传文件
    *
    * @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);
      buildAttachData(attachId,fileName, file.getSize(), bladeFile,file);
      bladeFile.setAttachId(attachId);
      // 修改link
      changeLink(bladeFile);
      return R.data(bladeFile);
   }
@@ -235,6 +280,28 @@
   }
   /**
    * 构建附件数据表
    *
    * @param attachId 附件id
    * @param fileName  文件名
    * @param fileSize  文件大小
    * @param bladeFile 对象存储文件
    * @return attachId
    */
   private Long buildAttachData(Long attachId,String fileName, Long fileSize, BladeFile bladeFile,MultipartFile file) {
      String fileExtension = FileUtil.getFileExtension(fileName);
      AttachData attach = new AttachData();
      attach.setAttachId(attachId);
      attach.setName(bladeFile.getName());
      attach.setOriginalName(bladeFile.getOriginalName());
      attach.setSize(fileSize);
      attach.setExtension(fileExtension);
      attach.setData(ImageUtils.mulToBase64(file));
      attachDataService.save(attach);
      return attach.getId();
   }
   /**
    * 删除文件
    *
    * @param fileName 存储桶对象名称