ソースを参照

10/26 xx "id修改为guid,并优化删除文件代码"

xiexiang 1 年間 前
コミット
ec379a4f67

+ 2 - 2
src/main/java/com/example/fms/common/model/dto/FMSDeleteFileDTO.java

@@ -12,9 +12,9 @@ import java.util.List;
 @Data
 public class FMSDeleteFileDTO {
     /**
-     * 需删除的ids
+     * 需删除的GUIDs
      */
-    private List<Integer> ids;
+    private List<String> guids;
 
     /**
      * 删除类型()

+ 10 - 30
src/main/java/com/example/fms/controller/FileMangerController.java

@@ -38,12 +38,6 @@ public class FileMangerController {
     private final FileMangerService fileManagerService;
     private final SystemFileService systemFileService;
 
-//    @PostMapping("/uploadSystemFile")
-//    @Operation(summary = "上传文件")
-//    public String upload(@RequestBody List<MultipartFile> files, Integer sourceId) {
-//        List<Integer> insertIds = fileManagerService.add(files, sourceId);
-//        return Response.success(insertIds);
-//    }
 
     @PostMapping("/uploadNormalFile")
     @Operation(summary = "上传普通文件")
@@ -75,25 +69,18 @@ public class FileMangerController {
 //        }
 //    }
 
-//    @GetMapping("/downloadSystemFile")
-//    @Operation(summary = "取出文件")
-//    public ResponseEntity<InputStreamResource> download(Integer fileId) throws Exception {
-//        byte[] fileData = fileManagerService.download(fileId);
-//        SystemFile systemFile = systemFileService.getById(fileId);
-//        String fileName = systemFile.getFileName();
-//        return ResponseEntity.ok().contentLength(fileData.length)
-//                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
-//                .contentType(MediaType.parseMediaType("application/octet-stream"))
-//                .body(new InputStreamResource(new ByteArrayInputStream(fileData)));
-//    }
 
     @GetMapping("/downloadFile")
     @Operation(summary = "下载文件")
-    public ResponseEntity<InputStreamResource> downloadFile(Integer fileId) {
+    public ResponseEntity<InputStreamResource> downloadFile(String fileId) {
+        //根据文件GUID获取文件信息
         byte[] fileData = fileManagerService.downloadFile(fileId);
-        SystemFile systemFile = systemFileService.getById(fileId);
+        LambdaQueryWrapper<SystemFile> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(SystemFile::getGUID, fileId);
+        SystemFile systemFile = systemFileService.getOne(queryWrapper);
         String fileName = systemFile.getOriginalName();
         try {
+            //文件原始名中的中文字符可能会乱码,对文件名进行URL编码
             String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
             return ResponseEntity.ok()
                     .contentLength(fileData.length)
@@ -103,30 +90,23 @@ public class FileMangerController {
         } catch (UnsupportedEncodingException e){
             throw new XiaoShiException("文件名取出时发生错误!");
         }
-
-
     }
 
+
     @PostMapping("/getFileData")
     @Operation(summary = "获取文件信息")
-    public List<SystemFile> getFileData(@RequestBody List<Integer> fileIds) {
+    public List<SystemFile> getFileData(@RequestBody List<String> fileIds) {
         LambdaQueryWrapper<SystemFile> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.in(SystemFile::getId, fileIds);
+        queryWrapper.in(SystemFile::getGUID, fileIds);
         List<SystemFile> systemFiles = systemFileService.list(queryWrapper);
         return systemFiles;
     }
 
-//    @PostMapping("/deleteSystemFile")
-//    @Operation(summary = "删除文件")
-//    public String delete(@RequestBody FMSDeleteFileDTO fMSDeleteFileDTO) {
-//        fileManagerService.delete(fMSDeleteFileDTO.getIds(), fMSDeleteFileDTO.getType());
-//        return Response.success("删除系统文件成功");
-//    }
 
     @PostMapping("/deleteFile")
     @Operation(summary = "删除文件")
     public String deleteFile(@RequestBody FMSDeleteFileDTO fMSDeleteFileDTO) {
-        String mes = fileManagerService.deleteFile(fMSDeleteFileDTO.getIds(), fMSDeleteFileDTO.getType());
+        String mes = fileManagerService.deleteFile(fMSDeleteFileDTO.getGuids(), fMSDeleteFileDTO.getType());
         return Response.success(mes);
     }
 

+ 32 - 18
src/main/java/com/example/fms/service/FileMangerService.java

@@ -1,5 +1,6 @@
 package com.example.fms.service;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.example.fms.common.model.dto.DownloadSysFileDTO;
 import com.example.fms.common.model.dto.SystemFileDTO;
 import com.example.fms.common.model.vo.ConfigSettingVO;
@@ -16,6 +17,7 @@ import org.springframework.util.CollectionUtils;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -27,7 +29,6 @@ import java.util.List;
 @Slf4j
 @Service
 public class FileMangerService {
-    private final OldFileFactoryService oldFileFactoryService;
     private final SystemFileService systemFileService;
     private final SystemFileMapper systemFileMapper;
     private final FileFactory fileFactory;
@@ -70,12 +71,14 @@ public class FileMangerService {
      * @return fileData 文件流
      * @throws Exception
      */
-    public byte[] downloadFile(Integer fileId) {
+    public byte[] downloadFile(String fileId) {
         if (fileId != null) {
             //1.新建一个对象
             DownloadSysFileDTO downloadSysFileDTO = new DownloadSysFileDTO();
             //2.根据传入文件id查询到文件信息
-            SystemFileVO systemFileVO = systemFileService.query(fileId);
+            LambdaQueryWrapper<SystemFile> queryWrapper = new LambdaQueryWrapper<>();
+            queryWrapper.eq(SystemFile::getGUID, fileId);
+            SystemFile systemFileVO = systemFileService.getOne(queryWrapper);
             //3.文件信息标记为已经删除的,返回null
             if (systemFileVO.getIsDelete().equals(1)) {
                 return null;
@@ -107,13 +110,13 @@ public class FileMangerService {
     /**
      * 删除系统文件
      * 分为物理删除和逻辑删除,逻辑删除只更新数据库中isDelete字段,物理删除需要调用工厂类方法
-     * @param ids 需要删除的id集合
+     * @param guIds 需要删除的GUID集合
      * @param type 删除方式判断字段,1为逻辑删除,2为物理删除
      */
-    public String deleteFile(List<Integer> ids, Integer type) {
+    public String deleteFile(List<String> guIds, Integer type) {
         //1.首先判断传入的需要删除的id集合不能为空
-        if (CollectionUtils.isEmpty(ids)) {
-            throw new XiaoShiException("需要删除的id集合不能为空");
+        if (CollectionUtils.isEmpty(guIds)) {
+            throw new XiaoShiException("需要删除的GUID集合不能为空");
         }
         //2.其次判断传入的删除类型不能为空
         if (type == null) {
@@ -122,9 +125,11 @@ public class FileMangerService {
         //3.1 判断如果是逻辑删除(更新isDelete字段为1)
         if (type.equals(1)) {
             //3.1.1 遍历传入的id集合
-            for (int i = 0; i < ids.size(); i++) {
+            for (int i = 0; i < guIds.size(); i++) {
                 //3.1.2 获取需要更新的对象
-                SystemFileVO systemFileVO = systemFileMapper.query(ids.get(i));
+                LambdaQueryWrapper<SystemFile> queryWrapper = new LambdaQueryWrapper<>();
+                queryWrapper.eq(SystemFile::getGUID, guIds.get(i));
+                SystemFile systemFileVO = systemFileService.getOne(queryWrapper);
                 //3.1.3 将对象的是否删除字段置为1
                 systemFileVO.setIsDelete(1);
                 //3.1.4 将查询出来的vo赋值给实体类
@@ -141,10 +146,13 @@ public class FileMangerService {
             String mes = "逻辑删除成功";
             return mes;
         } else if (type.equals(2)) { //3.2 物理删除(先删除服务器文件,删除数据库中记录)
-            //3.2.1 遍历传入的id集合
-            for (int i = 0; i < ids.size(); i++) {
-                //3.2.2 根据id到表中查询该文件记录的数据
-                SystemFileVO systemFileVO = systemFileService.query(ids.get(i));
+            //3.2.1 遍历传入的GUID集合
+            List<Integer> deleteIds = new ArrayList<>();
+            for (int i = 0; i < guIds.size(); i++) {
+                //3.2.2 根据GUID到表中查询该文件记录的数据
+                LambdaQueryWrapper<SystemFile> queryWrapper = new LambdaQueryWrapper<>();
+                queryWrapper.eq(SystemFile::getGUID, guIds.get(i));
+                SystemFile systemFileVO = systemFileService.getOne(queryWrapper);
                 //3.2.3 根据pType判断存储在服务器的哪个地址下
                 int pType = systemFileVO.getPType();
                 //3.2.4 文件路径
@@ -157,12 +165,18 @@ public class FileMangerService {
                 //TODO 调用工厂方法,工厂方法会根据sourceName创建并返回对应的方法的对象
                 IFileFactory fileFactoryObject = fileFactory.createObject(sourceName);
                 fileFactoryObject.deleteFile(filePath, configSettingVO);
-                int row = systemFileMapper.deleteById(ids.get(i));
-                if (row != 1) {
-                    throw new XiaoShiException("第" + i + "条删除异常");
-                }
+                deleteIds.add(systemFileVO.getId());
+//                //3.2.7 调用removeById删除此条记录,主键为根据GUID查询到的对象的id
+//                Boolean isDelete = systemFileService.removeById(systemFileVO.getId());
+//                if (isDelete != true) {
+//                    throw new XiaoShiException("第" + i + "条删除异常");
+//                }
+            }
+            Boolean isDelete = systemFileService.removeByIds(deleteIds);
+            if (isDelete != true) {
+                throw new XiaoShiException("删除异常");
             }
-            String mes = "物理删除成功";
+            String mes = "文件删除成功,文件记录删除成功";
             return mes;
         } else {
             String mes = "删除类型错误";

+ 0 - 153
src/main/java/com/example/fms/service/OldFileFactoryService.java

@@ -1,153 +0,0 @@
-package com.example.fms.service;
-
-
-import com.example.fms.common.model.dto.DownloadSysFileDTO;
-import com.example.fms.common.model.dto.SystemFileDTO;
-import com.example.fms.common.model.vo.ConfigSettingVO;
-import com.example.fms.common.utils.ExcuteConfigUtils;
-import com.example.fms.common.utils.FileUtils;
-import com.example.fms.exception.XiaoShiException;
-import lombok.RequiredArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.stereotype.Service;
-import org.springframework.web.multipart.MultipartFile;
-
-import java.util.*;
-
-/**
- * 文件工厂类
- *
- * @Author xiexiang
- * @Date 2023/6/2
- */
-@Slf4j
-@Service
-@RequiredArgsConstructor
-public class OldFileFactoryService {
-    private final FileUtils fileUtils;
-
-//    /**
-//     * 上传文件
-//     *
-//     * @param files
-//     * @param sourceId
-//     * @return
-//     */
-//    public List<SystemFileDTO> uploadFiles(List<MultipartFile> files, Integer sourceId) {
-//        if (files != null && files.size() != 0) {
-//            //调用解析配置方法,获取配置信息
-//            List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
-//            //根据配置id获得配置
-//            ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(sourceId)).findFirst().orElse(null);
-//            if (configSettingVO == null) {
-//                return null;
-//            }
-//            String sourceName = configSettingVO.getSourceName();
-//            if (sourceName.equals("FSS")) {
-//                return this.uploadToFSS(files, configSettingVO);
-//            }
-//            return null;
-//        }
-//        throw new XiaoShiException("传入文件为空");
-//    }
-
-//
-//    /**
-//     * 上传到服务器
-//     *
-//     * @param files
-//     * @param configSettingVO
-//     * @return
-//     */
-//    public List<SystemFileDTO> uploadToFSS(List<MultipartFile> files, ConfigSettingVO configSettingVO) {
-//        List<SystemFileDTO> systemFileDTOS = new ArrayList<>();
-//        for (MultipartFile file : files) {
-//            try {
-//                String directoryName = fileUtils.getDirectoryName();
-//                SystemFileDTO systemFileDTO = SftpService.upload(configSettingVO.getFilePath() + directoryName, file, configSettingVO);
-//                systemFileDTO.setPType(configSettingVO.getId());
-//                systemFileDTO.setSourceId(configSettingVO.getSourceId());
-//                systemFileDTO.setOriginalName(file.getOriginalFilename());
-//                systemFileDTO.setFilePath(configSettingVO.getFilePath() + directoryName + "/" + systemFileDTO.getFileName());
-//                systemFileDTOS.add(systemFileDTO);
-//            } catch (Exception e) {
-//                e.printStackTrace();
-//            }
-//        }
-//        return systemFileDTOS;
-//    }
-
-
-//    /**
-//     * 下载文件
-//     *
-//     * @param downloadSysFileDTO
-//     * @throws Exception
-//     */
-//    public byte[] download(DownloadSysFileDTO downloadSysFileDTO) throws Exception {
-//        Integer id = downloadSysFileDTO.getPType();
-//        //调用解析配置方法,获取配置信息
-//        List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
-//        //根据传入sourceId判断从哪里下载数据
-//        ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(id)).findFirst().orElse(null);
-//        if (configSettingVO == null) {
-//            throw new XiaoShiException("解析错误");
-//        }
-//        String sourceName = configSettingVO.getSourceName();
-//        if (sourceName.equals("FSS")) {
-//            byte[] file = this.downloadFromFSS(downloadSysFileDTO, configSettingVO);
-//            return file;
-//        } else {
-//            return null;
-//        }
-//    }
-//
-//    /**
-//     * @param //directory    下载目录 根据SFTP设置的根目录来进行传输
-//     * @param //downloadFile 下载的文件
-//     * @param //saveFile     存在本地的路径
-//     */
-//    public byte[] downloadFromFSS(DownloadSysFileDTO downloadSysFileDTO, ConfigSettingVO configSettingVO) throws Exception {
-//        //下载目录,也是存储路径
-//        String filePath = downloadSysFileDTO.getFilePath();
-//        int index = filePath.lastIndexOf("/");
-//        String directory = filePath.substring(0, index);
-//        //下载的文件,也就是文件名
-//        String downloadFile = downloadSysFileDTO.getFileName();
-//        //存在本地的路径,随机生成
-////        String saveFile = "C:\\Users\\Admin\\Desktop\\downloadFile";
-////        byte[] fileData = SftpService.download(directory, downloadFile, saveFile, configSettingVO);
-//        byte[] fileData = SftpService.download(directory, downloadFile, configSettingVO);
-//        return fileData;
-//    }
-
-//    /**
-//     * 删除文件
-//     *
-//     * @param filePath
-//     * @param pType
-//     */
-//    public static void delete(String filePath, Integer pType) {
-//        //调用解析配置方法,获取配置信息
-//        List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
-//        ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(pType)).findFirst().orElse(null);
-//        //根据传入类型判断从哪里删除数据
-//        //如果为1,则为服务器
-//        //if(sourceId.equals(1)){
-//        //拆分路径字符串
-//        int index = filePath.lastIndexOf('/');
-//        //要删除文件所在目录
-//        String directory = filePath.substring(0, index);
-//        //要删除的文件
-//        String deleteFile = filePath.substring(index + 1, filePath.length());
-//        try {
-//            SftpService.delete(directory, deleteFile, configSettingVO);
-//        } catch (Exception e) {
-//            throw new XiaoShiException("删除错误");
-//        }
-//        //} else if(sourceId.equals(2)){//类型为2,则为阿里云OSS
-//        //}
-//    }
-
-
-}