fix(bookdrop): ignore dot and underscore files during monitoring (#1771)

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
Balázs Szücs
2025-12-06 20:12:57 -07:00
committed by GitHub
parent 05261c84f1
commit d336cde261
2 changed files with 73 additions and 4 deletions
@@ -147,16 +147,19 @@ public class BookdropMonitoringService {
try (Stream<Path> 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<Path> 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);
@@ -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());
}
}