2021-08-18 00:01:11 +02:00
|
|
|
const Path = require('path')
|
|
|
|
const dir = require('node-dir')
|
|
|
|
const Logger = require('../Logger')
|
|
|
|
|
|
|
|
const AUDIOBOOK_PARTS_FORMATS = ['m4b', 'mp3']
|
|
|
|
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)
|
|
|
|
if (AUDIOBOOK_PARTS_FORMATS.includes(ext_cleaned)) return 'abpart'
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
async function getAllAudiobookFiles(path) {
|
|
|
|
console.log('getAllAudiobooks', path)
|
|
|
|
var paths = await getPaths(path)
|
2021-08-19 01:31:19 +02:00
|
|
|
var audiobooks = {}
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
paths.files.forEach((filepath) => {
|
|
|
|
var relpath = filepath.replace(path, '').slice(1)
|
|
|
|
var pathformat = Path.parse(relpath)
|
|
|
|
var authordir = Path.dirname(pathformat.dir)
|
|
|
|
var bookdir = Path.basename(pathformat.dir)
|
2021-08-19 01:31:19 +02:00
|
|
|
if (!audiobooks[bookdir]) {
|
|
|
|
audiobooks[bookdir] = {
|
2021-08-18 00:01:11 +02:00
|
|
|
author: authordir,
|
|
|
|
title: bookdir,
|
|
|
|
path: pathformat.dir,
|
|
|
|
fullPath: Path.join(path, pathformat.dir),
|
|
|
|
parts: [],
|
|
|
|
otherFiles: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var filetype = getFileType(pathformat.ext)
|
|
|
|
if (filetype === 'abpart') {
|
2021-08-19 01:31:19 +02:00
|
|
|
audiobooks[bookdir].parts.push(pathformat.base)
|
2021-08-18 00:01:11 +02:00
|
|
|
} else {
|
2021-08-19 01:31:19 +02:00
|
|
|
var fileObj = {
|
|
|
|
filetype: filetype,
|
|
|
|
filename: pathformat.base,
|
|
|
|
path: relpath,
|
|
|
|
fullPath: filepath,
|
|
|
|
ext: pathformat.ext
|
|
|
|
}
|
|
|
|
audiobooks[bookdir].otherFiles.push(fileObj)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
})
|
2021-08-19 01:31:19 +02:00
|
|
|
return Object.values(audiobooks)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
module.exports.getAllAudiobookFiles = getAllAudiobookFiles
|