Browse Source

上传数据做校验!

chenendian 3 tháng trước cách đây
mục cha
commit
14f531546a

+ 14 - 4
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/controller/NodeAttachmentController.java

@@ -1,7 +1,9 @@
 package com.siwei.apply.controller;
 
 import com.siwei.apply.domain.NodeAttachment;
+import com.siwei.apply.domain.ProjectWorkflow;
 import com.siwei.apply.service.NodeAttachmentService;
+import com.siwei.apply.service.ProjectService;
 import com.siwei.apply.utils.ServiceFileUtil;
 import com.siwei.common.core.domain.R;
 import com.siwei.common.core.utils.StringUtils;
@@ -23,6 +25,10 @@ public class NodeAttachmentController extends BaseController {
     @Autowired
     private NodeAttachmentService nodeAttachmentService;
 
+    @Autowired
+    private ProjectService projectService;
+
+
     /**
      * 处理文件并保存附件信息
      *
@@ -33,12 +39,17 @@ public class NodeAttachmentController extends BaseController {
     public R<Map<String, String>> processFile(@RequestBody Map<String, String> requestBody) {
         try {
             String filePath = requestBody.get("filePath");
+            String projectId = requestBody.get("projectId");
+            String nodeId = requestBody.get("id");
 
             if (filePath == null || filePath.trim().isEmpty()) {
                 return R.fail("filePath不能为空");
             }
-
-            String id = nodeAttachmentService.processFileAndSave(filePath);
+            if (nodeId == null || nodeId.trim().isEmpty()) {
+                return R.fail("nodeId不能为空");
+            }
+            ProjectWorkflow projectWorkflow = projectService.getProjectWorkflowByNodeId(projectId,nodeId);
+            String id = nodeAttachmentService.processFileAndSave(filePath,projectWorkflow.getNodeTableName());
 
             if (id != null) {
                 Map<String, String> responseData = new HashMap<>();
@@ -50,14 +61,13 @@ public class NodeAttachmentController extends BaseController {
 
         } catch (Exception e) {
             logger.error("处理文件异常", e);
-            return R.fail("处理文件异常:" + e.getMessage());
+            return R.fail(502,"处理文件异常:" + e.getMessage());
         }
     }
 
 
 
 
-
     /**
      * 删除某个附件信息
      *

+ 1 - 1
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/NodeAttachmentService.java

@@ -32,7 +32,7 @@ public interface NodeAttachmentService {
      * @param filePath 文件路径
      * @return 数据库存储后的id
      */
-    String processFileAndSave(String filePath);
+    String processFileAndSave(String filePath,String nodeTableName);
     
     /**
      * 删除节点附件信息

+ 3 - 0
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/ProjectService.java

@@ -2,6 +2,7 @@ package com.siwei.apply.service;
 
 import com.alibaba.fastjson2.JSONArray;
 import com.siwei.apply.domain.Project;
+import com.siwei.apply.domain.ProjectWorkflow;
 import com.siwei.apply.domain.res.ProjectCycleRes;
 import com.siwei.apply.domain.res.ProjectOverviewRes;
 import com.siwei.apply.domain.vo.ProjectFilterVo;
@@ -74,6 +75,8 @@ public interface ProjectService {
 
     JSONArray getResourceData() throws IOException;
 
+    ProjectWorkflow getProjectWorkflowByNodeId(String projectId, String nodeId);
+
     void testData() ;
 
 }

+ 0 - 1
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/impl/GyjsydscdjImpl.java

@@ -24,7 +24,6 @@ import com.siwei.apply.mapper.ProjectWorkflowMapper;
 import com.siwei.apply.mapper.WorkflowMapper;
 
 import java.util.List;
-import java.util.Collections;
 
 /**
  * 国有建设用地使用权首次登记 服务实现类

+ 30 - 9
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/impl/NodeAttachmentImpl.java

@@ -16,15 +16,18 @@ import com.siwei.apply.utils.FileExtractUtil;
 import com.siwei.apply.utils.ServiceFileUtil;
 import com.siwei.apply.utils.ServiceUtil;
 import com.siwei.common.core.exception.ServiceException;
+import org.apache.catalina.core.ApplicationContext;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.ibatis.annotations.Param;
+import org.apache.poi.sl.usermodel.ObjectMetaData;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.io.IOException;
 import java.nio.file.*;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -120,14 +123,6 @@ public class NodeAttachmentImpl implements NodeAttachmentService {
 
 
 
-
-
-
-
-
-
-
-
     /**
      * 处理文件并保存附件信息
      * @param filePath 文件路径
@@ -135,7 +130,7 @@ public class NodeAttachmentImpl implements NodeAttachmentService {
      */
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public String processFileAndSave(String filePath) {
+    public String processFileAndSave(String filePath,String nodeTableName) {
         try {
             if (filePath == null || filePath.trim().isEmpty()) {
                 logger.warn("处理文件失败:filePath不能为空");
@@ -155,11 +150,18 @@ public class NodeAttachmentImpl implements NodeAttachmentService {
                 // 解压文件到同名文件夹并获取目录结构
                 String extractDir = FileExtractUtil.extractToSameNameFolder(filePath);
                 if (extractDir != null) {
+                    //todo 这里需要校验文件名称,如果不在枚举类中,需要删除整个目录,并且提示
+                    if(!this.validUploadZipFile(extractDir,nodeTableName)){
+                        //把当前解压的目录删除
+                        ServiceFileUtil.deleteDirectory(extractDir);
+                        throw new ServiceException("请按照固定目录名上传");
+                    }
                     directoryStructure = FileExtractUtil.getDirectoryStructure(Paths.get(extractDir));
                 } else {
                     logger.error("解压文件失败: {}", filePath);
                     return null;
                 }
+
             }else if(Files.isDirectory(file)) {
                 // 如果是目录直接获取目录信息
                 directoryStructure = FileExtractUtil.getDirectoryStructure(file);
@@ -562,6 +564,25 @@ public class NodeAttachmentImpl implements NodeAttachmentService {
     }
 
 
+    public  Boolean  validUploadZipFile(String uploadFilePath,String tableName){
+        Map<String,List<String>>  currentAttachmentMap =  getCurrentAttachmentName(tableName);
+        List<String> currentAttachmentList = currentAttachmentMap.values().stream().findFirst().get();
+        boolean retFlag = true;
+        try {
+            List<String> directoryPathList = Files.list(Paths.get(uploadFilePath))
+                    .filter(Files::isDirectory).map(s->s.getFileName().toString()).collect(Collectors.toList());
+            for(String currentAttachment : currentAttachmentList){
+                if(!CollectionUtils.containsAny(directoryPathList,currentAttachment)){
+                    retFlag = false;
+                }
+            }
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        return retFlag;
+    }
+
+
 
 
 

+ 17 - 6
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/impl/ProjectImpl.java

@@ -331,13 +331,24 @@ public class ProjectImpl implements ProjectService {
         return map;
     }
 
+    @Override
+    public ProjectWorkflow getProjectWorkflowByNodeId(String projectId,String nodeId) {
+        ProjectWorkflow projectWorkflow = null;
+        List<ProjectWorkflow> projectWorkflowList = projectWorkflowMapper.selectByNodeId(nodeId,projectId);
+        if(CollectionUtils.isNotEmpty(projectWorkflowList) && Objects.nonNull(projectWorkflowList.get(0))) {
+            projectWorkflow = projectWorkflowList.get(0);
+        }
+        return  projectWorkflow;
+    }
 
-    /**
-     * 递归解析json文件数据
-     * @param jsonObj
-     * @return
-     * @throws IOException
-     */
+
+
+        /**
+         * 递归解析json文件数据
+         * @param jsonObj
+         * @return
+         * @throws IOException
+         */
     public static List<String> getAllAttachmentList(JSONObject jsonObj) throws IOException {
         List<String> list = new ArrayList<>();
         if(jsonObj!=null && jsonObj.getString("type").equals("directory") && jsonObj.getJSONArray("children")!=null){

+ 1 - 1
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/utils/FileExtractUtil.java

@@ -209,7 +209,7 @@ public class FileExtractUtil {
             Map<String, Object> structure = new HashMap<>();
             structure.put("name", directory.getFileName().toString());
             structure.put("type", Files.isDirectory(directory) ? "directory" : "file");
-            structure.put("physicalFlag", "1");
+            //structure.put("physicalFlag", "1");
             structure.put("path", directory.toString());
             
             if (Files.isDirectory(directory)) {

+ 14 - 4
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/utils/ServiceFileUtil.java

@@ -8,12 +8,14 @@ import org.apache.commons.io.FilenameUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.nio.file.StandardCopyOption;
 import java.util.Arrays;
+import java.util.Comparator;
 
 /**
  * 文件工具类
@@ -35,8 +37,6 @@ public class ServiceFileUtil {
 
 
 
-
-
     //给定一个名称创建一个目录,返回路径
     public static String  createDirectory(String directoryName) {
         String directoryPathAndName = null;
@@ -68,7 +68,6 @@ public class ServiceFileUtil {
     }
 
 
-
     //给定一个名称创建一个目录,返回路径
     public static String  createDirectory(String directoryPath, String directoryName) {
         String directoryPathAndName = null;
@@ -118,7 +117,18 @@ public class ServiceFileUtil {
         return directoryPath;
     }
 
-
+    //删除当前目录即使存在文件
+    public static void deleteDirectory(String directoryPath) throws IOException {
+        Path dirPath = Paths.get(directoryPath);
+        if (!Files.exists(dirPath)) {
+            logger.warn("目录不存在: {}", directoryPath);
+            throw new ServiceException("目录不存在: "+directoryPath);
+        }
+        Files.walk(dirPath)
+                .sorted(Comparator.reverseOrder())
+                .map(Path::toFile)
+                .forEach(File::delete);
+    }