From 49283d068feeb2737ec16d8b59b9fb4c11ca0b9a Mon Sep 17 00:00:00 2001 From: chenxudong Date: Thu, 10 Apr 2025 16:32:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E5=92=8C?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9=E7=9A=84=E5=88=A0=E9=99=A4=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../software/backup/pojo/BackupPro.java | 5 +-- .../manage/config/LoginInterceptor.java | 21 +++++------ .../service/serviceimpl/CommonService.java | 17 +++++---- .../service/serviceimpl/EdPrjServiceImpl.java | 36 ++++++++++++------- .../common/cons/ElectromagneticConstants.java | 2 +- .../software/common/util/OfficeFileUtil.java | 22 +++++------- 6 files changed, 55 insertions(+), 48 deletions(-) diff --git a/electrmangnetic-backup/src/main/java/com/electromagnetic/industry/software/backup/pojo/BackupPro.java b/electrmangnetic-backup/src/main/java/com/electromagnetic/industry/software/backup/pojo/BackupPro.java index b4c5814..403a286 100644 --- a/electrmangnetic-backup/src/main/java/com/electromagnetic/industry/software/backup/pojo/BackupPro.java +++ b/electrmangnetic-backup/src/main/java/com/electromagnetic/industry/software/backup/pojo/BackupPro.java @@ -1,5 +1,6 @@ package com.electromagnetic.industry.software.backup.pojo; +import com.electromagnetic.industry.software.common.util.EleCommonUtil; import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @@ -16,10 +17,10 @@ public class BackupPro { private String winPrefix; public String getSaveFolder() { - return System.getProperty("os.name").toLowerCase().startsWith("win") ? winPrefix + saveFolder : saveFolder; + return EleCommonUtil.isWinOs() ? winPrefix + saveFolder : saveFolder; } public String getLogPath() { - return System.getProperty("os.name").toLowerCase().startsWith("win") ? winPrefix + logPath : logPath; + return EleCommonUtil.isWinOs() ? winPrefix + logPath : logPath; } } diff --git a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/config/LoginInterceptor.java b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/config/LoginInterceptor.java index 891e21d..e06214d 100644 --- a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/config/LoginInterceptor.java +++ b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/config/LoginInterceptor.java @@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.SystemClock; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONUtil; import com.electromagnetic.industry.software.common.annotations.UserOperation; import com.electromagnetic.industry.software.common.cons.UserConstants; @@ -47,11 +48,11 @@ public class LoginInterceptor implements HandlerInterceptor { private static String getRealIp(HttpServletRequest request) { String ipAddress = request.getHeader("X-Forwarded-For"); - if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) { + if (StrUtil.isEmpty(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) { // 回退到X-Real-IP或直接RemoteAddr ipAddress = request.getHeader("X-Real-IP"); } - if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) { + if (StrUtil.isEmpty(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getRemoteAddr(); } // 处理多级代理的情况(取第一个IP) @@ -86,8 +87,8 @@ public class LoginInterceptor implements HandlerInterceptor { } private boolean checkSysAdminOperation(HttpServletRequest request, HttpServletResponse response) { - String requestURI = request.getRequestURI(); - if (requestURI.startsWith("/data/ed/prj") && !UserThreadLocal.getAdminType().equals(AdminTypeEnum.SYSTEM.getValue())) { + String uri = request.getRequestURI(); + if (uri.startsWith("/data/ed/prj") && !UserThreadLocal.getAdminType().equals(AdminTypeEnum.SYSTEM.getValue())) { log.warn("{}没有层级操作权限,当前用户类型是{}", UserThreadLocal.getUsername(), UserThreadLocal.getAdminType()); response.setStatus(HttpServletResponse.SC_FORBIDDEN); return false; @@ -171,15 +172,11 @@ public class LoginInterceptor implements HandlerInterceptor { .setDataId("") .setParentId(""); - if (res != null) { - userAccessLog.setResponse(JSONUtil.toJsonStr(res)); - if (!res.getSuccess()) { - userAccessLog.setAccessSuccess(false); - userAccessLog.setOperationMsg(res.getErrorMessage()); - userAccessLog.setExceptionDetail(UserThreadLocal.getUser().getExceptionDetail()); - } - } else { // 返回为ResponseEntity,且状态为失败。 + userAccessLog.setResponse(JSONUtil.toJsonStr(res)); + if (!res.getSuccess()) { userAccessLog.setAccessSuccess(false); + userAccessLog.setOperationMsg(res.getErrorMessage()); + userAccessLog.setExceptionDetail(UserThreadLocal.getUser().getExceptionDetail()); } userAccessLogMapper.insert(userAccessLog); } else { diff --git a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/CommonService.java b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/CommonService.java index d843ac2..533fdfd 100644 --- a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/CommonService.java +++ b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/CommonService.java @@ -436,6 +436,7 @@ public class CommonService { // 如果文件夹下存在文件(包括文件夹和已经逻辑删除的文件),则不允许删除。后面管理员选择会有物理删除文件夹和文件的功能,此时MySQL和文件系统则会进行物理删除该文件。 EdFileInfo srcFileInfo = edFileInfoMapper.selectById(id); String srcFilePath = getFileSysPath(srcFileInfo.getFilePath(), dataOwnCode); + String uuid = IdUtil.fastSimpleUUID(); try { // 这里要分两种情况,1是删除层级目录,2是删除用户创建的文件夹 String parentId = srcFileInfo.getParentId(); @@ -480,10 +481,12 @@ public class CommonService { return ElectromagneticResultUtil.fail("-1", info); } else { // 逻辑删除文件夹 + String newFileName = srcFileInfo.getFileName() + MYSQL_FILE_PATH_SPLIT + uuid + DELETE_FLAG; edFileInfoMapper.update(new EdFileInfo(), Wrappers.lambdaUpdate() .eq(EdFileInfo::getId, id) + .set(EdFileInfo::getFileName, newFileName) .set(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code)); - fileSystemService.renameFile(srcFilePath, srcFileInfo.getFileName() + "_" + IdUtil.fastSimpleUUID() + DELETE_FLAG); + fileSystemService.renameFile(srcFilePath, newFileName); } } UserThreadLocal.setSuccessInfo(srcFileInfo.getParentId(), id, "删除目录 {} 成功", srcFileInfo.getFileName()); @@ -533,13 +536,15 @@ public class CommonService { return res; } - public void deletePrjSysDir(List paths) { - for (String path : paths) { - if (!FileUtil.exist(path)) { + public void deletePrjSysDir(Map map) { + + for (Map.Entry entry : map.entrySet()) { + String srcPath = entry.getKey(); + String newName = entry.getValue(); + if (!FileUtil.exist(srcPath)) { continue; } - String fileName = new File(path).getName(); - fileSystemService.renameFile(path, fileName + "_" + IdUtil.fastSimpleUUID() + DELETE_FLAG); + fileSystemService.renameFile(srcPath, newName); } } diff --git a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/EdPrjServiceImpl.java b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/EdPrjServiceImpl.java index e48008a..7f3880c 100644 --- a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/EdPrjServiceImpl.java +++ b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/EdPrjServiceImpl.java @@ -2,6 +2,7 @@ package com.electromagnetic.industry.software.manage.service.serviceimpl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.text.StrFormatter; import cn.hutool.core.util.IdUtil; @@ -341,27 +342,36 @@ public class EdPrjServiceImpl extends ServiceImpl try { // 将已经处于删除状态设置成逻辑删除 LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class) - .select(EdFileInfo::getId, EdFileInfo::getFilePath) + .select(EdFileInfo::getId, EdFileInfo::getFilePath, EdFileInfo::getFileName) + .eq(EdFileInfo::getDataOwn, dataOwnCode) .eq(EdFileInfo::getDataStatus, EleDataStatusEnum.WAIT_DELETED.code) .likeRight(EdFileInfo::getFilePath, prjId); - List paths = this.baseMapper.selectList(queryWrapper).stream().map(EdFileInfo::getFilePath).toList(); - List fileSysPaths = new ArrayList<>(); - paths.forEach(path -> { - String fileSysPath = commonService.getFileSysPath(path, dataOwnCode); - fileSysPaths.add(fileSysPath); - }); + List edFileInfos = this.baseMapper.selectList(queryWrapper); + Map map = new HashMap<>(); + Map idNameMap = new HashMap<>(); - this.update(new EdFileInfo(), Wrappers.lambdaUpdate(EdFileInfo.class) - .eq(EdFileInfo::getDataStatus, EleDataStatusEnum.WAIT_DELETED.code) - .set(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code) - .likeRight(EdFileInfo::getFilePath, prjId)); - // 其余置为发布状态 + for (EdFileInfo edFileInfo : edFileInfos) { + String fileSysPath = commonService.getFileSysPath(edFileInfo.getFilePath(), dataOwnCode); + String uuid = IdUtil.fastSimpleUUID(); + String newName = edFileInfo.getFileName() + MYSQL_FILE_PATH_SPLIT + uuid + DELETE_FLAG; + map.put(fileSysPath, newName); + idNameMap.put(edFileInfo.getId(), newName); + } + for (Map.Entry entry : idNameMap.entrySet()) { + String id = entry.getKey(); + String newName = entry.getValue(); + this.update(new EdFileInfo(), Wrappers.lambdaUpdate(EdFileInfo.class) + .eq(EdFileInfo::getId, id) + .set(EdFileInfo::getFileName, newName) + .set(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code)); + } + // 其余置为发布状态 LambdaUpdateWrapper updateWrapper = Wrappers.lambdaUpdate(EdFileInfo.class) .set(EdFileInfo::getDataStatus, EleDataStatusEnum.PUBLISHED.code) .eq(EdFileInfo::getDataStatus, EleDataStatusEnum.NOT_PUBLISHED.code) .likeRight(EdFileInfo::getFilePath, prjId); this.update(new EdFileInfo(), updateWrapper); - commonService.deletePrjSysDir(fileSysPaths); + commonService.deletePrjSysDir(map); UserThreadLocal.setSuccessInfo("", prjId, "项目 {} 发布成功", fileInfo.getFileName()); return ElectromagneticResultUtil.success(true); } catch (Exception e) { diff --git a/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/cons/ElectromagneticConstants.java b/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/cons/ElectromagneticConstants.java index 1dc6ca0..dafc4f4 100644 --- a/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/cons/ElectromagneticConstants.java +++ b/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/cons/ElectromagneticConstants.java @@ -16,5 +16,5 @@ public interface ElectromagneticConstants { String FILE_SEC_PASSWD = "adknfhkj87654knd"; - String DELETE_FLAG = "deleted"; + String DELETE_FLAG = "_deleted"; } diff --git a/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/util/OfficeFileUtil.java b/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/util/OfficeFileUtil.java index 3a88038..6ee84a8 100644 --- a/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/util/OfficeFileUtil.java +++ b/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/util/OfficeFileUtil.java @@ -202,20 +202,14 @@ public class OfficeFileUtil { return ""; } - switch (cell.getCellType()) { - case STRING: - return cell.getStringCellValue(); - case NUMERIC: - return String.valueOf(cell.getNumericCellValue()); - case BOOLEAN: - return String.valueOf(cell.getBooleanCellValue()); - case ERROR: - return String.valueOf(cell.getErrorCellValue()); - case FORMULA: - return String.valueOf(cell.getCellFormula()); - default: - return ""; - } + return switch (cell.getCellType()) { + case STRING -> cell.getStringCellValue(); + case NUMERIC -> String.valueOf(cell.getNumericCellValue()); + case BOOLEAN -> String.valueOf(cell.getBooleanCellValue()); + case ERROR -> String.valueOf(cell.getErrorCellValue()); + case FORMULA -> String.valueOf(cell.getCellFormula()); + default -> ""; + }; } public static String parsePptAllText(String filePath) throws IOException {