From d336cde261d0295a52b0dad8209148e9dfc1a8d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= <127139797+balazs-szucs@users.noreply.github.com> Date: Sun, 7 Dec 2025 04:12:57 +0100 Subject: [PATCH] fix(bookdrop): ignore dot and underscore files during monitoring (#1771) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .../bookdrop/BookdropMonitoringService.java | 12 ++-- .../BookdropMonitoringServiceTest.java | 65 +++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 booklore-api/src/test/java/com/adityachandel/booklore/service/bookdrop/BookdropMonitoringServiceTest.java diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/bookdrop/BookdropMonitoringService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/bookdrop/BookdropMonitoringService.java index a2f3450a3..f7e8e13d5 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/bookdrop/BookdropMonitoringService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/bookdrop/BookdropMonitoringService.java @@ -147,16 +147,19 @@ public class BookdropMonitoringService { try (Stream pathStream = Files.walk(fullPath)) { pathStream .filter(Files::isRegularFile) + .filter(path -> !path.getFileName().toString().startsWith(".")) .filter(path -> BookFileExtension.fromFileName(path.getFileName().toString()).isPresent()) .forEach(path -> eventHandler.enqueueFile(path, StandardWatchEventKinds.ENTRY_CREATE)); } catch (IOException e) { log.error("Failed to scan new directory: {}", fullPath, e); } } else { - if (BookFileExtension.fromFileName(fullPath.getFileName().toString()).isPresent()) { - eventHandler.enqueueFile(fullPath, kind); - } else { - log.info("Ignored unsupported file type: {}", fullPath); + if (!(!fullPath.getFileName().toString().isEmpty() && fullPath.getFileName().toString().charAt(0) == '.')) { + if (BookFileExtension.fromFileName(fullPath.getFileName().toString()).isPresent()) { + eventHandler.enqueueFile(fullPath, kind); + } else { + log.info("Ignored unsupported file type: {}", fullPath); + } } } } else if (kind == StandardWatchEventKinds.ENTRY_DELETE) { @@ -185,6 +188,7 @@ public class BookdropMonitoringService { private void scanExistingBookdropFiles() { try (Stream files = Files.walk(bookdrop)) { files.filter(Files::isRegularFile) + .filter(path -> !(!path.getFileName().toString().isEmpty() && path.getFileName().toString().charAt(0) == '.')) .filter(path -> BookFileExtension.fromFileName(path.getFileName().toString()).isPresent()) .forEach(file -> { log.info("Found existing supported file on startup: {}", file); diff --git a/booklore-api/src/test/java/com/adityachandel/booklore/service/bookdrop/BookdropMonitoringServiceTest.java b/booklore-api/src/test/java/com/adityachandel/booklore/service/bookdrop/BookdropMonitoringServiceTest.java new file mode 100644 index 000000000..b3b5342b9 --- /dev/null +++ b/booklore-api/src/test/java/com/adityachandel/booklore/service/bookdrop/BookdropMonitoringServiceTest.java @@ -0,0 +1,65 @@ +package com.adityachandel.booklore.service.bookdrop; + +import com.adityachandel.booklore.config.AppProperties; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardWatchEventKinds; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; + +class BookdropMonitoringServiceTest { + + private AppProperties appProperties; + private BookdropEventHandlerService eventHandler; + private BookdropMonitoringService monitoringService; + + @TempDir + Path tempDir; + + @BeforeEach + void setUp() { + appProperties = mock(AppProperties.class); + eventHandler = mock(BookdropEventHandlerService.class); + + when(appProperties.getBookdropFolder()).thenReturn(tempDir.toString()); + monitoringService = new BookdropMonitoringService(appProperties, eventHandler); + } + + @Test + void scanExistingBookdropFiles_ShouldIgnoreDotUnderscoreFiles() throws IOException { + Path validFile = tempDir.resolve("book.epub"); + Files.createFile(validFile); + + Path invalidFile = tempDir.resolve("._book.epub"); + Files.createFile(invalidFile); + + Path hiddenFile = tempDir.resolve(".hidden.epub"); + Files.createFile(hiddenFile); + + Path subDir = tempDir.resolve("subdir"); + Files.createDirectories(subDir); + Path validFileInSubdir = subDir.resolve("another.epub"); + Files.createFile(validFileInSubdir); + + Path invalidFileInSubdir = subDir.resolve("._another.epub"); + Files.createFile(invalidFileInSubdir); + + monitoringService.start(); + + monitoringService.stop(); + + verify(eventHandler).enqueueFile(eq(validFile), eq(StandardWatchEventKinds.ENTRY_CREATE)); + verify(eventHandler).enqueueFile(eq(validFileInSubdir), eq(StandardWatchEventKinds.ENTRY_CREATE)); + + verify(eventHandler, never()).enqueueFile(eq(invalidFile), any()); + verify(eventHandler, never()).enqueueFile(eq(hiddenFile), any()); + verify(eventHandler, never()).enqueueFile(eq(invalidFileInSubdir), any()); + } +}