From ed8c7a5effd0d423ce1118b680ecdca6fe732609 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 02 七月 2025 16:43:13 +0800 Subject: [PATCH] Merge branch 'master' of http://192.168.11.205:9000/r/P2022036_Service --- src/main/java/com/lf/server/service/all/BaseUploadService.java | 178 +++++++++++++++++++++++++++++++++++++++++----------------- 1 files changed, 125 insertions(+), 53 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 2bb8137..eb21b50 100644 --- a/src/main/java/com/lf/server/service/all/BaseUploadService.java +++ b/src/main/java/com/lf/server/service/all/BaseUploadService.java @@ -7,6 +7,7 @@ import com.lf.server.entity.sys.UserEntity; import com.lf.server.helper.*; import com.lf.server.service.data.DirService; +import com.lf.server.service.sys.TokenService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -14,6 +15,7 @@ import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest; +import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; @@ -27,11 +29,14 @@ */ @Service public class BaseUploadService { - @Autowired + @Resource protected DirService dirService; - @Autowired + @Resource protected PathHelper pathHelper; + + @Resource + protected TokenService tokenService; public final Log log = LogFactory.getLog(getClass()); @@ -129,7 +134,7 @@ /** * 鏌ヨ鏂囦欢 */ - public List<MetaFileEntity> selectFiles(String subPath) { + public List<MetaFileEntity> selectFiles(String subPath, boolean hasMd5) { String root = pathHelper.getConfig().getTempPath() + File.separator + subPath; File file = new File(root); @@ -145,7 +150,7 @@ for (File f : files) { String fileName = FileHelper.getFileName(f.getPath()); String extName = FileHelper.getExtension(fileName); - if (!isExtValid(extName)) { + if (!isFileValid(f.getName())) { continue; } @@ -154,7 +159,7 @@ mf.setExtName(extName); mf.setSizes(FileHelper.sizeToMb(f.length())); mf.setPath(subPath + File.separator + fileName); - if (!StaticData.ZIP.equals(extName)) { + if (hasMd5 && !StaticData.ZIP.equals(extName)) { mf.setGuid(FileHelper.getFileMd5(f.getPath())); } @@ -190,32 +195,30 @@ * 鏌ヨ鏄犲皠 */ public List<MetaFileEntity> selectMappers(UserEntity ue, String path, DirEntity dir, VerEntity ver, String epsgCode) { - List<MetaFileEntity> metas = selectFiles(path); + List<MetaFileEntity> metas = selectFiles(path, true); if (null == metas || metas.isEmpty()) { return null; } List<MetaFileEntity> list = new ArrayList<>(); - List<DirEntity> dirs = dirService.selectRecursiveById(dir.getId()); for (MetaFileEntity meta : metas) { meta.setEventid(StringHelper.getGuid()); - meta.setDircode(dir.getCode()); meta.setDepcode(ue.getDepcode()); meta.setVerid(ver.getId()); meta.setCreateUser(ue.getId()); meta.setEpsgCode(epsgCode); if (StaticData.ZIP.equals(meta.getExtName())) { - List<MetaFileEntity> subs = getMapperFiles(path, dir, dirs, meta); + List<MetaFileEntity> subs = getMapperFiles(path, meta); if (null != subs && subs.size() > 0) { list.addAll(subs); continue; } } - list.add(meta); } setMetaType(list); + setDircode(list, dir); return list; } @@ -223,7 +226,7 @@ /** * 鑾峰彇鏄犲皠鏂囦欢 */ - private List<MetaFileEntity> getMapperFiles(String path, DirEntity dir, List<DirEntity> dirs, MetaFileEntity meta) { + private List<MetaFileEntity> getMapperFiles(String path, MetaFileEntity meta) { String tempPath = pathHelper.getConfig().getTempPath(); String zipFile = tempPath + File.separator + meta.getPath(); File file = new File(zipFile); @@ -243,7 +246,7 @@ List<File> files = new ArrayList<>(); getFilesByPath(files, subPath); - return getMapperFiles(files, dir, dirs, meta, tempPath.length() + 1); + return getMapperFiles(files, meta, tempPath.length() + 1); } /** @@ -252,8 +255,7 @@ private void getFilesByPath(List<File> list, String path) { File file = new File(path); if (!file.isDirectory()) { - String extName = FileHelper.getExtension(file); - if (isExtValid(extName)) { + if (isFileValid(file.getName())) { list.add(file); } return; @@ -272,8 +274,7 @@ if (f.isDirectory()) { getFilesByPath(list, f.getPath()); } else { - String extName = FileHelper.getExtension(f); - if (isExtValid(extName)) { + if (isFileValid(f.getName())) { list.add(f); } } @@ -281,16 +282,28 @@ } /** - * 鎵╁睍鏄惁鏈夋晥 + * 鏂囦欢鏄�/鍚︽湁鏁� */ - private boolean isExtValid(String extName) { - return StaticData.ALL_EXTENSION.contains(extName) && !StaticData.MAPPER_EXCLUDE_EXT.contains(extName); + private boolean isFileValid(String fileName) { + String extName = FileHelper.getExtension(fileName); + if (fileName.startsWith(StaticData.TILDE) || !StaticData.ALL_EXTENSION.contains(extName)) { + return false; + } + + fileName = fileName.toLowerCase(); + for (String ext : StaticData.MAPPER_EXCLUDE_EXT) { + if (fileName.contains(ext)) { + return false; + } + } + + return true; } /** * 鑾峰彇鏄犲皠鏂囦欢 */ - private List<MetaFileEntity> getMapperFiles(List<File> files, DirEntity dir, List<DirEntity> dirs, MetaFileEntity meta, int start) { + private List<MetaFileEntity> getMapperFiles(List<File> files, MetaFileEntity meta, int start) { List<MetaFileEntity> list = new ArrayList<>(); for (File f : files) { boolean isGdb = isGdbFile(f); @@ -302,26 +315,24 @@ String fileName = FileHelper.getFileName(f.getPath()); String extName = isOsgb ? StaticData.OSGB : FileHelper.getExtension(fileName); - if (!isExtValid(extName) || fileName.startsWith("~")) { + if (!isFileValid(f.getName())) { continue; } - String dircode = getDirByPath(f.getPath(), fileName, dir, dirs); if (isGdb) { - List<MetaFileEntity> rs = getGdbMappers(f, meta, dircode, start); + List<MetaFileEntity> rs = getGdbMappers(f, meta, start); if (null != rs && rs.size() > 0) { list.addAll(rs); } continue; } if (isOsgb) { - list.add(getOsgbMapper(f, meta, dircode, start)); + list.add(getOsgbMapper(f, meta, start)); continue; } MetaFileEntity mf = createMetaFileEntity(f, meta, fileName, extName); mf.setPath(f.getPath().substring(start)); - mf.setDircode(dircode); list.add(mf); } @@ -357,35 +368,12 @@ */ private MetaFileEntity createMetaFileEntity(MetaFileEntity meta) { MetaFileEntity mf = new MetaFileEntity(); - mf.setDircode(meta.getDircode()); mf.setDepcode(meta.getDepcode()); mf.setVerid(meta.getVerid()); mf.setCreateUser(meta.getCreateUser()); mf.setEpsgCode(meta.getEpsgCode()); return mf; - } - - /** - * 鏍规嵁鏂囦欢璺緞鑾峰彇鐩綍ID - */ - private String getDirByPath(String filePath,String fileName, DirEntity dir, List<DirEntity> dirs) { - if (0 != dir.getPid() || null == dirs || dirs.isEmpty()) { - return dir.getCode(); - } - if (StaticData.SLASH.equals(File.separator)) { - filePath = filePath.replace("/", "\\"); - } - - fileName = fileName.toLowerCase(); - filePath = filePath.toLowerCase(); - for (DirEntity entity : dirs) { - if (filePath.contains(entity.getFullName().toLowerCase() + "\\" + fileName)) { - return entity.getCode(); - } - } - - return dir.getCode(); } /** @@ -462,7 +450,7 @@ /** * 鑾峰彇GDB鏂囦欢鏄犲皠 */ - private List<MetaFileEntity> getGdbMappers(File f, MetaFileEntity meta, String dircode, int start) { + private List<MetaFileEntity> getGdbMappers(File f, MetaFileEntity meta, int start) { List<String> tabs = GdbHelper.getTabNames(f.getPath()); if (null == tabs || tabs.size() == 0) { return null; @@ -478,7 +466,6 @@ List<MetaFileEntity> list = new ArrayList<>(); for (String tab : tabs) { MetaFileEntity mf = createMetaFileEntity(meta); - mf.setDircode(dircode); mf.setEventid(StringHelper.getGuid()); mf.setName(fileName + "\\" + tab); mf.setExtName(extName); @@ -541,11 +528,10 @@ return false; } - /** * 鑾峰彇OSGB鏂囦欢鏄犲皠 */ - private MetaFileEntity getOsgbMapper(File f, MetaFileEntity meta, String dircode, int start) { + private MetaFileEntity getOsgbMapper(File f, MetaFileEntity meta, int start) { String fileName = FileHelper.getFileName(f.getPath()); List<String> files = new ArrayList<>(); @@ -555,7 +541,6 @@ double sizes = getFilesSize(files); MetaFileEntity mf = createMetaFileEntity(meta); - mf.setDircode(dircode); mf.setEventid(StringHelper.getGuid()); mf.setName(fileName); mf.setExtName(StaticData.OSGB); @@ -568,6 +553,93 @@ } /** + * 璁剧疆鐩綍缂栫爜 + */ + private void setDircode(List<MetaFileEntity> list, DirEntity dir) { + List<DirEntity> dirs = dirService.selectRecursiveById(dir.getId()); + for (MetaFileEntity mfe : list) { + String filePath = mfe.getPath().toLowerCase().replace("/", "\\"); + String fileName = mfe.getName().toLowerCase(); + + String code = findDirByPath(dirs, filePath, fileName); + if (StringHelper.isEmpty(code)) { + DirEntity baseDir = findBaseDirByPath(dirs, filePath); + if (null != baseDir) { + createDirByPath(baseDir, mfe.getPath().replace("/", "\\")); + dirs = dirService.selectRecursiveById(dir.getId()); + code = findDirByPath(dirs, filePath, fileName); + } + } + code = StringHelper.isEmpty(code) ? dir.getCode() : code; + mfe.setDircode(code); + } + } + + /** + * 鏍规嵁璺緞鏌ユ壘鐩綍 + */ + private String findDirByPath(List<DirEntity> dirs, String filePath,String fileName) { + for (DirEntity de : dirs) { + if (filePath.contains(de.getFullName().toLowerCase() + "\\" + fileName)) { + return de.getCode(); + } + } + + return null; + } + + /** + * 鏍规嵁璺緞鏌ユ壘鐖剁洰褰� + */ + private DirEntity findBaseDirByPath(List<DirEntity> dirs, String filePath) { + for (DirEntity de : dirs) { + if (StringHelper.isEmpty(de.getCode()) || de.getCode().length() != StaticData.FOUR) { + continue; + } + if (filePath.contains(de.getFullName().toLowerCase() + "\\")) { + return de; + } + } + + return null; + } + + /** + * 鏍规嵁璺緞鍒涘缓鐩綍 + */ + private void createDirByPath(DirEntity baseDir, String filePath) { + int start = filePath.toLowerCase().indexOf(baseDir.getFullName().toLowerCase() + "\\") + baseDir.getFullName().length() + 1; + int end = filePath.lastIndexOf("\\"); + if (end <= start) { + return; + } + if (0 == baseDir.getLevel()) { + baseDir.setLevel(2); + } + + HttpServletRequest req = WebHelper.getRequest(); + UserEntity ue = tokenService.getCurrentUser(req); + + String[] strs = filePath.substring(start, end).split("\\\\"); + for (String str : strs) { + DirEntity entity = dirService.selectDirByName(str, baseDir.getId()); + if (null == entity) { + entity = new DirEntity(); + entity.setPid(baseDir.getId()); + entity.setName(str); + entity.setOrderNum(dirService.selectMaxOrderNum()); + entity.setLevel(baseDir.getLevel() + 1); + entity.setCreateUser(ue.getId()); + + dirService.insert(entity); + baseDir = dirService.selectDir(entity.getId()); + } else { + baseDir = entity; + } + } + } + + /** * 鑾峰彇鍙傛暟 * * Enumeration<String> headers = req.getHeaderNames(); * Enumeration<String> attributes = req.getAttributeNames(); -- Gitblit v1.9.3