Smarter AUTO_DETECT grouping for audiobook series folders and multi-part files (#2900)
This commit is contained in:
@@ -109,22 +109,25 @@ public class LibraryFileHelper {
|
||||
boolean hasNonAudioBooks = dirHasNonAudioBooks.getOrDefault(dir, false);
|
||||
|
||||
if (audioFiles != null && audioFiles.size() >= MIN_AUDIO_FILES_FOR_FOLDER_AUDIOBOOK && !hasNonAudioBooks) {
|
||||
// This is a folder-based audiobook: contains 2+ audio files and no ebook files
|
||||
// Don't treat library root as audiobook folder
|
||||
if (!dir.equals(libraryPath)) {
|
||||
log.info("Detected folder-based audiobook: {} ({} audio files)", dir.getFileName(), audioFiles.size());
|
||||
if (FileUtils.isSeriesFolder(audioFiles)) {
|
||||
log.info("Detected series folder: {} ({} audio files with distinct titles)", dir.getFileName(), audioFiles.size());
|
||||
addIndividualAudioFiles(audioFiles, libraryEntity, pathEntity, libraryFiles);
|
||||
} else {
|
||||
log.info("Detected folder-based audiobook: {} ({} audio files)", dir.getFileName(), audioFiles.size());
|
||||
|
||||
processedAsFolderAudiobook.add(dir);
|
||||
processedAsFolderAudiobook.add(dir);
|
||||
|
||||
// Add a single LibraryFile representing the folder
|
||||
libraryFiles.add(LibraryFile.builder()
|
||||
.libraryEntity(libraryEntity)
|
||||
.libraryPathEntity(pathEntity)
|
||||
.fileSubPath(FileUtils.getRelativeSubPath(pathEntity.getPath(), dir))
|
||||
.fileName(dir.getFileName().toString())
|
||||
.bookFileType(BookFileType.AUDIOBOOK)
|
||||
.folderBased(true)
|
||||
.build());
|
||||
libraryFiles.add(LibraryFile.builder()
|
||||
.libraryEntity(libraryEntity)
|
||||
.libraryPathEntity(pathEntity)
|
||||
.fileSubPath(FileUtils.getRelativeSubPath(pathEntity.getPath(), dir))
|
||||
.fileName(dir.getFileName().toString())
|
||||
.bookFileType(BookFileType.AUDIOBOOK)
|
||||
.folderBased(true)
|
||||
.build());
|
||||
}
|
||||
} else {
|
||||
// Library root - add individual audio files
|
||||
addIndividualAudioFiles(audioFiles, libraryEntity, pathEntity, libraryFiles);
|
||||
|
||||
+10
-5
@@ -18,6 +18,8 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.*;
|
||||
@@ -252,7 +254,7 @@ public class LibraryFileEventProcessor {
|
||||
}
|
||||
|
||||
private FolderAnalysis analyzeFolderForAudiobook(Path folderPath) {
|
||||
int audioFileCount = 0;
|
||||
List<Path> audioFiles = new ArrayList<>();
|
||||
boolean hasNonAudioBook = false;
|
||||
|
||||
try (var stream = Files.walk(folderPath)) {
|
||||
@@ -263,7 +265,7 @@ public class LibraryFileEventProcessor {
|
||||
var ext = BookFileExtension.fromFileName(fileName);
|
||||
if (ext.isPresent()) {
|
||||
if (ext.get().getType() == BookFileType.AUDIOBOOK) {
|
||||
audioFileCount++;
|
||||
audioFiles.add(file);
|
||||
} else {
|
||||
hasNonAudioBook = true;
|
||||
}
|
||||
@@ -273,12 +275,15 @@ public class LibraryFileEventProcessor {
|
||||
log.warn("[ERROR] Analyzing folder '{}': {}", folderPath, e.getMessage());
|
||||
}
|
||||
|
||||
return new FolderAnalysis(audioFileCount, hasNonAudioBook);
|
||||
boolean isSeriesFolder = audioFiles.size() >= MIN_AUDIO_FILES_FOR_FOLDER_AUDIOBOOK
|
||||
&& FileUtils.isSeriesFolder(audioFiles);
|
||||
|
||||
return new FolderAnalysis(audioFiles.size(), hasNonAudioBook, isSeriesFolder);
|
||||
}
|
||||
|
||||
private record FolderAnalysis(int audioFileCount, boolean hasNonAudioBook) {
|
||||
private record FolderAnalysis(int audioFileCount, boolean hasNonAudioBook, boolean isSeriesFolder) {
|
||||
boolean isFolderBasedAudiobook() {
|
||||
return audioFileCount >= MIN_AUDIO_FILES_FOR_FOLDER_AUDIOBOOK && !hasNonAudioBook;
|
||||
return audioFileCount >= MIN_AUDIO_FILES_FOR_FOLDER_AUDIOBOOK && !hasNonAudioBook && !isSeriesFolder;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +44,23 @@ public class BookFileGroupingUtils {
|
||||
"[\\s_-]?(\\d{1,3})\\s*$"
|
||||
);
|
||||
|
||||
// Part/disc indicators: "(part 1)", "[pt 2]", "- disc 1", "cd 3", etc.
|
||||
// "part" requires brackets or dash prefix to avoid conflict with series labels ("Part 1" = series entry).
|
||||
// "pt", "disc", "disk", "cd" are unambiguous and can be matched bare.
|
||||
private static final Pattern PART_INDICATOR_PATTERN = Pattern.compile(
|
||||
"\\s*(?:" +
|
||||
"[\\(\\[]\\s*(?:part|pt|dis[ck]|cd)\\s*\\d+\\s*[\\)\\]]" +
|
||||
"|-\\s*(?:part|pt|dis[ck]|cd)\\s*\\d+" +
|
||||
"|(?:pt|dis[ck]|cd)\\s*\\d+" +
|
||||
")\\s*$",
|
||||
Pattern.CASE_INSENSITIVE
|
||||
);
|
||||
|
||||
// Bare-number prefix: "1. Title", "01 - Title" (NOT "101 Dalmatians" or "1984")
|
||||
private static final Pattern BARE_NUMBER_PREFIX_PATTERN = Pattern.compile(
|
||||
"^(\\d{1,3})(?:\\.|\\s*-)\\s+(.+)$"
|
||||
);
|
||||
|
||||
// Edition/version patterns that should NOT prevent grouping
|
||||
// Includes format indicators (audiobook, ebook) and edition descriptors
|
||||
private static final Pattern EDITION_PATTERN = Pattern.compile(
|
||||
@@ -86,6 +103,9 @@ public class BookFileGroupingUtils {
|
||||
|
||||
baseName = FORMAT_INDICATOR_PATTERN.matcher(baseName).replaceAll("");
|
||||
|
||||
// Strip part/disc indicators (e.g., "(part 1)", "[cd 2]")
|
||||
baseName = PART_INDICATOR_PATTERN.matcher(baseName).replaceAll("");
|
||||
|
||||
// Strip trailing author names (after dash)
|
||||
baseName = TRAILING_AUTHOR_PATTERN.matcher(baseName).replaceAll("");
|
||||
|
||||
@@ -318,6 +338,18 @@ public class BookFileGroupingUtils {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: bare-number prefix like "1. title" or "01 - title"
|
||||
Matcher bareMatcher = BARE_NUMBER_PREFIX_PATTERN.matcher(key);
|
||||
if (bareMatcher.matches()) {
|
||||
String number = bareMatcher.group(1);
|
||||
String baseTitle = bareMatcher.group(2).trim();
|
||||
baseTitle = WHITESPACE_PATTERN.matcher(baseTitle).replaceAll(" ").trim();
|
||||
if (baseTitle.length() >= 3) {
|
||||
return new SeriesInfo(baseTitle, number);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,8 +12,11 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@UtilityClass
|
||||
@Slf4j
|
||||
@@ -147,6 +150,43 @@ public class FileUtils {
|
||||
}
|
||||
}
|
||||
|
||||
private static final Pattern LEADING_NUMBER_PREFIX = Pattern.compile("^\\d{1,3}(?:\\.|\\s*-)\\s*");
|
||||
private static final Pattern PART_DISC_INDICATOR = Pattern.compile(
|
||||
"\\s*[\\(\\[\\-]?\\s*(?:part|pt|dis[ck]|cd)\\s*\\d+\\s*[\\)\\]]?\\s*$",
|
||||
Pattern.CASE_INSENSITIVE
|
||||
);
|
||||
private static final Pattern TRAILING_NUMBERS = Pattern.compile("\\s*\\d+\\s*$");
|
||||
private static final Set<String> GENERIC_AUDIO_TITLES = Set.of(
|
||||
"chapter", "track", "part", "disc", "disk", "cd", "side", "intro", "epilogue", "prologue", "outro"
|
||||
);
|
||||
|
||||
/**
|
||||
* Determines if a list of audio files represents a series folder (each file is a separate book)
|
||||
* rather than a multi-file audiobook (chapter files for one book).
|
||||
*
|
||||
* Extracts a "base title" from each file by stripping numbering, part indicators, and extensions,
|
||||
* then counts distinct non-generic titles. If more than one distinct title exists, it's a series folder.
|
||||
*/
|
||||
public boolean isSeriesFolder(List<Path> audioFiles) {
|
||||
Set<String> distinctTitles = new HashSet<>();
|
||||
for (Path file : audioFiles) {
|
||||
String title = extractBaseTitle(file.getFileName().toString());
|
||||
if (!title.isEmpty() && !GENERIC_AUDIO_TITLES.contains(title)) {
|
||||
distinctTitles.add(title);
|
||||
}
|
||||
}
|
||||
return distinctTitles.size() > 1;
|
||||
}
|
||||
|
||||
private String extractBaseTitle(String fileName) {
|
||||
int lastDot = fileName.lastIndexOf('.');
|
||||
String baseName = lastDot > 0 ? fileName.substring(0, lastDot) : fileName;
|
||||
baseName = LEADING_NUMBER_PREFIX.matcher(baseName).replaceFirst("");
|
||||
baseName = PART_DISC_INDICATOR.matcher(baseName).replaceAll("");
|
||||
baseName = TRAILING_NUMBERS.matcher(baseName).replaceAll("");
|
||||
return baseName.toLowerCase().trim();
|
||||
}
|
||||
|
||||
public void deleteDirectoryRecursively(Path path) throws IOException {
|
||||
if (!Files.exists(path)) return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user