2021-08-18 00:01:11 +02:00
|
|
|
const Path = require('path')
|
2021-10-11 02:29:22 +02:00
|
|
|
const fs = require('fs-extra')
|
2021-08-18 00:01:11 +02:00
|
|
|
const Logger = require('../Logger')
|
2022-02-28 01:07:36 +01:00
|
|
|
const { recurseFiles, getFileTimestampsWithIno } = require('./fileUtils')
|
2021-10-02 01:42:48 +02:00
|
|
|
const globals = require('./globals')
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2021-10-10 23:36:21 +02:00
|
|
|
function isBookFile(path) {
|
2021-09-18 01:40:30 +02:00
|
|
|
if (!path) return false
|
|
|
|
var ext = Path.extname(path)
|
|
|
|
if (!ext) return false
|
2021-10-10 23:36:21 +02:00
|
|
|
var extclean = ext.slice(1).toLowerCase()
|
|
|
|
return globals.SupportedAudioTypes.includes(extclean) || globals.SupportedEbookTypes.includes(extclean)
|
2021-09-18 01:40:30 +02:00
|
|
|
}
|
|
|
|
|
2021-11-06 23:26:44 +01:00
|
|
|
// TODO: Function needs to be re-done
|
2021-10-05 05:11:42 +02:00
|
|
|
// Input: array of relative file paths
|
|
|
|
// Output: map of files grouped into potential audiobook dirs
|
2021-11-26 01:39:02 +01:00
|
|
|
function groupFilesIntoAudiobookPaths(paths) {
|
2021-11-06 23:26:44 +01:00
|
|
|
// Step 1: Clean path, Remove leading "/", Filter out files in root dir
|
|
|
|
var pathsFiltered = paths.map(path => {
|
|
|
|
return path.startsWith('/') ? path.slice(1) : path
|
|
|
|
}).filter(path => Path.parse(path).dir)
|
2021-09-11 02:55:02 +02:00
|
|
|
|
|
|
|
// Step 2: Sort by least number of directories
|
|
|
|
pathsFiltered.sort((a, b) => {
|
2021-11-06 23:26:44 +01:00
|
|
|
var pathsA = Path.dirname(a).split('/').length
|
|
|
|
var pathsB = Path.dirname(b).split('/').length
|
2021-09-11 02:55:02 +02:00
|
|
|
return pathsA - pathsB
|
|
|
|
})
|
|
|
|
|
2021-11-06 23:26:44 +01:00
|
|
|
// Step 3: Group files in dirs
|
|
|
|
var audiobookGroup = {}
|
|
|
|
pathsFiltered.forEach((path) => {
|
|
|
|
var dirparts = Path.dirname(path).split('/')
|
|
|
|
var numparts = dirparts.length
|
|
|
|
var _path = ''
|
|
|
|
|
|
|
|
// Iterate over directories in path
|
|
|
|
for (let i = 0; i < numparts; i++) {
|
|
|
|
var dirpart = dirparts.shift()
|
|
|
|
_path = Path.posix.join(_path, dirpart)
|
|
|
|
|
|
|
|
if (audiobookGroup[_path]) { // Directory already has files, add file
|
|
|
|
var relpath = Path.posix.join(dirparts.join('/'), Path.basename(path))
|
|
|
|
audiobookGroup[_path].push(relpath)
|
|
|
|
return
|
|
|
|
} else if (!dirparts.length) { // This is the last directory, create group
|
|
|
|
audiobookGroup[_path] = [Path.basename(path)]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return audiobookGroup
|
|
|
|
}
|
|
|
|
module.exports.groupFilesIntoAudiobookPaths = groupFilesIntoAudiobookPaths
|
|
|
|
|
|
|
|
// Input: array of relative file items (see recurseFiles)
|
|
|
|
// Output: map of files grouped into potential audiobook dirs
|
|
|
|
function groupFileItemsIntoBooks(fileItems) {
|
|
|
|
// Step 1: Filter out files in root dir (with depth of 0)
|
|
|
|
var itemsFiltered = fileItems.filter(i => i.deep > 0)
|
|
|
|
|
|
|
|
// Step 2: Seperate audio/ebook files and other files
|
|
|
|
// - Directories without an audio or ebook file will not be included
|
|
|
|
var bookFileItems = []
|
|
|
|
var otherFileItems = []
|
|
|
|
itemsFiltered.forEach(item => {
|
|
|
|
if (isBookFile(item.fullpath)) bookFileItems.push(item)
|
|
|
|
else otherFileItems.push(item)
|
2021-09-18 01:40:30 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// Step 3: Group audio files in audiobooks
|
2021-09-11 02:55:02 +02:00
|
|
|
var audiobookGroup = {}
|
2021-11-06 23:26:44 +01:00
|
|
|
bookFileItems.forEach((item) => {
|
|
|
|
var dirparts = item.reldirpath.split('/')
|
2021-09-11 02:55:02 +02:00
|
|
|
var numparts = dirparts.length
|
|
|
|
var _path = ''
|
2021-09-18 01:40:30 +02:00
|
|
|
|
|
|
|
// Iterate over directories in path
|
2021-09-11 02:55:02 +02:00
|
|
|
for (let i = 0; i < numparts; i++) {
|
|
|
|
var dirpart = dirparts.shift()
|
2021-11-06 23:26:44 +01:00
|
|
|
_path = Path.posix.join(_path, dirpart)
|
2021-09-18 01:40:30 +02:00
|
|
|
|
|
|
|
if (audiobookGroup[_path]) { // Directory already has files, add file
|
2021-11-06 23:26:44 +01:00
|
|
|
var relpath = Path.posix.join(dirparts.join('/'), item.name)
|
2021-09-11 02:55:02 +02:00
|
|
|
audiobookGroup[_path].push(relpath)
|
|
|
|
return
|
2021-09-18 01:40:30 +02:00
|
|
|
} else if (!dirparts.length) { // This is the last directory, create group
|
2021-11-06 23:26:44 +01:00
|
|
|
audiobookGroup[_path] = [item.name]
|
2021-09-11 02:55:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-09-18 01:40:30 +02:00
|
|
|
|
|
|
|
// Step 4: Add other files into audiobook groups
|
2021-11-06 23:26:44 +01:00
|
|
|
otherFileItems.forEach((item) => {
|
|
|
|
var dirparts = item.reldirpath.split('/')
|
2021-09-18 01:40:30 +02:00
|
|
|
var numparts = dirparts.length
|
|
|
|
var _path = ''
|
|
|
|
|
|
|
|
// Iterate over directories in path
|
|
|
|
for (let i = 0; i < numparts; i++) {
|
|
|
|
var dirpart = dirparts.shift()
|
2021-11-06 23:26:44 +01:00
|
|
|
_path = Path.posix.join(_path, dirpart)
|
2021-09-18 01:40:30 +02:00
|
|
|
if (audiobookGroup[_path]) { // Directory is audiobook group
|
2021-11-06 23:26:44 +01:00
|
|
|
var relpath = Path.posix.join(dirparts.join('/'), item.name)
|
2021-09-18 01:40:30 +02:00
|
|
|
audiobookGroup[_path].push(relpath)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-09-11 02:55:02 +02:00
|
|
|
return audiobookGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanFileObjects(basepath, abrelpath, files) {
|
2022-02-28 01:07:36 +01:00
|
|
|
return Promise.all(files.map(async (file) => {
|
|
|
|
var fullPath = Path.posix.join(basepath, file)
|
|
|
|
var fileTsData = await getFileTimestampsWithIno(fullPath)
|
|
|
|
|
2021-09-11 02:55:02 +02:00
|
|
|
var ext = Path.extname(file)
|
|
|
|
return {
|
|
|
|
filetype: getFileType(ext),
|
|
|
|
filename: Path.basename(file),
|
2021-11-06 23:26:44 +01:00
|
|
|
path: Path.posix.join(abrelpath, file), // /AUDIOBOOK/PATH/filename.mp3
|
2022-02-28 01:07:36 +01:00
|
|
|
fullPath, // /audiobooks/AUDIOBOOK/PATH/filename.mp3
|
|
|
|
ext: ext,
|
|
|
|
...fileTsData
|
2021-09-11 02:55:02 +02:00
|
|
|
}
|
2022-02-28 01:07:36 +01:00
|
|
|
}))
|
2021-09-11 02:55:02 +02:00
|
|
|
}
|
|
|
|
|
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-10-02 01:42:48 +02:00
|
|
|
if (globals.SupportedAudioTypes.includes(ext_cleaned)) return 'audio'
|
|
|
|
if (globals.SupportedImageTypes.includes(ext_cleaned)) return 'image'
|
|
|
|
if (globals.SupportedEbookTypes.includes(ext_cleaned)) return 'ebook'
|
2021-11-10 00:54:28 +01:00
|
|
|
if (ext_cleaned === 'nfo') return 'info'
|
|
|
|
if (ext_cleaned === 'txt') return 'text'
|
|
|
|
if (ext_cleaned === 'opf') return 'opf'
|
2021-08-19 01:31:19 +02:00
|
|
|
return 'unknown'
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
// Scan folder
|
|
|
|
async function scanRootDir(folder, serverSettings = {}) {
|
2021-11-06 23:26:44 +01:00
|
|
|
var folderPath = folder.fullPath.replace(/\\/g, '/')
|
2021-09-11 02:55:02 +02:00
|
|
|
var parseSubtitle = !!serverSettings.scannerParseSubtitle
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2021-10-11 02:29:22 +02:00
|
|
|
var pathExists = await fs.pathExists(folderPath)
|
|
|
|
if (!pathExists) {
|
|
|
|
Logger.error(`[scandir] Invalid folder path does not exist "${folderPath}"`)
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2021-11-06 23:26:44 +01:00
|
|
|
var fileItems = await recurseFiles(folderPath)
|
2021-09-11 02:55:02 +02:00
|
|
|
|
2021-11-06 23:26:44 +01:00
|
|
|
var audiobookGrouping = groupFileItemsIntoBooks(fileItems)
|
2021-09-11 02:55:02 +02:00
|
|
|
|
|
|
|
if (!Object.keys(audiobookGrouping).length) {
|
2021-11-06 23:26:44 +01:00
|
|
|
Logger.error('Root path has no books', fileItems.length)
|
2021-09-11 02:55:02 +02:00
|
|
|
return []
|
2021-09-07 03:14:04 +02:00
|
|
|
}
|
|
|
|
|
2021-09-11 02:55:02 +02:00
|
|
|
var audiobooks = []
|
|
|
|
for (const audiobookPath in audiobookGrouping) {
|
2021-10-05 05:11:42 +02:00
|
|
|
var audiobookData = getAudiobookDataFromDir(folderPath, audiobookPath, parseSubtitle)
|
2021-09-11 02:55:02 +02:00
|
|
|
|
2022-02-28 01:07:36 +01:00
|
|
|
var fileObjs = await cleanFileObjects(audiobookData.fullPath, audiobookPath, audiobookGrouping[audiobookPath])
|
|
|
|
var audiobookFolderStats = await getFileTimestampsWithIno(audiobookData.fullPath)
|
2021-09-11 02:55:02 +02:00
|
|
|
audiobooks.push({
|
2021-10-05 05:11:42 +02:00
|
|
|
folderId: folder.id,
|
|
|
|
libraryId: folder.libraryId,
|
2022-02-28 01:07:36 +01:00
|
|
|
ino: audiobookFolderStats.ino,
|
|
|
|
mtimeMs: audiobookFolderStats.mtimeMs || 0,
|
|
|
|
ctimeMs: audiobookFolderStats.ctimeMs || 0,
|
|
|
|
birthtimeMs: audiobookFolderStats.birthtimeMs || 0,
|
2021-09-11 02:55:02 +02:00
|
|
|
...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
|
2021-10-05 05:11:42 +02:00
|
|
|
function getAudiobookDataFromDir(folderPath, dir, parseSubtitle = false) {
|
2021-11-06 23:26:44 +01:00
|
|
|
dir = dir.replace(/\\/g, '/')
|
|
|
|
var splitDir = dir.split('/')
|
2021-09-11 02:55:02 +02:00
|
|
|
|
|
|
|
// 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-18 18:13:05 +02:00
|
|
|
// If in a series directory check for volume number match
|
2022-02-27 23:52:01 +01:00
|
|
|
/* ACCEPTS
|
2021-09-18 18:13:05 +02:00
|
|
|
Book 2 - Title Here - Subtitle Here
|
|
|
|
Title Here - Subtitle Here - Vol 12
|
|
|
|
Title Here - volume 9 - Subtitle Here
|
|
|
|
Vol. 3 Title Here - Subtitle Here
|
|
|
|
1980 - Book 2-Title Here
|
|
|
|
Title Here-Volume 999-Subtitle Here
|
2022-02-27 23:52:01 +01:00
|
|
|
2 - Book Title
|
|
|
|
100 - Book Title
|
|
|
|
0.5 - Book Title
|
2021-09-18 18:13:05 +02:00
|
|
|
*/
|
|
|
|
var volumeNumber = null
|
|
|
|
if (series) {
|
2022-02-27 23:52:01 +01:00
|
|
|
// Added 1.7.1: If title starts with a # that is 3 digits or less (or w/ 2 decimal), then use as volume number
|
|
|
|
var volumeMatch = title.match(/^(\d{1,3}(?:\.\d{1,2})?) - ./)
|
|
|
|
if (volumeMatch && volumeMatch.length > 1) {
|
|
|
|
volumeNumber = volumeMatch[1]
|
|
|
|
title = title.replace(`${volumeNumber} - `, '')
|
|
|
|
} else {
|
|
|
|
// Match volumes with decimal (OLD: /(-? ?)\b((?:Book|Vol.?|Volume) (\d{1,3}))\b( ?-?)/i)
|
|
|
|
var volumeMatch = title.match(/(-? ?)\b((?:Book|Vol.?|Volume) (\d{0,3}(?:\.\d{1,2})?))\b( ?-?)/i)
|
|
|
|
if (volumeMatch && volumeMatch.length > 3 && volumeMatch[2] && volumeMatch[3]) {
|
|
|
|
volumeNumber = volumeMatch[3]
|
|
|
|
var replaceChunk = volumeMatch[2]
|
|
|
|
|
|
|
|
// "1980 - Book 2-Title Here"
|
|
|
|
// Group 1 would be "- "
|
|
|
|
// Group 3 would be "-"
|
|
|
|
// Only remove the first group
|
|
|
|
if (volumeMatch[1]) {
|
|
|
|
replaceChunk = volumeMatch[1] + replaceChunk
|
|
|
|
} else if (volumeMatch[4]) {
|
|
|
|
replaceChunk += volumeMatch[4]
|
|
|
|
}
|
|
|
|
title = title.replace(replaceChunk, '').trim()
|
2021-09-18 18:13:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-18 19:45:34 +02:00
|
|
|
|
|
|
|
var publishYear = null
|
2021-10-24 03:31:48 +02:00
|
|
|
// If Title is of format 1999 OR (1999) - Title, then use 1999 as publish year
|
|
|
|
var publishYearMatch = title.match(/^(\(?[0-9]{4}\)?) - (.+)/)
|
|
|
|
if (publishYearMatch && publishYearMatch.length > 2 && publishYearMatch[1]) {
|
|
|
|
// Strip parentheses
|
|
|
|
if (publishYearMatch[1].startsWith('(') && publishYearMatch[1].endsWith(')')) {
|
|
|
|
publishYearMatch[1] = publishYearMatch[1].slice(1, -1)
|
|
|
|
}
|
2021-09-18 19:45:34 +02:00
|
|
|
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
|
2021-09-18 18:13:05 +02:00
|
|
|
// Subtitle is everything after " - "
|
2021-09-11 02:55:02 +02:00
|
|
|
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,
|
2021-09-18 18:13:05 +02:00
|
|
|
volumeNumber,
|
2021-09-07 03:14:04 +02:00
|
|
|
publishYear,
|
2021-09-11 02:55:02 +02:00
|
|
|
path: dir, // relative audiobook path i.e. /Author Name/Book Name/..
|
2021-11-06 23:26:44 +01:00
|
|
|
fullPath: Path.posix.join(folderPath, dir) // i.e. /audiobook/Author Name/Book Name/..
|
2021-09-07 03:14:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
async function getAudiobookFileData(folder, audiobookPath, serverSettings = {}) {
|
2021-09-07 03:14:04 +02:00
|
|
|
var parseSubtitle = !!serverSettings.scannerParseSubtitle
|
|
|
|
|
2021-11-22 03:00:40 +01:00
|
|
|
var fileItems = await recurseFiles(audiobookPath, folder.fullPath)
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2021-11-06 23:26:44 +01:00
|
|
|
audiobookPath = audiobookPath.replace(/\\/g, '/')
|
|
|
|
var folderFullPath = folder.fullPath.replace(/\\/g, '/')
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2021-11-06 23:26:44 +01:00
|
|
|
var audiobookDir = audiobookPath.replace(folderFullPath, '').slice(1)
|
|
|
|
var audiobookData = getAudiobookDataFromDir(folderFullPath, audiobookDir, parseSubtitle)
|
2022-02-28 01:07:36 +01:00
|
|
|
var audiobookFolderStats = await getFileTimestampsWithIno(audiobookData.fullPath)
|
2021-09-11 02:55:02 +02:00
|
|
|
var audiobook = {
|
2022-02-28 01:07:36 +01:00
|
|
|
ino: audiobookFolderStats.ino,
|
|
|
|
mtimeMs: audiobookFolderStats.mtimeMs || 0,
|
|
|
|
ctimeMs: audiobookFolderStats.ctimeMs || 0,
|
|
|
|
birthtimeMs: audiobookFolderStats.birthtimeMs || 0,
|
2021-10-05 05:11:42 +02:00
|
|
|
folderId: folder.id,
|
|
|
|
libraryId: folder.libraryId,
|
2021-09-11 02:55:02 +02:00
|
|
|
...audiobookData,
|
|
|
|
audioFiles: [],
|
|
|
|
otherFiles: []
|
|
|
|
}
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2021-11-06 23:26:44 +01:00
|
|
|
for (let i = 0; i < fileItems.length; i++) {
|
|
|
|
var fileItem = fileItems[i]
|
2021-09-27 13:52:21 +02:00
|
|
|
|
2022-02-28 01:07:36 +01:00
|
|
|
var fileStatData = await getFileTimestampsWithIno(fileItem.fullpath)
|
2021-08-26 00:36:54 +02:00
|
|
|
var fileObj = {
|
2021-11-06 23:26:44 +01:00
|
|
|
filetype: getFileType(fileItem.extension),
|
|
|
|
filename: fileItem.name,
|
|
|
|
path: fileItem.path,
|
|
|
|
fullPath: fileItem.fullpath,
|
2022-02-28 01:07:36 +01:00
|
|
|
ext: fileItem.extension,
|
|
|
|
...fileStatData
|
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-27 13:52:21 +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
|