fix(bookdrop): improve file filtering to ignore .caltrash (calibre generated trash file) (#1785)

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
Balázs Szücs
2025-12-07 15:16:53 -07:00
committed by GitHub
parent ad0a99bcbc
commit 5e5bb41585
3 changed files with 17 additions and 4 deletions
@@ -3,6 +3,7 @@ package com.adityachandel.booklore.service.bookdrop;
import com.adityachandel.booklore.config.AppProperties; import com.adityachandel.booklore.config.AppProperties;
import com.adityachandel.booklore.model.enums.BookFileExtension; import com.adityachandel.booklore.model.enums.BookFileExtension;
import com.adityachandel.booklore.util.FileService; import com.adityachandel.booklore.util.FileService;
import com.adityachandel.booklore.util.FileUtils;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy; import jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -147,14 +148,14 @@ public class BookdropMonitoringService {
try (Stream<Path> pathStream = Files.walk(fullPath)) { try (Stream<Path> pathStream = Files.walk(fullPath)) {
pathStream pathStream
.filter(Files::isRegularFile) .filter(Files::isRegularFile)
.filter(path -> !path.getFileName().toString().startsWith(".")) .filter(path -> !FileUtils.shouldIgnore(path))
.filter(path -> BookFileExtension.fromFileName(path.getFileName().toString()).isPresent()) .filter(path -> BookFileExtension.fromFileName(path.getFileName().toString()).isPresent())
.forEach(path -> eventHandler.enqueueFile(path, StandardWatchEventKinds.ENTRY_CREATE)); .forEach(path -> eventHandler.enqueueFile(path, StandardWatchEventKinds.ENTRY_CREATE));
} catch (IOException e) { } catch (IOException e) {
log.error("Failed to scan new directory: {}", fullPath, e); log.error("Failed to scan new directory: {}", fullPath, e);
} }
} else { } else {
if (!(!fullPath.getFileName().toString().isEmpty() && fullPath.getFileName().toString().charAt(0) == '.')) { if (!FileUtils.shouldIgnore(fullPath)) {
if (BookFileExtension.fromFileName(fullPath.getFileName().toString()).isPresent()) { if (BookFileExtension.fromFileName(fullPath.getFileName().toString()).isPresent()) {
eventHandler.enqueueFile(fullPath, kind); eventHandler.enqueueFile(fullPath, kind);
} else { } else {
@@ -188,7 +189,7 @@ public class BookdropMonitoringService {
private void scanExistingBookdropFiles() { private void scanExistingBookdropFiles() {
try (Stream<Path> files = Files.walk(bookdrop)) { try (Stream<Path> files = Files.walk(bookdrop)) {
files.filter(Files::isRegularFile) files.filter(Files::isRegularFile)
.filter(path -> !(!path.getFileName().toString().isEmpty() && path.getFileName().toString().charAt(0) == '.')) .filter(path -> !FileUtils.shouldIgnore(path))
.filter(path -> BookFileExtension.fromFileName(path.getFileName().toString()).isPresent()) .filter(path -> BookFileExtension.fromFileName(path.getFileName().toString()).isPresent())
.forEach(file -> { .forEach(file -> {
log.info("Found existing supported file on startup: {}", file); log.info("Found existing supported file on startup: {}", file);
@@ -34,6 +34,7 @@ public class LibraryFileHelper {
try (Stream<Path> stream = Files.walk(libraryPath, FileVisitOption.FOLLOW_LINKS)) { try (Stream<Path> stream = Files.walk(libraryPath, FileVisitOption.FOLLOW_LINKS)) {
return stream.filter(Files::isRegularFile) return stream.filter(Files::isRegularFile)
.filter(path -> !FileUtils.shouldIgnore(path))
.map(fullPath -> { .map(fullPath -> {
String fileName = fullPath.getFileName().toString(); String fileName = fullPath.getFileName().toString();
Optional<BookFileExtension> bookExtension = BookFileExtension.fromFileName(fileName); Optional<BookFileExtension> bookExtension = BookFileExtension.fromFileName(fileName);
@@ -51,7 +52,6 @@ public class LibraryFileHelper {
.build(); .build();
}) })
.filter(Objects::nonNull) .filter(Objects::nonNull)
.filter(file -> !file.getFileName().startsWith("."))
.toList(); .toList();
} }
} }
@@ -59,4 +59,16 @@ public class FileUtils {
.forEach(File::delete); .forEach(File::delete);
} }
} }
public boolean shouldIgnore(Path path) {
if (!path.getFileName().toString().isEmpty() && path.getFileName().toString().charAt(0) == '.') {
return true;
}
for (Path part : path) {
if (".caltrash".equals(part.toString())) {
return true;
}
}
return false;
}
} }