fix(file-imports): Properly group multi-file audiobooks imported via the folder watcher (#1489)
This commit is contained in:
@@ -251,6 +251,12 @@ public class LibraryFileEventProcessor implements SmartLifecycle {
|
|||||||
var mode = library.getOrganizationMode() != null
|
var mode = library.getOrganizationMode() != null
|
||||||
? library.getOrganizationMode() : LibraryOrganizationMode.AUTO_DETECT;
|
? library.getOrganizationMode() : LibraryOrganizationMode.AUTO_DETECT;
|
||||||
|
|
||||||
|
FolderAnalysis analysis = analyzeFolderForAudiobook(folderPath);
|
||||||
|
if (analysis.isFolderBasedAudiobook()) {
|
||||||
|
processFolderAudiobook(library, folderPath, analysis);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (mode == LibraryOrganizationMode.BOOK_PER_FILE) {
|
if (mode == LibraryOrganizationMode.BOOK_PER_FILE) {
|
||||||
processFilesInFolderIndividually(library, folderPath);
|
processFilesInFolderIndividually(library, folderPath);
|
||||||
return;
|
return;
|
||||||
@@ -261,30 +267,27 @@ public class LibraryFileEventProcessor implements SmartLifecycle {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// AUTO_DETECT: existing audiobook folder detection logic
|
processTrackedAndWalkedFiles(library, folderPath);
|
||||||
FolderAnalysis analysis = analyzeFolderForAudiobook(folderPath);
|
}
|
||||||
|
|
||||||
if (analysis.isFolderBasedAudiobook()) {
|
private void processFolderAudiobook(LibraryEntity library, Path folderPath, FolderAnalysis analysis) {
|
||||||
log.info("[FOLDER_AUDIOBOOK] Detected folder-based audiobook: {} ({} audio files)",
|
log.info("[FOLDER_AUDIOBOOK] Detected folder-based audiobook: {} ({} audio files)",
|
||||||
folderPath.getFileName(), analysis.audioFileCount());
|
folderPath.getFileName(), analysis.audioFileCount());
|
||||||
try {
|
try {
|
||||||
bookFileTransactionalHandler.handleNewFolderAudiobook(library.getId(), folderPath);
|
bookFileTransactionalHandler.handleNewFolderAudiobook(library.getId(), folderPath);
|
||||||
int cleared = 0;
|
int cleared = 0;
|
||||||
var iterator = filesFromPendingFolder.iterator();
|
var iterator = filesFromPendingFolder.iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
if (iterator.next().startsWith(folderPath)) {
|
if (iterator.next().startsWith(folderPath)) {
|
||||||
iterator.remove();
|
iterator.remove();
|
||||||
cleared++;
|
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 {
|
if (cleared > 0) {
|
||||||
processTrackedAndWalkedFiles(library, folderPath);
|
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();
|
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
|
@Test
|
||||||
void autoDetect_singleAudioFileStaysIndividual() throws IOException {
|
void autoDetect_singleAudioFileStaysIndividual() throws IOException {
|
||||||
Path dir = Files.createDirectories(tempDir.resolve("Book"));
|
Path dir = Files.createDirectories(tempDir.resolve("Book"));
|
||||||
|
|||||||
@@ -184,6 +184,42 @@ class LibraryFileEventProcessorTest {
|
|||||||
verify(bookFileTransactionalHandler, times(2)).handleNewBookFile(eq(1L), any());
|
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
|
@Test
|
||||||
void bookPerFileMode_ignoresFolderWithIgnoreFile() throws Exception {
|
void bookPerFileMode_ignoresFolderWithIgnoreFile() throws Exception {
|
||||||
library.setOrganizationMode(LibraryOrganizationMode.BOOK_PER_FILE);
|
library.setOrganizationMode(LibraryOrganizationMode.BOOK_PER_FILE);
|
||||||
|
|||||||
Reference in New Issue
Block a user