fix(api): fix potential memory leaks in file processing
This commit is contained in:
@@ -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,22 +193,22 @@ 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)) {
|
||||||
.filter(Files::isRegularFile)
|
for (Path audioFile : audioFiles
|
||||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
.filter(Files::isRegularFile)
|
||||||
.toList();
|
.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) {
|
try (InputStream fis = Files.newInputStream(audioFile)) {
|
||||||
String entryName = folderPrefix + audioFile.getFileName().toString();
|
fis.transferTo(zos);
|
||||||
ZipEntry zipEntry = new ZipEntry(entryName);
|
}
|
||||||
zipEntry.setSize(Files.size(audioFile));
|
|
||||||
zos.putNextEntry(zipEntry);
|
|
||||||
|
|
||||||
try (InputStream fis = Files.newInputStream(audioFile)) {
|
zos.closeEntry();
|
||||||
fis.transferTo(zos);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
zos.closeEntry();
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Regular file
|
// Regular file
|
||||||
@@ -322,16 +322,16 @@ 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)) {
|
||||||
.filter(Files::isRegularFile)
|
for (Path audioFile : files
|
||||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
.filter(Files::isRegularFile)
|
||||||
.toList();
|
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||||
|
.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();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -131,16 +131,16 @@ 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)) {
|
||||||
.filter(Files::isRegularFile)
|
for (Path audioFile : files
|
||||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
.filter(Files::isRegularFile)
|
||||||
.toList();
|
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||||
|
.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();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
.filter(Files::isRegularFile)
|
try (var files = Files.list(folderPath)) {
|
||||||
.filter(p -> isAudioFile(p.getFileName().toString()))
|
audioFiles = files
|
||||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
.filter(Files::isRegularFile)
|
||||||
.toList();
|
.filter(p -> isAudioFile(p.getFileName().toString()))
|
||||||
|
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||||
|
.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,16 +86,19 @@ 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;
|
||||||
.filter(Files::isRegularFile)
|
try (var files = Files.walk(folderPath)) {
|
||||||
.mapToLong(p -> {
|
totalBytes = files
|
||||||
try {
|
.filter(Files::isRegularFile)
|
||||||
return Files.size(p);
|
.mapToLong(p -> {
|
||||||
} catch (IOException e) {
|
try {
|
||||||
return 0L;
|
return Files.size(p);
|
||||||
}
|
} catch (IOException e) {
|
||||||
})
|
return 0L;
|
||||||
.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)) {
|
||||||
.filter(Files::isRegularFile)
|
return files
|
||||||
.filter(p -> isAudioFile(p.getFileName().toString()))
|
.filter(Files::isRegularFile)
|
||||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
.filter(p -> isAudioFile(p.getFileName().toString()))
|
||||||
.findFirst();
|
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||||
|
.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)) {
|
||||||
.filter(Files::isRegularFile)
|
return files
|
||||||
.filter(p -> isAudioFile(p.getFileName().toString()))
|
.filter(Files::isRegularFile)
|
||||||
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
.filter(p -> isAudioFile(p.getFileName().toString()))
|
||||||
.toList();
|
.sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
||||||
|
.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();
|
||||||
|
|||||||
Reference in New Issue
Block a user