优化代码提交

This commit is contained in:
lilin 2025-07-07 11:09:57 +08:00
parent 2e0ab0cef1
commit a7ab178032
3 changed files with 68 additions and 36 deletions

View File

@ -3089,6 +3089,9 @@ public class TsFilesServiceImpl extends ServiceImpl<TsFilesMapper, TsFiles> impl
List<TsFiles> filteredRecords = tsFilesPage.getRecords().stream() List<TsFiles> filteredRecords = tsFilesPage.getRecords().stream()
.filter(tsFile -> { .filter(tsFile -> {
try { try {
if("FOLDER".equals(tsFile.getIsFile())){
return false;
}
// 计算本地文件MD5 // 计算本地文件MD5
File localFile = new File(filePathConfig.getValue() + tsFile.getWorkPath(), tsFile.getFileName()); File localFile = new File(filePathConfig.getValue() + tsFile.getWorkPath(), tsFile.getFileName());
String localMD5 = calculateMD5Data(new FileInputStream(localFile)); String localMD5 = calculateMD5Data(new FileInputStream(localFile));
@ -3173,6 +3176,9 @@ public class TsFilesServiceImpl extends ServiceImpl<TsFilesMapper, TsFiles> impl
List<TsFiles> filteredRecords = records.stream() List<TsFiles> filteredRecords = records.stream()
.filter(tsFile -> { .filter(tsFile -> {
try { try {
if("FOLDER".equals(tsFile.getIsFile())){
return false;
}
// 计算本地文件MD5 // 计算本地文件MD5
File localFile = new File(filePathConfig.getValue() + tsFile.getWorkPath(), tsFile.getFileName()); File localFile = new File(filePathConfig.getValue() + tsFile.getWorkPath(), tsFile.getFileName());
String localMD5 = calculateMD5Data(new FileInputStream(localFile)); String localMD5 = calculateMD5Data(new FileInputStream(localFile));
@ -5016,18 +5022,19 @@ public class TsFilesServiceImpl extends ServiceImpl<TsFilesMapper, TsFiles> impl
TsTask tsTask = tsTaskMapper.selectById(taskId); TsTask tsTask = tsTaskMapper.selectById(taskId);
TableNameContextHolder.setTaskCode(tsTask.getTaskCode()); TableNameContextHolder.setTaskCode(tsTask.getTaskCode());
StorageSource storageSourceLocal = getStorageConfig(tsTask.getLocalStorageId());
TsFiles tsFiles = tsFilesMapper.selectById(id); TsFiles tsFiles = tsFilesMapper.selectById(id);
String fileNameData = tsFiles.getFileName(); String fileNameData = tsFiles.getFileName();
String path = ""; String path = "";
StorageSource storageSourceLocal = null;
if ("local".equals(type)) { if ("local".equals(type)) {
storageSourceLocal = getStorageConfig(tsTask.getLocalStorageId());
String workPath = tsFiles.getWorkPath(); String workPath = tsFiles.getWorkPath();
path = workPath + fileNameData; path = workPath + fileNameData;
}else { }else {
storageSourceLocal = getStorageConfig(tsTask.getBackupStorageId());
String backupPath = tsFiles.getBackupPath(); String backupPath = tsFiles.getBackupPath();
path = backupPath + fileNameData; path = backupPath + fileNameData;
} }
//准备获取文件的信息 //准备获取文件的信息
AbstractBaseFileService<?> fileService = storageSourceContext.getByStorageKey(storageSourceLocal.getKey()); AbstractBaseFileService<?> fileService = storageSourceContext.getByStorageKey(storageSourceLocal.getKey());
fileItemResult = fileService.getFileItem(path); fileItemResult = fileService.getFileItem(path);

View File

@ -54,6 +54,7 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp; import java.sql.Timestamp;
@ -1642,7 +1643,7 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
while ((entry = zis.getNextEntry()) != null) { while ((entry = zis.getNextEntry()) != null) {
processZipEntry(zis, entry, destRoot); processZipEntry(zis, entry, destRoot);
} }
return destRoot.toFile(); // 解压成功直接返回 return destRoot.toFile();
} catch (IllegalArgumentException | IOException e) { } catch (IllegalArgumentException | IOException e) {
LOGGER.debug("编码 {} 解压失败,尝试下一个编码", charset, e); LOGGER.debug("编码 {} 解压失败,尝试下一个编码", charset, e);
} }
@ -1661,11 +1662,23 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
String sanitizedName = sanitizeFileName(entry.getName()); String sanitizedName = sanitizeFileName(entry.getName());
Path targetPath = buildSafePath(destRoot, sanitizedName); Path targetPath = buildSafePath(destRoot, sanitizedName);
// 3. 仅处理实际文件跳过空目录 // 3. 处理目录包括空目录
if (!entry.isDirectory()) { if (entry.isDirectory()) {
createDirectory(targetPath);
}
// 4. 处理文件
else {
writeFileContent(zis, targetPath); writeFileContent(zis, targetPath);
} }
} }
// 新增方法专门处理目录创建
private void createDirectory(Path dirPath) throws IOException {
// 确保路径安全后创建目录
if (!java.nio.file.Files.exists(dirPath)) {
java.nio.file.Files.createDirectories(dirPath);
LOGGER.debug("创建空目录:{}", dirPath);
}
}
/** /**
* 判断是否需要跳过条目 * 判断是否需要跳过条目
@ -1710,16 +1723,17 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
* 写入文件内容仅在需要时创建目录 * 写入文件内容仅在需要时创建目录
*/ */
private void writeFileContent(ZipInputStream zis, Path targetPath) throws IOException { private void writeFileContent(ZipInputStream zis, Path targetPath) throws IOException {
// 仅当文件不存在时才写入 // 确保父目录存在
if (!java.nio.file.Files.exists(targetPath)) {
// 按需创建父目录
Path parent = targetPath.getParent(); Path parent = targetPath.getParent();
if (parent != null && !java.nio.file.Files.exists(parent)) { if (parent != null && !java.nio.file.Files.exists(parent)) {
java.nio.file.Files.createDirectories(parent); java.nio.file.Files.createDirectories(parent);
} }
// 使用缓冲流提升性能 // 仅当文件不存在时才写入
try (BufferedOutputStream os = new BufferedOutputStream(java.nio.file.Files.newOutputStream(targetPath))) { if (!java.nio.file.Files.exists(targetPath)) {
try (BufferedOutputStream os = new BufferedOutputStream(
java.nio.file.Files.newOutputStream(targetPath, StandardOpenOption.CREATE_NEW))
) {
byte[] buffer = new byte[8192]; byte[] buffer = new byte[8192];
int bytesRead; int bytesRead;
while ((bytesRead = zis.read(buffer)) != -1) { while ((bytesRead = zis.read(buffer)) != -1) {

View File

@ -211,9 +211,8 @@ public class StorageSourceConvertImpl implements StorageSourceConvert {
return resultPage; return resultPage;
} }
// 计算本地路径的空间使用 // 计算本地路径的空间剩余
public static String calculateLocalStorageUsage(String path) { public static String calculateLocalStorageUsage(String path) {
try { try {
Path target = Paths.get(path); Path target = Paths.get(path);
@ -232,31 +231,43 @@ public class StorageSourceConvertImpl implements StorageSourceConvert {
return "0%"; // 特殊文件系统处理 return "0%"; // 特殊文件系统处理
} }
double usagePercentage = (double) (totalSpace - usableSpace) / totalSpace * 100; double remainingPercentage = (double) usableSpace / totalSpace * 100; // 计算剩余空间百分比
DecimalFormat df = new DecimalFormat("#.##"); DecimalFormat df = new DecimalFormat("#.##");
return df.format(usagePercentage) + "%"; return df.format(remainingPercentage) + "%";
} catch (Exception e) { } catch (Exception e) {
throw new IllegalArgumentException("无法获取磁盘空间: " + e.getMessage()); throw new IllegalArgumentException("无法获取磁盘空间: " + e.getMessage());
} }
// File file = new File(path); }
// if (!file.exists()) { // 计算本地路径的空间使用率
// throw new IllegalArgumentException("路径不存在: " + path); // public static String calculateLocalStorageUsage(String path) {
//
// try {
// Path target = Paths.get(path);
//
// // 处理符号链接获取实际挂载点
// if (Files.isSymbolicLink(target)) {
// target = Files.readSymbolicLink(target);
// } // }
// //
// // 获取磁盘空间信息 // // 获取文件存储信息
// long totalSpace = file.getTotalSpace(); // FileStore store = Files.getFileStore(target);
// long freeSpace = file.getFreeSpace();
// // 计算使用率
// long usedSpace = totalSpace - freeSpace;
// double usagePercentage = (double) usedSpace / totalSpace * 100;
// //
// // 格式化输出 // long totalSpace = store.getTotalSpace();
// long usableSpace = store.getUsableSpace();
//
// if (totalSpace <= 0) {
// return "0%"; // 特殊文件系统处理
// }
//
// double usagePercentage = (double) (totalSpace - usableSpace) / totalSpace * 100;
// DecimalFormat df = new DecimalFormat("#.##"); // DecimalFormat df = new DecimalFormat("#.##");
//
// return df.format(usagePercentage) + "%"; // return df.format(usagePercentage) + "%";
//
} // } catch (Exception e) {
// throw new IllegalArgumentException("无法获取磁盘空间: " + e.getMessage());
// }
// }