mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-07-16 04:14:58 +02:00
Update scanner v3, add isActive support for users
This commit is contained in:
parent
394d312282
commit
beaa1e14bb
13 changed files with 230 additions and 160 deletions
|
@ -1,7 +1,6 @@
|
|||
const Path = require('path')
|
||||
const dir = require('node-dir')
|
||||
const Logger = require('../Logger')
|
||||
const { cleanString } = require('./index')
|
||||
|
||||
const AUDIO_FORMATS = ['m4b', 'mp3', 'm4a']
|
||||
const INFO_FORMATS = ['nfo']
|
||||
|
@ -12,7 +11,7 @@ function getPaths(path) {
|
|||
return new Promise((resolve) => {
|
||||
dir.paths(path, function (err, res) {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
Logger.error(err)
|
||||
resolve(false)
|
||||
}
|
||||
resolve(res)
|
||||
|
@ -20,6 +19,54 @@ function getPaths(path) {
|
|||
})
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function getFileType(ext) {
|
||||
var ext_cleaned = ext.toLowerCase()
|
||||
if (ext_cleaned.startsWith('.')) ext_cleaned = ext_cleaned.slice(1)
|
||||
|
@ -30,27 +77,53 @@ function getFileType(ext) {
|
|||
return 'unknown'
|
||||
}
|
||||
|
||||
// Input relative filepath, output all details that can be parsed
|
||||
function getAudiobookDataFromFilepath(abRootPath, relpath, parseSubtitle = false) {
|
||||
var pathformat = Path.parse(relpath)
|
||||
var path = pathformat.dir
|
||||
// Primary scan: abRootPath is /audiobooks
|
||||
async function scanRootDir(abRootPath, serverSettings = {}) {
|
||||
var parseSubtitle = !!serverSettings.scannerParseSubtitle
|
||||
|
||||
if (!path) {
|
||||
Logger.error('Ignoring file in root dir', relpath)
|
||||
return null
|
||||
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 []
|
||||
}
|
||||
|
||||
// If relative file directory has 3 folders, then the middle folder will be series
|
||||
var splitDir = path.split(Path.sep)
|
||||
var author = null
|
||||
if (splitDir.length > 1) author = splitDir.shift()
|
||||
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()
|
||||
var series = null
|
||||
if (splitDir.length > 1) series = splitDir.shift()
|
||||
var title = splitDir.shift()
|
||||
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/
|
||||
|
||||
|
||||
var publishYear = null
|
||||
var subtitle = null
|
||||
|
||||
// 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) {
|
||||
|
@ -60,6 +133,8 @@ function getAudiobookDataFromFilepath(abRootPath, relpath, parseSubtitle = false
|
|||
}
|
||||
}
|
||||
|
||||
// Subtitle can be parsed from the title if user enabled
|
||||
var subtitle = null
|
||||
if (parseSubtitle && title.includes(' - ')) {
|
||||
var splitOnSubtitle = title.split(' - ')
|
||||
title = splitOnSubtitle.shift()
|
||||
|
@ -72,71 +147,34 @@ function getAudiobookDataFromFilepath(abRootPath, relpath, parseSubtitle = false
|
|||
subtitle,
|
||||
series,
|
||||
publishYear,
|
||||
path, // relative audiobook path i.e. /Author Name/Book Name/..
|
||||
fullPath: Path.join(abRootPath, path) // i.e. /audiobook/Author Name/Book Name/..
|
||||
path: dir, // relative audiobook path i.e. /Author Name/Book Name/..
|
||||
fullPath: Path.join(abRootPath, dir) // i.e. /audiobook/Author Name/Book Name/..
|
||||
}
|
||||
}
|
||||
|
||||
async function getAllAudiobookFileData(abRootPath, serverSettings = {}) {
|
||||
var parseSubtitle = !!serverSettings.scannerParseSubtitle
|
||||
|
||||
var paths = await getPaths(abRootPath)
|
||||
var audiobooks = {}
|
||||
|
||||
paths.files.forEach((filepath) => {
|
||||
var relpath = Path.normalize(filepath).replace(abRootPath, '').slice(1)
|
||||
var parsed = Path.parse(relpath)
|
||||
var path = parsed.dir
|
||||
|
||||
if (!audiobooks[path]) {
|
||||
var audiobookData = getAudiobookDataFromFilepath(abRootPath, relpath, parseSubtitle)
|
||||
if (!audiobookData) return
|
||||
|
||||
audiobooks[path] = {
|
||||
...audiobookData,
|
||||
audioFiles: [],
|
||||
otherFiles: []
|
||||
}
|
||||
}
|
||||
|
||||
var fileObj = {
|
||||
filetype: getFileType(parsed.ext),
|
||||
filename: parsed.base,
|
||||
path: relpath,
|
||||
fullPath: filepath,
|
||||
ext: parsed.ext
|
||||
}
|
||||
if (fileObj.filetype === 'audio') {
|
||||
audiobooks[path].audioFiles.push(fileObj)
|
||||
} else {
|
||||
audiobooks[path].otherFiles.push(fileObj)
|
||||
}
|
||||
})
|
||||
return Object.values(audiobooks)
|
||||
}
|
||||
module.exports.getAllAudiobookFileData = getAllAudiobookFileData
|
||||
|
||||
|
||||
async function getAudiobookFileData(abRootPath, audiobookPath, serverSettings = {}) {
|
||||
var parseSubtitle = !!serverSettings.scannerParseSubtitle
|
||||
|
||||
var paths = await getPaths(audiobookPath)
|
||||
var audiobook = null
|
||||
var filepaths = paths.files
|
||||
|
||||
paths.files.forEach((filepath) => {
|
||||
// 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
|
||||
})
|
||||
|
||||
var audiobookDir = Path.normalize(audiobookPath).replace(abRootPath, '').slice(1)
|
||||
var audiobookData = getAudiobookDataFromDir(abRootPath, audiobookDir, parseSubtitle)
|
||||
var audiobook = {
|
||||
...audiobookData,
|
||||
audioFiles: [],
|
||||
otherFiles: []
|
||||
}
|
||||
|
||||
filepaths.forEach((filepath) => {
|
||||
var relpath = Path.normalize(filepath).replace(abRootPath, '').slice(1)
|
||||
|
||||
if (!audiobook) {
|
||||
var audiobookData = getAudiobookDataFromFilepath(abRootPath, relpath, parseSubtitle)
|
||||
if (!audiobookData) return
|
||||
|
||||
audiobook = {
|
||||
...audiobookData,
|
||||
audioFiles: [],
|
||||
otherFiles: []
|
||||
}
|
||||
}
|
||||
|
||||
var extname = Path.extname(filepath)
|
||||
var basename = Path.basename(filepath)
|
||||
var fileObj = {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue