From 9bce48a9f30f7d80c43f43f46d40df20fcb00e15 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期五, 03 二月 2023 09:39:59 +0800
Subject: [PATCH] 1

---
 src/main/java/com/lf/server/service/all/BaseUploadService.java |  109 +++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 90 insertions(+), 19 deletions(-)

diff --git a/src/main/java/com/lf/server/service/all/BaseUploadService.java b/src/main/java/com/lf/server/service/all/BaseUploadService.java
index e4ce665..103b928 100644
--- a/src/main/java/com/lf/server/service/all/BaseUploadService.java
+++ b/src/main/java/com/lf/server/service/all/BaseUploadService.java
@@ -6,6 +6,7 @@
 import com.lf.server.entity.data.VerEntity;
 import com.lf.server.entity.sys.UserEntity;
 import com.lf.server.helper.*;
+import com.lf.server.service.data.DirService;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -27,7 +28,10 @@
 @Service
 public class BaseUploadService {
     @Autowired
-    public PathHelper pathHelper;
+    protected DirService dirService;
+
+    @Autowired
+    protected PathHelper pathHelper;
 
     public final Log log = LogFactory.getLog(getClass());
 
@@ -190,6 +194,7 @@
         }
 
         List<MetaFileEntity> list = new ArrayList<>();
+        List<DirEntity> dirs = dirService.selectDirRecursive(dir.getName());
         for (MetaFileEntity meta : metas) {
             meta.setEventid(StringHelper.getGuid());
             meta.setDirid(dir.getId());
@@ -199,16 +204,16 @@
             meta.setEpsgCode(epsgCode);
 
             if (StaticData.ZIP.equals(meta.getExtName())) {
-                List<MetaFileEntity> subs = getMapperFiles(path, meta);
+                List<MetaFileEntity> subs = getMapperFiles(path, dir, dirs, meta);
                 if (null != subs && subs.size() > 0) {
                     list.addAll(subs);
                     continue;
                 }
             }
 
-            meta.setEventid(StringHelper.getGuid());
             list.add(meta);
         }
+        setMetaType(list);
 
         return list;
     }
@@ -216,8 +221,9 @@
     /**
      * 鑾峰彇鏄犲皠鏂囦欢
      */
