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

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;
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;
}
}

View File

@ -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,16 +172,12 @@ 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.setAccessSuccess(false);
}
userAccessLogMapper.insert(userAccessLog);
} else {
for (AccessSuccessInfo accessSuccessInfo : successInfoList) {

View File

@ -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.<EdFileInfo>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<String> paths) {
for (String path : paths) {
if (!FileUtil.exist(path)) {
public void deletePrjSysDir(Map<String, String> map) {
for (Map.Entry<String, String> 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);
}
}

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.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<EdFileInfoMapper, EdFileInfo>
try {
// 将已经处于删除状态设置成逻辑删除
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)
.likeRight(EdFileInfo::getFilePath, prjId);
List<String> paths = this.baseMapper.selectList(queryWrapper).stream().map(EdFileInfo::getFilePath).toList();
List<String> fileSysPaths = new ArrayList<>();
paths.forEach(path -> {
String fileSysPath = commonService.getFileSysPath(path, dataOwnCode);
fileSysPaths.add(fileSysPath);
});
List<EdFileInfo> edFileInfos = this.baseMapper.selectList(queryWrapper);
Map<String, String> map = new HashMap<>();
Map<String, String> idNameMap = new HashMap<>();
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)
.eq(EdFileInfo::getDataStatus, EleDataStatusEnum.WAIT_DELETED.code)
.set(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code)
.likeRight(EdFileInfo::getFilePath, prjId));
.eq(EdFileInfo::getId, id)
.set(EdFileInfo::getFileName, newName)
.set(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code));
}
// 其余置为发布状态
LambdaUpdateWrapper<EdFileInfo> 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) {

View File

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

View File

@ -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 {