2021-08-18 00:01:11 +02:00
|
|
|
const Path = require('path')
|
|
|
|
const dir = require('node-dir')
|
|
|
|
const Logger = require('../Logger')
|
|
|
|
|
2021-08-27 21:35:16 +02:00
|
|
|
const AUDIO_FORMATS = ['m4b', 'mp3', 'm4a']
|
2021-08-18 00:01:11 +02:00
|
|
|
const INFO_FORMATS = ['nfo']
|
|
|
|
const IMAGE_FORMATS = ['png', 'jpg', 'jpeg', 'webp']
|
|
|
|
const EBOOK_FORMATS = ['epub', 'pdf']
|
|
|
|
|
|
|
|
function getPaths(path) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
dir.paths(path, function (err, res) {
|
|
|
|
if (err) {
|
2021-09-11 02:55:02 +02:00
|
|
|
Logger.error(err)
|
2021-08-18 00:01:11 +02:00
|
|
|
resolve(false)
|
|
|
|
}
|
|
|
|
resolve(res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-11 02:55:02 +02:00
|
|
|
function groupFilesIntoAudiobookPaths(paths) {
|
|
|
|
// Step 1: Normalize path, Remove leading "/", Filter out files in root dir
|
|
|
|
var pathsFiltered = paths.map(path => Path.normalize(path.slice(1))).filter(path => Path.parse(path).dir)
|
|
|
|
|
|
|
|
|
|
|
|
// Step 2: Sort by least number of directories
|
|
|
|
pathsFiltered.sort((a, b) => {
|
|
|
|
var pathsA = Path.dirname(a).split(Path.sep).length
|
|
|
|
var pathsB = Path.dirname(b).split(Path.sep).length
|
|
|
|
return pathsA - pathsB
|
|
|
|
})
|
|
|
|
|
|
|
|
// Step 3: Group into audiobooks
|
|
|
|
var audiobookGroup = {}
|
|
|
|
pathsFiltered.forEach((path) => {
|
|
|
|
var dirparts = Path.dirname(path).split(Path.sep)
|
|
|
|
var numparts = dirparts.length
|
|
|
|
var _path = ''
|
|
|
|
for (let i = 0; i < numparts; i++) {
|
|
|
|
var dirpart = dirparts.shift()
|
|
|
|
_path = Path.join(_path, dirpart)
|
|
|
|
if (audiobookGroup[_path]) {
|
|
|
|
var relpath = Path.join(dirparts.join(Path.sep), Path.basename(path))
|
|
|
|
audiobookGroup[_path].push(relpath)
|
|
|
|
return
|
|
|
|
} else if (!dirparts.length) {
|
|
|
|
audiobookGroup[_path] = [Path.basename(path)]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return audiobookGroup
|
|
|
|
}
|
|
|
|
module.exports.groupFilesIntoAudiobookPaths = groupFilesIntoAudiobookPaths
|
|
|
|
|
|
|
|
function cleanFileObjects(basepath, abrelpath, files) {
|
|
|
|
return files.map((file) => {
|
|
|
|
var ext = Path.extname(file)
|
|
|
|
return {
|
|
|
|
filetype: getFileType(ext),
|
|
|
|
filename: Path.basename(file),
|
|
|
|
path: Path.join(abrelpath, file), // /AUDIOBOOK/PATH/filename.mp3
|
|
|
|
fullPath: Path.join(basepath, file), // /audiobooks/AUDIOBOOK/PATH/filename.mp3
|
|
|
|
ext: ext
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
function getFileType(ext) {
|
|
|
|
var ext_cleaned = ext.toLowerCase()
|
|
|
|
if (ext_cleaned.startsWith('.')) ext_cleaned = ext_cleaned.slice(1)
|
2021-08-26 00:36:54 +02:00
|
|
|
if (AUDIO_FORMATS.includes(ext_cleaned)) return 'audio'
|
2021-08-18 00:01:11 +02:00
|
|
|
if (INFO_FORMATS.includes(ext_cleaned)) return 'info'
|
|
|
|
if (IMAGE_FORMATS.includes(ext_cleaned)) return 'image'
|
|
|
|
if (EBOOK_FORMATS.includes(ext_cleaned)) return 'ebook'
|
2021-08-19 01:31:19 +02:00
|
|
|
return 'unknown'
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2021-09-11 02:55:02 +02:00
|
|
|
// Primary scan: abRootPath is /audiobooks
|
|
|
|
async function scanRootDir(abRootPath, serverSettings = {}) {
|
|
|
|
var parseSubtitle = !!serverSettings.scannerParseSubtitle
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2021-09-11 02:55:02 +02:00
|
|
|
var pathdata = await getPaths(abRootPath)
|
|
|
|
var filepaths = pathdata.files.map(filepath => {
|
|
|
|
return Path.normalize(filepath).replace(abRootPath, '')
|
|
|
|
})
|
|
|
|
|
|
|
|
var audiobookGrouping = groupFilesIntoAudiobookPaths(filepaths)
|
|
|
|
|
|
|
|
if (!Object.keys(audiobookGrouping).length) {
|
|
|
|
Logger.error('Root path has no audiobooks')
|
|
|
|
return []
|
2021-09-07 03:14:04 +02:00
|
|
|
}
|
|
|
|
|
2021-09-11 02:55:02 +02:00
|
|
|
var audiobooks = []
|
|
|
|
for (const audiobookPath in audiobookGrouping) {
|
|
|
|
var audiobookData = getAudiobookDataFromDir(abRootPath, audiobookPath, parseSubtitle)
|
|
|
|
|
|
|
|
var fileObjs = cleanFileObjects(audiobookData.fullPath, audiobookPath, audiobookGrouping[audiobookPath])
|
|
|
|
audiobooks.push({
|
|
|
|
...audiobookData,
|
|
|
|
audioFiles: fileObjs.filter(f => f.filetype === 'audio'),
|
|
|
|
otherFiles: fileObjs.filter(f => f.filetype !== 'audio')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return audiobooks
|
|
|
|
}
|
|
|
|
module.exports.scanRootDir = scanRootDir
|
|
|
|
|
|
|
|
// Input relative filepath, output all details that can be parsed
|
|
|
|
function getAudiobookDataFromDir(abRootPath, dir, parseSubtitle = false) {
|
|
|
|
var splitDir = dir.split(Path.sep)
|
|
|
|
|
|
|
|
// Audio files will always be in the directory named for the title
|
|
|
|
var title = splitDir.pop()
|
2021-09-07 03:14:04 +02:00
|
|
|
var series = null
|
2021-09-11 02:55:02 +02:00
|
|
|
var author = null
|
|
|
|
// If there are at least 2 more directories, next furthest will be the series
|
|
|
|
if (splitDir.length > 1) series = splitDir.pop()
|
|
|
|
if (splitDir.length > 0) author = splitDir.pop()
|
|
|
|
|
|
|
|
// There could be many more directories, but only the top 3 are used for naming /author/series/title/
|
2021-09-07 03:14:04 +02:00
|
|
|
|
|
|
|
|
2021-09-11 02:55:02 +02:00
|
|
|
var publishYear = null
|
2021-09-07 03:14:04 +02:00
|
|
|
// If Title is of format 1999 - Title, then use 1999 as publish year
|
|
|
|
var publishYearMatch = title.match(/^([0-9]{4}) - (.+)/)
|
|
|
|
if (publishYearMatch && publishYearMatch.length > 2) {
|
|
|
|
if (!isNaN(publishYearMatch[1])) {
|
|
|
|
publishYear = publishYearMatch[1]
|
|
|
|
title = publishYearMatch[2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-11 02:55:02 +02:00
|
|
|
// Subtitle can be parsed from the title if user enabled
|
|
|
|
var subtitle = null
|
2021-09-07 03:14:04 +02:00
|
|
|
if (parseSubtitle && title.includes(' - ')) {
|
|
|
|
var splitOnSubtitle = title.split(' - ')
|
|
|
|
title = splitOnSubtitle.shift()
|
|
|
|
subtitle = splitOnSubtitle.join(' - ')
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
author,
|
|
|
|
title,
|
|
|
|
subtitle,
|
|
|
|
series,
|
|
|
|
publishYear,
|
2021-09-11 02:55:02 +02:00
|
|
|
path: dir, // relative audiobook path i.e. /Author Name/Book Name/..
|
|
|
|
fullPath: Path.join(abRootPath, dir) // i.e. /audiobook/Author Name/Book Name/..
|
2021-09-07 03:14:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getAudiobookFileData(abRootPath, audiobookPath, serverSettings = {}) {
|
|
|
|
var parseSubtitle = !!serverSettings.scannerParseSubtitle
|
|
|
|
|
|
|
|
var paths = await getPaths(audiobookPath)
|
2021-09-11 02:55:02 +02:00
|
|
|
var filepaths = paths.files
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2021-09-11 02:55:02 +02:00
|
|
|
// Sort by least number of directories
|
|
|
|
filepaths.sort((a, b) => {
|
|
|
|
var pathsA = Path.dirname(a).split(Path.sep).length
|
|
|
|
var pathsB = Path.dirname(b).split(Path.sep).length
|
|
|
|
return pathsA - pathsB
|
|
|
|
})
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2021-09-11 02:55:02 +02:00
|
|
|
var audiobookDir = Path.normalize(audiobookPath).replace(abRootPath, '').slice(1)
|
|
|
|
var audiobookData = getAudiobookDataFromDir(abRootPath, audiobookDir, parseSubtitle)
|
|
|
|
var audiobook = {
|
|
|
|
...audiobookData,
|
|
|
|
audioFiles: [],
|
|
|
|
otherFiles: []
|
|
|
|
}
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2021-09-11 02:55:02 +02:00
|
|
|
filepaths.forEach((filepath) => {
|
|
|
|
var relpath = Path.normalize(filepath).replace(abRootPath, '').slice(1)
|
2021-09-07 03:14:04 +02:00
|
|
|
var extname = Path.extname(filepath)
|
|
|
|
var basename = Path.basename(filepath)
|
2021-08-26 00:36:54 +02:00
|
|
|
var fileObj = {
|
2021-09-07 03:14:04 +02:00
|
|
|
filetype: getFileType(extname),
|
|
|
|
filename: basename,
|
2021-08-26 00:36:54 +02:00
|
|
|
path: relpath,
|
|
|
|
fullPath: filepath,
|
2021-09-07 03:14:04 +02:00
|
|
|
ext: extname
|
2021-08-26 00:36:54 +02:00
|
|
|
}
|
|
|
|
if (fileObj.filetype === 'audio') {
|
2021-09-07 03:14:04 +02:00
|
|
|
audiobook.audioFiles.push(fileObj)
|
2021-08-18 00:01:11 +02:00
|
|
|
} else {
|
2021-09-07 03:14:04 +02:00
|
|
|
audiobook.otherFiles.push(fileObj)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
})
|
2021-09-07 03:14:04 +02:00
|
|
|
return audiobook
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2021-09-07 03:14:04 +02:00
|
|
|
module.exports.getAudiobookFileData = getAudiobookFileData
|