-    private List<MetaFileEntity> getMapperFiles(String path, MetaFileEntity meta) {
-        String zipFile = pathHelper.getConfig().getTempPath() + File.separator + meta.getPath();
+    private List<MetaFileEntity> getMapperFiles(String path, DirEntity dir, List<DirEntity> dirs, MetaFileEntity meta) {
+        String tempPath = pathHelper.getConfig().getTempPath();
+        String zipFile = tempPath + File.separator + meta.getPath();
         File file = new File(zipFile);
         if (!file.exists() || file.isDirectory()) {
             return null;
@@ -231,19 +237,50 @@
         String subPath = zipFolder + File.separator + meta.getName().toLowerCase().replace(".zip", "");
         ZipHelper.unzip(zipFile, subPath);
 
-        File subFile = new File(subPath);
-        File[] files = subFile.listFiles();
-        if (null == files || files.length == 0) {
-            return null;
+        List<File> files = new ArrayList<>();
+        getFilesByPath(files, subPath);
+
+        return getMapperFiles(files, dir, dirs, meta, tempPath.length() + 1);
+    }
+
+    /**
+     * 鏍规嵁璺緞鑾峰彇鏂囦欢
+     */
+    private void getFilesByPath(List<File> list, String path) {
+        File file = new File(path);
+        if (!file.isDirectory()) {
+            String extName = FileHelper.getExtension(file);
+            if (StaticData.ALL_EXTENSION.contains(extName)) {
+                list.add(file);
+            }
+            return;
         }
 
-        return getMapperFiles(files, meta, subPath);
+        if (isGdbFile(file)) {
+            list.add(file);
+            return;
+        }
+
+        File[] files = file.listFiles();
+        if (null == files) {
+            return;
+        }
+        for (File f : files) {
+            if (f.isDirectory()) {
+                getFilesByPath(list, f.getPath());
+            } else {
+                String extName = FileHelper.getExtension(f);
+                if (StaticData.ALL_EXTENSION.contains(extName)) {
+                    list.add(f);
+                }
+            }
+        }
     }
 
     /**
      * 鑾峰彇鏄犲皠鏂囦欢
      */
-    private List<MetaFileEntity> getMapperFiles(File[] files, MetaFileEntity meta,String subPath) {
+    private List<MetaFileEntity> getMapperFiles(List<File> files, DirEntity dir, List<DirEntity> dirs, MetaFileEntity meta, int start) {
         List<MetaFileEntity> list = new ArrayList<>();
         for (File f : files) {
             boolean isGdb = isGdbFile(f);
@@ -253,13 +290,14 @@
 
             String fileName = FileHelper.getFileName(f.getPath());
             String extName = FileHelper.getExtension(fileName);
-            if (!StaticData.ALL_EXTENSION.contains(extName)) {
+            if (!StaticData.ALL_EXTENSION.contains(extName) || fileName.startsWith("~")) {
                 continue;
             }
 
+            int dirid = getDirByPath(f.getPath(), fileName, dir, dirs);
             boolean isShp = StaticData.SHP.equals(extName);
             if (isGdb) {
-                List<MetaFileEntity> rs = getGdbMappers(f, meta, subPath);
+                List<MetaFileEntity> rs = getGdbMappers(f, meta, dirid, start);
                 if (null != rs && rs.size() > 0) {
                     list.addAll(rs);
                 }
@@ -267,14 +305,15 @@
             }
 
             MetaFileEntity mf = createMetaFileEntity(meta);
+            mf.setDirid(dirid);
             mf.setEventid(StringHelper.getGuid());
             mf.setName(fileName);
             mf.setExtName(extName);
-            mf.setPath(f.getPath().substring(subPath.length()));
+            mf.setPath(f.getPath().substring(start));
 
             if (isShp) {
                 List<String> shpFiles = getShpFiles(f.getPath());
-                mf.setTab(fileName);
+                mf.setTab(fileName.replace(StaticData.SHP, ""));
                 mf.setSizes(getFilesSize(shpFiles));
                 mf.setGuid(getFilesMd5(shpFiles));
             } else {
@@ -302,14 +341,34 @@
     }
 
     /**
+     * 鏍规嵁鏂囦欢璺緞鑾峰彇鐩綍ID
+     */
+    private int getDirByPath(String filePath,String fileName, DirEntity dir, List<DirEntity> dirs) {
+        if (0 != dir.getPid() || null == dirs || dirs.isEmpty()) {
+            return dir.getId();
+        }
+        if (StaticData.SLASH.equals(File.separator)) {
+            filePath = filePath.replace("/", "\\");
+        }
+
+        for (DirEntity entity : dirs) {
+            if (filePath.contains(entity.getFullName() + "\\" + fileName)) {
+                return entity.getId();
+            }
+        }
+
+        return dir.getId();
+    }
+
+    /**
      * 鑾峰彇SHP鏂囦欢闆嗗悎
      */
     private  List<String> getShpFiles(String shpPath) {
         List<String> list = new ArrayList<>();
         list.add(shpPath);
 
-        for (int i = 1, c = StaticData.SHP_EXTENSION.size(); i < c; i++) {
-            String path = shpPath.replace(".shp", StaticData.SHP_EXTENSION.get(i));
+        for (int i = 0, c = StaticData.SHP_EXT.size(); i < c; i++) {
+            String path = shpPath.replace(".shp", StaticData.SHP_EXT.get(i));
 
             File f = new File(path);
             if (f.exists() && !f.isDirectory()) {
@@ -375,7 +434,7 @@
     /**
      * 鑾峰彇GDB鏂囦欢鏄犲皠
      */
-    private List<MetaFileEntity> getGdbMappers(File f, MetaFileEntity meta, String subPath) {
+    private List<MetaFileEntity> getGdbMappers(File f, MetaFileEntity meta, int dirid, int start) {
         List<String> tabs = GdbHelper.getTabNames(f.getPath());
         if (null == tabs || tabs.size() == 0) {
             return null;
@@ -391,11 +450,12 @@
         List<MetaFileEntity> list = new ArrayList<>();
         for (String tab : tabs) {
             MetaFileEntity mf = createMetaFileEntity(meta);
+            mf.setDirid(dirid);
             mf.setEventid(StringHelper.getGuid());
             mf.setName(fileName);
             mf.setExtName(extName);
             mf.setSizes(FileHelper.sizeToMb(f.length()));
-            mf.setPath(f.getPath().substring(subPath.length()));
+            mf.setPath(f.getPath().substring(start));
             mf.setTab(tab);
             mf.setSizes(sizes);
             mf.setGuid(md5);
@@ -426,6 +486,17 @@
     }
 
     /**
+     * 璁剧疆鍏冩暟鎹枃浠剁殑绫诲瀷
+     */
+    private void setMetaType(List<MetaFileEntity> list) {
+        for (MetaFileEntity mf : list) {
+            if (null != mf.getExtName()) {
+                mf.setType(mf.getExtName().replace(".", ""));
+            }
+        }
+    }
+
+    /**
      * 鑾峰彇鍙傛暟 *
      * Enumeration<String> headers = req.getHeaderNames();
      * Enumeration<String> attributes = req.getAttributeNames();

--
Gitblit v1.9.3