From 2b7f53b0a7c77c9abde65184215fb918462c99df Mon Sep 17 00:00:00 2001 From: advplyr Date: Mon, 7 Mar 2022 16:22:20 -0600 Subject: [PATCH] Add:Support for book folders with CD# subfolders #393 --- server/scanner/AudioFileScanner.js | 12 ++++++++++-- server/utils/scandir.js | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/server/scanner/AudioFileScanner.js b/server/scanner/AudioFileScanner.js index 9cfd6660..3cf00833 100644 --- a/server/scanner/AudioFileScanner.js +++ b/server/scanner/AudioFileScanner.js @@ -9,8 +9,9 @@ const { LogLevel } = require('../utils/constants') class AudioFileScanner { constructor() { } - getTrackAndDiscNumberFromFilename(bookScanData, filename) { + getTrackAndDiscNumberFromFilename(bookScanData, audioFileData) { const { title, author, series, publishYear } = bookScanData + const { filename, path } = audioFileData var partbasename = Path.basename(filename, Path.extname(filename)) // Remove title, author, series, and publishYear from filename if there @@ -31,6 +32,13 @@ class AudioFileScanner { partbasename = partbasename.replace(/\b(disc|cd) ?(\d\d?)\b/i, '') } + // Look for disc number in folder path e.g. /Book Title/CD01/audiofile.mp3 + var pathdir = Path.dirname(path).split('/').pop() + if (pathdir && /^cd\d{1,3}$/i.test(pathdir)) { + var discFromFolder = Number(pathdir.replace(/cd/i, '')) + if (!isNaN(discFromFolder) && discFromFolder !== null) discNumber = discFromFolder + } + var numbersinpath = partbasename.match(/\d{1,4}/g) var trackNumber = numbersinpath && numbersinpath.length ? parseInt(numbersinpath[0]) : null return { @@ -60,7 +68,7 @@ class AudioFileScanner { audioFileData.trackNumFromMeta = probeData.trackNumber audioFileData.discNumFromMeta = probeData.discNumber - const { trackNumber, discNumber } = this.getTrackAndDiscNumberFromFilename(bookScanData, audioFileData.filename) + const { trackNumber, discNumber } = this.getTrackAndDiscNumberFromFilename(bookScanData, audioFileData) audioFileData.trackNumFromFilename = trackNumber audioFileData.discNumFromFilename = discNumber diff --git a/server/utils/scandir.js b/server/utils/scandir.js index c8011c52..2e84d3c2 100644 --- a/server/utils/scandir.js +++ b/server/utils/scandir.js @@ -47,6 +47,9 @@ function groupFilesIntoAudiobookPaths(paths) { } else if (!dirparts.length) { // This is the last directory, create group audiobookGroup[_path] = [Path.basename(path)] return + } else if (dirparts.length === 1 && /^cd\d{1,3}$/i.test(dirparts[0])) { // Next directory is the last and is a CD dir, create group + audiobookGroup[_path] = [Path.posix.join(dirparts[0], Path.basename(path))] + return } } }) @@ -88,6 +91,9 @@ function groupFileItemsIntoBooks(fileItems) { } else if (!dirparts.length) { // This is the last directory, create group audiobookGroup[_path] = [item.name] return + } else if (dirparts.length === 1 && /^cd\d{1,3}$/i.test(dirparts[0])) { // Next directory is the last and is a CD dir, create group + audiobookGroup[_path] = [Path.posix.join(dirparts[0], item.name)] + return } } })