fix(file-imports): Properly group multi-file audiobooks imported via the folder watcher (#1489)

This commit is contained in:
Zach
2026-05-25 20:01:58 -04:00
committed by GitHub
parent 150dcf4f88
commit fbbcd3f33e
3 changed files with 88 additions and 21 deletions
@@ -251,6 +251,12 @@ public class LibraryFileEventProcessor implements SmartLifecycle {
var mode = library.getOrganizationMode() != null
? library.getOrganizationMode() : LibraryOrganizationMode.AUTO_DETECT;
FolderAnalysis analysis = analyzeFolderForAudiobook(folderPath);
if (analysis.isFolderBasedAudiobook()) {
processFolderAudiobook(library, folderPath, analysis);
return;
}
if (mode == LibraryOrganizationMode.BOOK_PER_FILE) {
processFilesInFolderIndividually(library, folderPath);
return;
@@ -261,30 +267,27 @@ public class LibraryFileEventProcessor implements SmartLifecycle {
return;
}
// AUTO_DETECT: existing audiobook folder detection logic
FolderAnalysis analysis = analyzeFolderForAudiobook(folderPath);
processTrackedAndWalkedFiles(library, folderPath);
}
if (analysis.isFolderBasedAudiobook()) {
log.info("[FOLDER_AUDIOBOOK] Detected folder-based audiobook: {} ({} audio files)",
folderPath.getFileName(), analysis.audioFileCount());
try {
bookFileTransactionalHandler.handleNewFolderAudiobook(library.getId(), folderPath);
int cleared = 0;
var iterator = filesFromPendingFolder.iterator();
while (iterator.hasNext()) {
if (iterator.next().startsWith(folderPath)) {
iterator.remove();
cleared++;
}
private void processFolderAudiobook(LibraryEntity library, Path folderPath, FolderAnalysis analysis) {
log.info("[FOLDER_AUDIOBOOK] Detected folder-based audiobook: {} ({} audio files)",
folderPath.getFileName(), analysis.audioFileCount());
try {
bookFileTransactionalHandler.handleNewFolderAudiobook(library.getId(), folderPath);
int cleared = 0;
var iterator = filesFromPendingFolder.iterator();
while (iterator.hasNext()) {
if (iterator.next().startsWith(folderPath)) {
iterator.remove();
cleared++;
}
if (cleared > 0) {
log.debug("[FOLDER_AUDIOBOOK] Cleared {} tracked files that are now part of folder audiobook", cleared);
}
} catch (Exception e) {
log.warn("[ERROR] Processing folder audiobook '{}': {}", folderPath, e.getMessage());
}
} else {
processTrackedAndWalkedFiles(library, folderPath);
if (cleared > 0) {
log.debug("[FOLDER_AUDIOBOOK] Cleared {} tracked files that are now part of folder audiobook", cleared);
}
} catch (Exception e) {
log.warn("[ERROR] Processing folder audiobook '{}': {}", folderPath, e.getMessage());
}
}
@@ -374,6 +374,34 @@ class LibraryFileHelperTest {
assertThat(files.getFirst().isFolderBased()).isTrue();
}
@Test
void bookPerFile_collapsesMultiFileAudiobookSubfolder() throws IOException {
Path audioDir = Files.createDirectories(tempDir.resolve("Chaptered Audiobook"));
Files.write(audioDir.resolve("Chaptered Audiobook - 1 - Chapter One.mp3"), new byte[]{1});
Files.write(audioDir.resolve("Chaptered Audiobook - 2 - Chapter Two.mp3"), new byte[]{1});
List<LibraryFile> files = libraryFileHelper.getLibraryFiles(createLibraryWithMode(tempDir, LibraryOrganizationMode.BOOK_PER_FILE));
assertThat(files).hasSize(1);
assertThat(files.getFirst().isFolderBased()).isTrue();
assertThat(files.getFirst().getFileName()).isEqualTo("Chaptered Audiobook");
assertThat(files.getFirst().getBookFileType()).isEqualTo(BookFileType.AUDIOBOOK);
}
@Test
void bookPerFolder_collapsesMultiFileAudiobookSubfolder() throws IOException {
Path audioDir = Files.createDirectories(tempDir.resolve("Chaptered Audiobook"));
Files.write(audioDir.resolve("Chaptered Audiobook - 1 - Chapter One.mp3"), new byte[]{1});
Files.write(audioDir.resolve("Chaptered Audiobook - 2 - Chapter Two.mp3"), new byte[]{1});
List<LibraryFile> files = libraryFileHelper.getLibraryFiles(createLibraryWithMode(tempDir, LibraryOrganizationMode.BOOK_PER_FOLDER));
assertThat(files).hasSize(1);
assertThat(files.getFirst().isFolderBased()).isTrue();
assertThat(files.getFirst().getFileName()).isEqualTo("Chaptered Audiobook");
assertThat(files.getFirst().getBookFileType()).isEqualTo(BookFileType.AUDIOBOOK);
}
@Test
void autoDetect_singleAudioFileStaysIndividual() throws IOException {
Path dir = Files.createDirectories(tempDir.resolve("Book"));
@@ -184,6 +184,42 @@ class LibraryFileEventProcessorTest {
verify(bookFileTransactionalHandler, times(2)).handleNewBookFile(eq(1L), any());
}
@Test
void bookPerFileMode_processesMultiFileAudiobookFolderAsOneBook() throws Exception {
library.setOrganizationMode(LibraryOrganizationMode.BOOK_PER_FILE);
Path folder = tempDir.resolve("Chaptered Audiobook");
Files.createDirectory(folder);
Files.writeString(folder.resolve("Chaptered Audiobook - 1 - Chapter One.mp3"), "audio content 1");
Files.writeString(folder.resolve("Chaptered Audiobook - 2 - Chapter Two.mp3"), "audio content 2");
processor.processEvent(StandardWatchEventKinds.ENTRY_CREATE, 1L, folder, true);
Thread.sleep(8000);
verify(bookFileTransactionalHandler).handleNewFolderAudiobook(eq(1L), eq(folder));
verify(bookFileTransactionalHandler, never()).handleNewBookFile(anyLong(), any());
verify(libraryProcessingService, never()).processLibraryFiles(any(), any());
}
@Test
void bookPerFolderMode_processesMultiFileAudiobookFolderAsOneBook() throws Exception {
library.setOrganizationMode(LibraryOrganizationMode.BOOK_PER_FOLDER);
Path folder = tempDir.resolve("Chaptered Audiobook");
Files.createDirectory(folder);
Files.writeString(folder.resolve("Chaptered Audiobook - 1 - Chapter One.mp3"), "audio content 1");
Files.writeString(folder.resolve("Chaptered Audiobook - 2 - Chapter Two.mp3"), "audio content 2");
processor.processEvent(StandardWatchEventKinds.ENTRY_CREATE, 1L, folder, true);
Thread.sleep(8000);
verify(bookFileTransactionalHandler).handleNewFolderAudiobook(eq(1L), eq(folder));
verify(bookFileTransactionalHandler, never()).handleNewBookFile(anyLong(), any());
verify(libraryProcessingService, never()).processLibraryFiles(any(), any());
}
@Test
void bookPerFileMode_ignoresFolderWithIgnoreFile() throws Exception {
library.setOrganizationMode(LibraryOrganizationMode.BOOK_PER_FILE);