fix(api): fix potential memory leaks in file processing
This commit is contained in:
@@ -123,8 +123,8 @@ public class BookFileEntity {
|
||||
return getFullFilePath();
|
||||
}
|
||||
Path folderPath = getFullFilePath();
|
||||
try {
|
||||
return Files.list(folderPath)
|
||||
try (var files = Files.list(folderPath)) {
|
||||
return files
|
||||
.filter(java.nio.file.Files::isRegularFile)
|
||||
.filter(p -> {
|
||||
String name = p.getFileName().toString().toLowerCase();
|
||||
|
||||
@@ -193,22 +193,22 @@ public class BookDownloadService {
|
||||
// Handle folder-based audiobooks - add all files from the folder
|
||||
if (bookFile.isFolderBased() && Files.isDirectory(filePath)) {
|
||||
String folderPrefix = bookFile.getFileName() + "/";
|
||||
List<Path> audioFiles = Files.list(filePath)
|
||||
.filter(Files::isRegularFile)
|
||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||
.toList();
|
||||
try (var audioFiles = Files.list(filePath)) {
|
||||
for (Path audioFile : audioFiles
|
||||
.filter(Files::isRegularFile)
|
||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||
.toList()) {
|
||||
String entryName = folderPrefix + audioFile.getFileName().toString();
|
||||
ZipEntry zipEntry = new ZipEntry(entryName);
|
||||
zipEntry.setSize(Files.size(audioFile));
|
||||
zos.putNextEntry(zipEntry);
|
||||
|
||||
for (Path audioFile : audioFiles) {
|
||||
String entryName = folderPrefix + audioFile.getFileName().toString();
|
||||
ZipEntry zipEntry = new ZipEntry(entryName);
|
||||
zipEntry.setSize(Files.size(audioFile));
|
||||
zos.putNextEntry(zipEntry);
|
||||
try (InputStream fis = Files.newInputStream(audioFile)) {
|
||||
fis.transferTo(zos);
|
||||
}
|
||||
|
||||
try (InputStream fis = Files.newInputStream(audioFile)) {
|
||||
fis.transferTo(zos);
|
||||
zos.closeEntry();
|
||||
}
|
||||
|
||||
zos.closeEntry();
|
||||
}
|
||||
} else {
|
||||
// Regular file
|
||||
@@ -322,16 +322,16 @@ public class BookDownloadService {
|
||||
|
||||
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
|
||||
// Get all files in the folder, sorted by name
|
||||
List<Path> files = Files.list(folderPath)
|
||||
.filter(Files::isRegularFile)
|
||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||
.toList();
|
||||
|
||||
for (Path audioFile : files) {
|
||||
ZipEntry entry = new ZipEntry(audioFile.getFileName().toString());
|
||||
zos.putNextEntry(entry);
|
||||
Files.copy(audioFile, zos);
|
||||
zos.closeEntry();
|
||||
try (var files = Files.list(folderPath)) {
|
||||
for (Path audioFile : files
|
||||
.filter(Files::isRegularFile)
|
||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||
.toList()) {
|
||||
ZipEntry entry = new ZipEntry(audioFile.getFileName().toString());
|
||||
zos.putNextEntry(entry);
|
||||
Files.copy(audioFile, zos);
|
||||
zos.closeEntry();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -131,16 +131,16 @@ public class AdditionalFileService {
|
||||
|
||||
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
|
||||
// Get all files in the folder, sorted by name
|
||||
List<Path> files = Files.list(folderPath)
|
||||
.filter(Files::isRegularFile)
|
||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||
.toList();
|
||||
|
||||
for (Path audioFile : files) {
|
||||
ZipEntry entry = new ZipEntry(audioFile.getFileName().toString());
|
||||
zos.putNextEntry(entry);
|
||||
Files.copy(audioFile, zos);
|
||||
zos.closeEntry();
|
||||
try (var files = Files.list(folderPath)) {
|
||||
for (Path audioFile : files
|
||||
.filter(Files::isRegularFile)
|
||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||
.toList()) {
|
||||
ZipEntry entry = new ZipEntry(audioFile.getFileName().toString());
|
||||
zos.putNextEntry(entry);
|
||||
Files.copy(audioFile, zos);
|
||||
zos.closeEntry();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -52,11 +52,14 @@ public class FileFingerprint {
|
||||
throw new RuntimeException("Folder does not exist: " + folderPath);
|
||||
}
|
||||
|
||||
List<Path> audioFiles = Files.list(folderPath)
|
||||
.filter(Files::isRegularFile)
|
||||
.filter(p -> isAudioFile(p.getFileName().toString()))
|
||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||
.toList();
|
||||
List<Path> audioFiles;
|
||||
try (var files = Files.list(folderPath)) {
|
||||
audioFiles = files
|
||||
.filter(Files::isRegularFile)
|
||||
.filter(p -> isAudioFile(p.getFileName().toString()))
|
||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
if (audioFiles.isEmpty()) {
|
||||
throw new RuntimeException("No audio files in folder: " + folderPath);
|
||||
|
||||
@@ -86,16 +86,19 @@ public class FileUtils {
|
||||
log.warn("Folder does not exist or is not a directory: {}", folderPath.toAbsolutePath());
|
||||
return null;
|
||||
}
|
||||
long totalBytes = Files.walk(folderPath)
|
||||
.filter(Files::isRegularFile)
|
||||
.mapToLong(p -> {
|
||||
try {
|
||||
return Files.size(p);
|
||||
} catch (IOException e) {
|
||||
return 0L;
|
||||
}
|
||||
})
|
||||
.sum();
|
||||
long totalBytes;
|
||||
try (var files = Files.walk(folderPath)) {
|
||||
totalBytes = files
|
||||
.filter(Files::isRegularFile)
|
||||
.mapToLong(p -> {
|
||||
try {
|
||||
return Files.size(p);
|
||||
} catch (IOException e) {
|
||||
return 0L;
|
||||
}
|
||||
})
|
||||
.sum();
|
||||
}
|
||||
return totalBytes / 1024;
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to get folder size for path [{}]: {}", folderPath, e.getMessage(), e);
|
||||
@@ -111,11 +114,13 @@ public class FileUtils {
|
||||
if (!Files.exists(folderPath) || !Files.isDirectory(folderPath)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Files.list(folderPath)
|
||||
.filter(Files::isRegularFile)
|
||||
.filter(p -> isAudioFile(p.getFileName().toString()))
|
||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||
.findFirst();
|
||||
try (var files = Files.list(folderPath)) {
|
||||
return files
|
||||
.filter(Files::isRegularFile)
|
||||
.filter(p -> isAudioFile(p.getFileName().toString()))
|
||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||
.findFirst();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to list folder [{}]: {}", folderPath, e.getMessage(), e);
|
||||
return Optional.empty();
|
||||
@@ -166,11 +171,13 @@ public class FileUtils {
|
||||
if (!Files.exists(folderPath) || !Files.isDirectory(folderPath)) {
|
||||
return List.of();
|
||||
}
|
||||
return Files.list(folderPath)
|
||||
.filter(Files::isRegularFile)
|
||||
.filter(p -> isAudioFile(p.getFileName().toString()))
|
||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||
.toList();
|
||||
try (var files = Files.list(folderPath)) {
|
||||
return files
|
||||
.filter(Files::isRegularFile)
|
||||
.filter(p -> isAudioFile(p.getFileName().toString()))
|
||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||
.toList();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to list audio files in folder [{}]: {}", folderPath, e.getMessage(), e);
|
||||
return List.of();
|
||||
|
||||
Reference in New Issue
Block a user