2021-08-18 00:01:11 +02:00
|
|
|
const Path = require('path')
|
|
|
|
const dir = require('node-dir')
|
|
|
|
const Logger = require('../Logger')
|
2021-08-22 15:52:37 +02:00
|
|
|
const { cleanString } = require('./index')
|
2021-08-18 00:01:11 +02:00
|
|
|
|
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) {
|
|
|
|
console.error(err)
|
|
|
|
resolve(false)
|
|
|
|
}
|
|
|
|
resolve(res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07 03:14:04 +02:00
|
|
|
// 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
|
|
|
|
|
|
|
|
if (!path) {
|
|
|
|
Logger.error('Ignoring file in root dir', relpath)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 series = null
|
|
|
|
if (splitDir.length > 1) series = splitDir.shift()
|
|
|
|
var title = splitDir.shift()
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (!isNaN(publishYearMatch[1])) {
|
|
|
|
publishYear = publishYearMatch[1]
|
|
|
|
title = publishYearMatch[2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parseSubtitle && title.includes(' - ')) {
|
|
|
|
var splitOnSubtitle = title.split(' - ')
|
|
|
|
title = splitOnSubtitle.shift()
|
|
|
|
subtitle = splitOnSubtitle.join(' - ')
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
author,
|
|
|
|
title,
|
|
|
|
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/..
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getAllAudiobookFileData(abRootPath, serverSettings = {}) {
|
2021-09-05 02:58:39 +02:00
|
|
|
var parseSubtitle = !!serverSettings.scannerParseSubtitle
|
|
|
|
|
2021-08-20 02:14:24 +02:00
|
|
|
var paths = await getPaths(abRootPath)
|
2021-08-19 01:31:19 +02:00
|
|
|
var audiobooks = {}
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
paths.files.forEach((filepath) => {
|
2021-08-26 00:36:54 +02:00
|
|
|
var relpath = Path.normalize(filepath).replace(abRootPath, '').slice(1)
|
2021-09-07 03:14:04 +02:00
|
|
|
var parsed = Path.parse(relpath)
|
|
|
|
var path = parsed.dir
|
2021-08-20 02:14:24 +02:00
|
|
|
|
2021-09-07 03:14:04 +02:00
|
|
|
if (!audiobooks[path]) {
|
|
|
|
var audiobookData = getAudiobookDataFromFilepath(abRootPath, relpath, parseSubtitle)
|
|
|
|
if (!audiobookData) return
|
2021-08-23 21:08:54 +02:00
|
|
|
|
2021-09-07 03:14:04 +02:00
|
|
|
audiobooks[path] = {
|
|
|
|
...audiobookData,
|
|
|
|
audioFiles: [],
|
|
|
|
otherFiles: []
|
2021-08-20 02:14:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 03:14:04 +02:00
|
|
|
var fileObj = {
|
|
|
|
filetype: getFileType(parsed.ext),
|
|
|
|
filename: parsed.base,
|
|
|
|
path: relpath,
|
|
|
|
fullPath: filepath,
|
|
|
|
ext: parsed.ext
|
2021-09-05 02:58:39 +02:00
|
|
|
}
|
2021-09-07 03:14:04 +02:00
|
|
|
if (fileObj.filetype === 'audio') {
|
|
|
|
audiobooks[path].audioFiles.push(fileObj)
|
|
|
|
} else {
|
|
|
|
audiobooks[path].otherFiles.push(fileObj)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return Object.values(audiobooks)
|
|
|
|
}
|
|
|
|
module.exports.getAllAudiobookFileData = getAllAudiobookFileData
|
2021-09-05 02:58:39 +02:00
|
|
|
|
2021-09-07 03:14:04 +02:00
|
|
|
|
|
|
|
async function getAudiobookFileData(abRootPath, audiobookPath, serverSettings = {}) {
|
|
|
|
var parseSubtitle = !!serverSettings.scannerParseSubtitle
|
|
|
|
|
|
|
|
var paths = await getPaths(audiobookPath)
|
|
|
|
var audiobook = null
|
|
|
|
|
|
|
|
paths.files.forEach((filepath) => {
|
|
|
|
var relpath = Path.normalize(filepath).replace(abRootPath, '').slice(1)
|
|
|
|
|
|
|
|
if (!audiobook) {
|
|
|
|
var audiobookData = getAudiobookDataFromFilepath(abRootPath, relpath, parseSubtitle)
|
|
|
|
if (!audiobookData) return
|
|
|
|
|
|
|
|
audiobook = {
|
|
|
|
...audiobookData,
|
2021-08-26 00:36:54 +02:00
|
|
|
audioFiles: [],
|
2021-08-18 00:01:11 +02:00
|
|
|
otherFiles: []
|
|
|
|
}
|
|
|
|
}
|
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
|