优化文件和文件夹的删除逻辑

This commit is contained in:
chenxudong 2025-04-10 16:32:02 +08:00
parent acd011e69f
commit 49283d068f
6 changed files with 55 additions and 48 deletions

View File

@ -1,5 +1,6 @@
package com.electromagnetic.industry.software.backup.pojo; package com.electromagnetic.industry.software.backup.pojo;
import com.electromagnetic.industry.software.common.util.EleCommonUtil;
import lombok.Setter; import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -16,10 +17,10 @@ public class BackupPro {
private String winPrefix; private String winPrefix;
public String getSaveFolder() { public String getSaveFolder() {
return System.getProperty("os.name").toLowerCase().startsWith("win") ? winPrefix + saveFolder : saveFolder; return EleCommonUtil.isWinOs() ? winPrefix + saveFolder : saveFolder;
} }
public String getLogPath() { public String getLogPath() {
return System.getProperty("os.name").toLowerCase().startsWith("win") ? winPrefix + logPath : logPath; return EleCommonUtil.isWinOs() ? winPrefix + logPath : logPath;
} }
} }

View File

@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.SystemClock; import cn.hutool.core.date.SystemClock;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.electromagnetic.industry.software.common.annotations.UserOperation; import com.electromagnetic.industry.software.common.annotations.UserOperation;
import com.electromagnetic.industry.software.common.cons.UserConstants; import com.electromagnetic.industry.software.common.cons.UserConstants;
@ -47,11 +48,11 @@ public class LoginInterceptor implements HandlerInterceptor {
private static String getRealIp(HttpServletRequest request) { private static String getRealIp(HttpServletRequest request) {
String ipAddress = request.getHeader("X-Forwarded-For"); 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 // 回退到X-Real-IP或直接RemoteAddr
ipAddress = request.getHeader("X-Real-IP"); ipAddress = request.getHeader("X-Real-IP");
} }
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) { if (StrUtil.isEmpty(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr(); ipAddress = request.getRemoteAddr();
} }
// 处理多级代理的情况取第一个IP // 处理多级代理的情况取第一个IP
@ -86,8 +87,8 @@ public class LoginInterceptor implements HandlerInterceptor {
} }
private boolean checkSysAdminOperation(HttpServletRequest request, HttpServletResponse response) { private boolean checkSysAdminOperation(HttpServletRequest request, HttpServletResponse response) {
String requestURI = request.getRequestURI(); String uri = request.getRequestURI();
if (requestURI.startsWith("/data/ed/prj") && !UserThreadLocal.getAdminType().equals(AdminTypeEnum.SYSTEM.getValue())) { if (uri.startsWith("/data/ed/prj") && !UserThreadLocal.getAdminType().equals(AdminTypeEnum.SYSTEM.getValue())) {
log.warn("{}没有层级操作权限,当前用户类型是{}", UserThreadLocal.getUsername(), UserThreadLocal.getAdminType()); log.warn("{}没有层级操作权限,当前用户类型是{}", UserThreadLocal.getUsername(), UserThreadLocal.getAdminType());
response.setStatus(HttpServletResponse.SC_FORBIDDEN); response.setStatus(HttpServletResponse.SC_FORBIDDEN);
return false; return false;
@ -171,16 +172,12 @@ public class LoginInterceptor implements HandlerInterceptor {
.setDataId("") .setDataId("")
.setParentId(""); .setParentId("");
if (res != null) {
userAccessLog.setResponse(JSONUtil.toJsonStr(res)); userAccessLog.setResponse(JSONUtil.toJsonStr(res));
if (!res.getSuccess()) { if (!res.getSuccess()) {
userAccessLog.setAccessSuccess(false); userAccessLog.setAccessSuccess(false);
userAccessLog.setOperationMsg(res.getErrorMessage()); userAccessLog.setOperationMsg(res.getErrorMessage());
userAccessLog.setExceptionDetail(UserThreadLocal.getUser().getExceptionDetail()); userAccessLog.setExceptionDetail(UserThreadLocal.getUser().getExceptionDetail());
} }
} else { // 返回为ResponseEntity且状态为失败
userAccessLog.setAccessSuccess(false);
}
userAccessLogMapper.insert(userAccessLog); userAccessLogMapper.insert(userAccessLog);
} else { } else {
for (AccessSuccessInfo accessSuccessInfo : successInfoList) { for (AccessSuccessInfo accessSuccessInfo : successInfoList) {

View File

@ -436,6 +436,7 @@ public class CommonService {
// 如果文件夹下存在文件包括文件夹和已经逻辑删除的文件则不允许删除后面管理员选择会有物理删除文件夹和文件的功能此时MySQL和文件系统则会进行物理删除该文件 // 如果文件夹下存在文件包括文件夹和已经逻辑删除的文件则不允许删除后面管理员选择会有物理删除文件夹和文件的功能此时MySQL和文件系统则会进行物理删除该文件
EdFileInfo srcFileInfo = edFileInfoMapper.selectById(id); EdFileInfo srcFileInfo = edFileInfoMapper.selectById(id);
String srcFilePath = getFileSysPath(srcFileInfo.getFilePath(), dataOwnCode); String srcFilePath = getFileSysPath(srcFileInfo.getFilePath(), dataOwnCode);
String uuid = IdUtil.fastSimpleUUID();
try { try {
// 这里要分两种情况1是删除层级目录2是删除用户创建的文件夹 // 这里要分两种情况1是删除层级目录2是删除用户创建的文件夹
String parentId = srcFileInfo.getParentId(); String parentId = srcFileInfo.getParentId();
@ -480,10 +481,12 @@ public class CommonService {
return ElectromagneticResultUtil.fail("-1", info); return ElectromagneticResultUtil.fail("-1", info);
} else { } else {
// 逻辑删除文件夹 // 逻辑删除文件夹
String newFileName = srcFileInfo.getFileName() + MYSQL_FILE_PATH_SPLIT + uuid + DELETE_FLAG;
edFileInfoMapper.update(new EdFileInfo(), Wrappers.<EdFileInfo>lambdaUpdate() edFileInfoMapper.update(new EdFileInfo(), Wrappers.<EdFileInfo>lambdaUpdate()
.eq(EdFileInfo::getId, id) .eq(EdFileInfo::getId, id)
.set(EdFileInfo::getFileName, newFileName)
.set(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code)); .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()); UserThreadLocal.setSuccessInfo(srcFileInfo.getParentId(), id, "删除目录 {} 成功", srcFileInfo.getFileName());
@ -533,13 +536,15 @@ public class CommonService {
return res; return res;
} }
public void deletePrjSysDir(List<String> paths) { public void deletePrjSysDir(Map<String, String> map) {
for (String path : paths) {
if (!FileUtil.exist(path)) { for (Map.Entry<String, String> entry : map.entrySet()) {
String srcPath = entry.getKey();
String newName = entry.getValue();
if (!FileUtil.exist(srcPath)) {
continue; continue;
} }
String fileName = new File(path).getName(); fileSystemService.renameFile(srcPath, newName);
fileSystemService.renameFile(path, fileName + "_" + IdUtil.fastSimpleUUID() + DELETE_FLAG);
} }
} }

View File

@ -2,6 +2,7 @@ package com.electromagnetic.industry.software.manage.service.serviceimpl;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.StrFormatter; import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
@ -341,27 +342,36 @@ public class EdPrjServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo>
try { try {
// 将已经处于删除状态设置成逻辑删除 // 将已经处于删除状态设置成逻辑删除
LambdaQueryWrapper<EdFileInfo> queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class) LambdaQueryWrapper<EdFileInfo> 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) .eq(EdFileInfo::getDataStatus, EleDataStatusEnum.WAIT_DELETED.code)
.likeRight(EdFileInfo::getFilePath, prjId); .likeRight(EdFileInfo::getFilePath, prjId);
List<String> paths = this.baseMapper.selectList(queryWrapper).stream().map(EdFileInfo::getFilePath).toList(); List<EdFileInfo> edFileInfos = this.baseMapper.selectList(queryWrapper);
List<String> fileSysPaths = new ArrayList<>(); Map<String, String> map = new HashMap<>();
paths.forEach(path -> { Map<String, String> idNameMap = new HashMap<>();
String fileSysPath = commonService.getFileSysPath(path, dataOwnCode);
fileSysPaths.add(fileSysPath);
});
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<String, String> entry : idNameMap.entrySet()) {
String id = entry.getKey();
String newName = entry.getValue();
this.update(new EdFileInfo(), Wrappers.lambdaUpdate(EdFileInfo.class) this.update(new EdFileInfo(), Wrappers.lambdaUpdate(EdFileInfo.class)
.eq(EdFileInfo::getDataStatus, EleDataStatusEnum.WAIT_DELETED.code) .eq(EdFileInfo::getId, id)
.set(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code) .set(EdFileInfo::getFileName, newName)
.likeRight(EdFileInfo::getFilePath, prjId)); .set(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code));
}
// 其余置为发布状态 // 其余置为发布状态
LambdaUpdateWrapper<EdFileInfo> updateWrapper = Wrappers.lambdaUpdate(EdFileInfo.class) LambdaUpdateWrapper<EdFileInfo> updateWrapper = Wrappers.lambdaUpdate(EdFileInfo.class)
.set(EdFileInfo::getDataStatus, EleDataStatusEnum.PUBLISHED.code) .set(EdFileInfo::getDataStatus, EleDataStatusEnum.PUBLISHED.code)
.eq(EdFileInfo::getDataStatus, EleDataStatusEnum.NOT_PUBLISHED.code) .eq(EdFileInfo::getDataStatus, EleDataStatusEnum.NOT_PUBLISHED.code)
.likeRight(EdFileInfo::getFilePath, prjId); .likeRight(EdFileInfo::getFilePath, prjId);
this.update(new EdFileInfo(), updateWrapper); this.update(new EdFileInfo(), updateWrapper);
commonService.deletePrjSysDir(fileSysPaths); commonService.deletePrjSysDir(map);
UserThreadLocal.setSuccessInfo("", prjId, "项目 {} 发布成功", fileInfo.getFileName()); UserThreadLocal.setSuccessInfo("", prjId, "项目 {} 发布成功", fileInfo.getFileName());
return ElectromagneticResultUtil.success(true); return ElectromagneticResultUtil.success(true);
} catch (Exception e) { } catch (Exception e) {

View File

@ -16,5 +16,5 @@ public interface ElectromagneticConstants {
String FILE_SEC_PASSWD = "adknfhkj87654knd"; String FILE_SEC_PASSWD = "adknfhkj87654knd";
String DELETE_FLAG = "deleted"; String DELETE_FLAG = "_deleted";
} }

View File

@ -202,20 +202,14 @@ public class OfficeFileUtil {
return ""; return "";
} }
switch (cell.getCellType()) { return switch (cell.getCellType()) {
case STRING: case STRING -> cell.getStringCellValue();
return cell.getStringCellValue(); case NUMERIC -> String.valueOf(cell.getNumericCellValue());
case NUMERIC: case BOOLEAN -> String.valueOf(cell.getBooleanCellValue());
return String.valueOf(cell.getNumericCellValue()); case ERROR -> String.valueOf(cell.getErrorCellValue());
case BOOLEAN: case FORMULA -> String.valueOf(cell.getCellFormula());
return String.valueOf(cell.getBooleanCellValue()); default -> "";
case ERROR: };
return String.valueOf(cell.getErrorCellValue());
case FORMULA:
return String.valueOf(cell.getCellFormula());
default:
return "";
}
} }
public static String parsePptAllText(String filePath) throws IOException { public static String parsePptAllText(String filePath) throws IOException {