fix(api): fix potential memory leaks in file processing

This commit is contained in:
Balázs Szücs
2026-03-20 20:13:58 -05:00
committed by James Cox
parent 55c0ac0899
commit 031e8ae257
5 changed files with 70 additions and 60 deletions
@@ -123,8 +123,8 @@ public class BookFileEntity {
return getFullFilePath(); return getFullFilePath();
} }
Path folderPath = getFullFilePath(); Path folderPath = getFullFilePath();
try { try (var files = Files.list(folderPath)) {
return Files.list(folderPath) return files
.filter(java.nio.file.Files::isRegularFile) .filter(java.nio.file.Files::isRegularFile)
.filter(p -> { .filter(p -> {
String name = p.getFileName().toString().toLowerCase(); String name = p.getFileName().toString().toLowerCase();
@@ -193,12 +193,11 @@ public class BookDownloadService {
// Handle folder-based audiobooks - add all files from the folder // Handle folder-based audiobooks - add all files from the folder
if (bookFile.isFolderBased() && Files.isDirectory(filePath)) { if (bookFile.isFolderBased() && Files.isDirectory(filePath)) {
String folderPrefix = bookFile.getFileName() + "/"; String folderPrefix = bookFile.getFileName() + "/";
List<Path> audioFiles = Files.list(filePath) try (var audioFiles = Files.list(filePath)) {
for (Path audioFile : audioFiles
.filter(Files::isRegularFile) .filter(Files::isRegularFile)
.sorted(Comparator.comparing(p -> p.getFileName().toString())) .sorted(Comparator.comparing(p -> p.getFileName().toString()))
.toList(); .toList()) {
for (Path audioFile : audioFiles) {
String entryName = folderPrefix + audioFile.getFileName().toString(); String entryName = folderPrefix + audioFile.getFileName().toString();
ZipEntry zipEntry = new ZipEntry(entryName); ZipEntry zipEntry = new ZipEntry(entryName);
zipEntry.setSize(Files.size(audioFile)); zipEntry.setSize(Files.size(audioFile));
@@ -210,6 +209,7 @@ public class BookDownloadService {
zos.closeEntry(); zos.closeEntry();
} }
}
} else { } else {
// Regular file // Regular file
ZipEntry zipEntry = new ZipEntry(bookFile.getFileName()); ZipEntry zipEntry = new ZipEntry(bookFile.getFileName());
@@ -322,18 +322,18 @@ public class BookDownloadService {
try (ZipOutputStream zos = new ZipOutputStream(baos)) { try (ZipOutputStream zos = new ZipOutputStream(baos)) {
// Get all files in the folder, sorted by name // Get all files in the folder, sorted by name
List<Path> files = Files.list(folderPath) try (var files = Files.list(folderPath)) {
for (Path audioFile : files
.filter(Files::isRegularFile) .filter(Files::isRegularFile)
.sorted(Comparator.comparing(p -> p.getFileName().toString())) .sorted(Comparator.comparing(p -> p.getFileName().toString()))
.toList(); .toList()) {
for (Path audioFile : files) {
ZipEntry entry = new ZipEntry(audioFile.getFileName().toString()); ZipEntry entry = new ZipEntry(audioFile.getFileName().toString());
zos.putNextEntry(entry); zos.putNextEntry(entry);
Files.copy(audioFile, zos); Files.copy(audioFile, zos);
zos.closeEntry(); zos.closeEntry();
} }
} }
}
byte[] zipBytes = baos.toByteArray(); byte[] zipBytes = baos.toByteArray();
Resource resource = new org.springframework.core.io.ByteArrayResource(zipBytes); Resource resource = new org.springframework.core.io.ByteArrayResource(zipBytes);
@@ -131,18 +131,18 @@ public class AdditionalFileService {
try (ZipOutputStream zos = new ZipOutputStream(baos)) { try (ZipOutputStream zos = new ZipOutputStream(baos)) {
// Get all files in the folder, sorted by name // Get all files in the folder, sorted by name
List<Path> files = Files.list(folderPath) try (var files = Files.list(folderPath)) {
for (Path audioFile : files
.filter(Files::isRegularFile) .filter(Files::isRegularFile)
.sorted(Comparator.comparing(p -> p.getFileName().toString())) .sorted(Comparator.comparing(p -> p.getFileName().toString()))
.toList(); .toList()) {
for (Path audioFile : files) {
ZipEntry entry = new ZipEntry(audioFile.getFileName().toString()); ZipEntry entry = new ZipEntry(audioFile.getFileName().toString());
zos.putNextEntry(entry); zos.putNextEntry(entry);
Files.copy(audioFile, zos); Files.copy(audioFile, zos);
zos.closeEntry(); zos.closeEntry();
} }
} }
}
byte[] zipBytes = baos.toByteArray(); byte[] zipBytes = baos.toByteArray();
Resource resource = new ByteArrayResource(zipBytes); Resource resource = new ByteArrayResource(zipBytes);
@@ -52,11 +52,14 @@ public class FileFingerprint {
throw new RuntimeException("Folder does not exist: " + folderPath); throw new RuntimeException("Folder does not exist: " + folderPath);
} }
List<Path> audioFiles = Files.list(folderPath) List<Path> audioFiles;
try (var files = Files.list(folderPath)) {
audioFiles = files
.filter(Files::isRegularFile) .filter(Files::isRegularFile)
.filter(p -> isAudioFile(p.getFileName().toString())) .filter(p -> isAudioFile(p.getFileName().toString()))
.sorted(Comparator.comparing(p -> p.getFileName().toString())) .sorted(Comparator.comparing(p -> p.getFileName().toString()))
.toList(); .toList();
}
if (audioFiles.isEmpty()) { if (audioFiles.isEmpty()) {
throw new RuntimeException("No audio files in folder: " + folderPath); throw new RuntimeException("No audio files in folder: " + folderPath);
@@ -86,7 +86,9 @@ public class FileUtils {
log.warn("Folder does not exist or is not a directory: {}", folderPath.toAbsolutePath()); log.warn("Folder does not exist or is not a directory: {}", folderPath.toAbsolutePath());
return null; return null;
} }
long totalBytes = Files.walk(folderPath) long totalBytes;
try (var files = Files.walk(folderPath)) {
totalBytes = files
.filter(Files::isRegularFile) .filter(Files::isRegularFile)
.mapToLong(p -> { .mapToLong(p -> {
try { try {
@@ -96,6 +98,7 @@ public class FileUtils {
} }
}) })
.sum(); .sum();
}
return totalBytes / 1024; return totalBytes / 1024;
} catch (IOException e) { } catch (IOException e) {
log.error("Failed to get folder size for path [{}]: {}", folderPath, e.getMessage(), 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)) { if (!Files.exists(folderPath) || !Files.isDirectory(folderPath)) {
return Optional.empty(); return Optional.empty();
} }
return Files.list(folderPath) try (var files = Files.list(folderPath)) {
return files
.filter(Files::isRegularFile) .filter(Files::isRegularFile)
.filter(p -> isAudioFile(p.getFileName().toString())) .filter(p -> isAudioFile(p.getFileName().toString()))
.sorted(Comparator.comparing(p -> p.getFileName().toString())) .sorted(Comparator.comparing(p -> p.getFileName().toString()))
.findFirst(); .findFirst();
}
} catch (IOException e) { } catch (IOException e) {
log.error("Failed to list folder [{}]: {}", folderPath, e.getMessage(), e); log.error("Failed to list folder [{}]: {}", folderPath, e.getMessage(), e);
return Optional.empty(); return Optional.empty();
@@ -166,11 +171,13 @@ public class FileUtils {
if (!Files.exists(folderPath) || !Files.isDirectory(folderPath)) { if (!Files.exists(folderPath) || !Files.isDirectory(folderPath)) {
return List.of(); return List.of();
} }
return Files.list(folderPath) try (var files = Files.list(folderPath)) {
return files
.filter(Files::isRegularFile) .filter(Files::isRegularFile)
.filter(p -> isAudioFile(p.getFileName().toString())) .filter(p -> isAudioFile(p.getFileName().toString()))
.sorted(Comparator.comparing(p -> p.getFileName().toString())) .sorted(Comparator.comparing(p -> p.getFileName().toString()))
.toList(); .toList();
}
} catch (IOException e) { } catch (IOException e) {
log.error("Failed to list audio files in folder [{}]: {}", folderPath, e.getMessage(), e); log.error("Failed to list audio files in folder [{}]: {}", folderPath, e.getMessage(), e);
return List.of(); return List.of();