优化代码提交

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

View File

@ -54,6 +54,7 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
@ -1642,7 +1643,7 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
while ((entry = zis.getNextEntry()) != null) {
processZipEntry(zis, entry, destRoot);
}
return destRoot.toFile(); // 解压成功直接返回
return destRoot.toFile();
} catch (IllegalArgumentException | IOException e) {
LOGGER.debug("编码 {} 解压失败,尝试下一个编码", charset, e);
}
@ -1661,11 +1662,23 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
String sanitizedName = sanitizeFileName(entry.getName());
Path targetPath = buildSafePath(destRoot, sanitizedName);
// 3. 仅处理实际文件跳过空目录
if (!entry.isDirectory()) {
// 3. 处理目录包括空目录
if (entry.isDirectory()) {
createDirectory(targetPath);
}
// 4. 处理文件
else {
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 {
// 仅当文件不存在时才写入
if (!java.nio.file.Files.exists(targetPath)) {
// 按需创建父目录
// 确保父目录存在
Path parent = targetPath.getParent();
if (parent != null && !java.nio.file.Files.exists(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];
int bytesRead;
while ((bytesRead = zis.read(buffer)) != -1) {

View File

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