Selaa lähdekoodia

新增文件删除byurl

车车 3 kuukautta sitten
vanhempi
sitoutus
ddda251d44

+ 10 - 0
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java

@@ -75,6 +75,16 @@ public class FileController {
         return success(true);
     }
 
+    @DeleteMapping("/deleteByUrl")
+    @Operation(summary = "删除文件")
+    @Parameter(name = "url", description = "编号", required = true)
+    @PreAuthorize("@ss.hasPermission('infra:file:delete')")
+    public CommonResult<Boolean> deleteByUrl(@RequestParam("url") String url) throws Exception {
+        fileService.deleteByUrl(url);
+        return success(true);
+    }
+
+
     @GetMapping("/{configId}/get/**")
     @PermitAll
     @TenantIgnore

+ 7 - 0
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java

@@ -60,6 +60,13 @@ public interface FileService {
      */
     void deleteFile(Long id) throws Exception;
 
+    /**
+     * 删除文件
+     *
+     * @param url url
+     */
+    void deleteByUrl(String url) throws Exception;
+
     /**
      * 获得文件内容
      *

+ 28 - 0
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java

@@ -17,9 +17,11 @@ import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper;
 import cn.iocoder.yudao.module.infra.framework.file.core.client.FileClient;
 import cn.iocoder.yudao.module.infra.framework.file.core.client.s3.FilePresignedUrlRespDTO;
 import cn.iocoder.yudao.module.infra.framework.file.core.utils.FileTypeUtils;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.google.common.annotations.VisibleForTesting;
 import jakarta.annotation.Resource;
 import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -32,6 +34,7 @@ import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_NOT_EX
  *
  * @author 芋道源码
  */
+@Slf4j
 @Service
 public class FileServiceImpl implements FileService {
 
@@ -159,6 +162,31 @@ public class FileServiceImpl implements FileService {
         fileMapper.deleteById(id);
     }
 
+    @Override
+    public void deleteByUrl(String url) {
+        try {
+            FileDO fileDO = fileMapper.selectOne(Wrappers.<FileDO>lambdaQuery()
+                    .eq(FileDO::getUrl, url));
+            if (fileDO == null) {
+                throw exception(FILE_NOT_EXISTS);
+            }
+
+            // 校验存在
+            FileDO file = validateFileExists(fileDO.getId());
+
+            // 从文件存储器中删除
+            FileClient client = fileConfigService.getFileClient(file.getConfigId());
+            Assert.notNull(client, "客户端({}) 不能为空", file.getConfigId());
+            client.delete(file.getPath());
+
+            // 删除记录
+            fileMapper.deleteById(fileDO.getId());
+        } catch (Exception e) {
+            log.error("文件删除失败{}", e.getMessage());
+        }
+
+    }
+
     private FileDO validateFileExists(Long id) {
         FileDO fileDO = fileMapper.selectById(id);
         if (fileDO == null) {