2021-08-18 00:01:11 +02:00
|
|
|
const Path = require('path')
|
2023-09-04 18:50:55 +02:00
|
|
|
const { filePathToPOSIX } = require('./fileUtils')
|
2021-10-02 01:42:48 +02:00
|
|
|
const globals = require('./globals')
|
2022-03-13 00:45:32 +01:00
|
|
|
const LibraryFile = require('../objects/files/LibraryFile')
|
2023-10-09 00:10:43 +02:00
|
|
|
const parseNameString = require('./parsers/parseNameString')
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef LibraryItemFilenameMetadata
|
|
|
|
* @property {string} title
|
|
|
|
* @property {string} subtitle Book mediaType only
|
2023-10-14 22:04:16 +02:00
|
|
|
* @property {string} asin Book mediaType only
|
2023-10-09 00:10:43 +02:00
|
|
|
* @property {string[]} authors Book mediaType only
|
|
|
|
* @property {string[]} narrators Book mediaType only
|
|
|
|
* @property {string} seriesName Book mediaType only
|
|
|
|
* @property {string} seriesSequence Book mediaType only
|
|
|
|
* @property {string} publishedYear Book mediaType only
|
|
|
|
*/
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2023-06-10 19:46:57 +02:00
|
|
|
function isMediaFile(mediaType, ext, audiobooksOnly = false) {
|
2022-05-25 02:06:44 +02:00
|
|
|
if (!ext) return false
|
2023-06-10 19:46:57 +02:00
|
|
|
const extclean = ext.slice(1).toLowerCase()
|
2024-09-04 00:04:58 +02:00
|
|
|
if (mediaType === 'podcast') return globals.SupportedAudioTypes.includes(extclean)
|
2023-06-10 19:46:57 +02:00
|
|
|
else if (audiobooksOnly) return globals.SupportedAudioTypes.includes(extclean)
|
2022-05-25 02:06:44 +02:00
|
|
|
return globals.SupportedAudioTypes.includes(extclean) || globals.SupportedEbookTypes.includes(extclean)
|
2021-09-18 01:40:30 +02:00
|
|
|
}
|
|
|
|
|
2023-06-10 19:46:57 +02:00
|
|
|
function checkFilepathIsAudioFile(filepath) {
|
|
|
|
const ext = Path.extname(filepath)
|
|
|
|
if (!ext) return false
|
|
|
|
const extclean = ext.slice(1).toLowerCase()
|
|
|
|
return globals.SupportedAudioTypes.includes(extclean)
|
|
|
|
}
|
|
|
|
module.exports.checkFilepathIsAudioFile = checkFilepathIsAudioFile
|
|
|
|
|
2023-08-26 23:33:27 +02:00
|
|
|
/**
|
2024-09-04 00:04:58 +02:00
|
|
|
* @param {string} mediaType
|
2024-12-04 23:30:35 +01:00
|
|
|
* @param {import('./fileUtils').FilePathItem[]} fileItems
|
2024-09-04 00:04:58 +02:00
|
|
|
* @param {boolean} [audiobooksOnly=false]
|
2023-08-26 23:33:27 +02:00
|
|
|
* @returns {Record<string,string[]>} map of files grouped into potential libarary item dirs
|
|
|
|
*/
|
2023-06-10 19:46:57 +02:00
|
|
|
function groupFileItemsIntoLibraryItemDirs(mediaType, fileItems, audiobooksOnly = false) {
|
2022-05-25 02:06:44 +02:00
|
|
|
// Step 1: Filter out non-book-media files in root dir (with depth of 0)
|
2024-09-04 00:04:58 +02:00
|
|
|
const itemsFiltered = fileItems.filter((i) => {
|
|
|
|
return i.deep > 0 || (mediaType === 'book' && isMediaFile(mediaType, i.extension, audiobooksOnly))
|
2022-05-25 02:06:44 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// Step 2: Seperate media files and other files
|
|
|
|
// - Directories without a media file will not be included
|
2024-12-04 23:30:35 +01:00
|
|
|
/** @type {import('./fileUtils').FilePathItem[]} */
|
2022-12-22 23:38:55 +01:00
|
|
|
const mediaFileItems = []
|
2024-12-04 23:30:35 +01:00
|
|
|
/** @type {import('./fileUtils').FilePathItem[]} */
|
2022-12-22 23:38:55 +01:00
|
|
|
const otherFileItems = []
|
2024-09-04 00:04:58 +02:00
|
|
|
itemsFiltered.forEach((item) => {
|
2023-06-10 19:46:57 +02:00
|
|
|
if (isMediaFile(mediaType, item.extension, audiobooksOnly)) mediaFileItems.push(item)
|
2022-05-25 02:06:44 +02:00
|
|
|
else otherFileItems.push(item)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Step 3: Group audio files in library items
|
2022-12-22 23:38:55 +01:00
|
|
|
const libraryItemGroup = {}
|
2022-05-25 02:06:44 +02:00
|
|
|
mediaFileItems.forEach((item) => {
|
2024-09-04 00:04:58 +02:00
|
|
|
const dirparts = item.reldirpath.split('/').filter((p) => !!p)
|
2022-12-22 23:38:55 +01:00
|
|
|
const numparts = dirparts.length
|
|
|
|
let _path = ''
|
2022-05-25 02:06:44 +02:00
|
|
|
|
|
|
|
if (!dirparts.length) {
|
|
|
|
// Media file in root
|
|
|
|
libraryItemGroup[item.name] = item.name
|
|
|
|
} else {
|
|
|
|
// Iterate over directories in path
|
|
|
|
for (let i = 0; i < numparts; i++) {
|
2022-12-22 23:38:55 +01:00
|
|
|
const dirpart = dirparts.shift()
|
2022-05-25 02:06:44 +02:00
|
|
|
_path = Path.posix.join(_path, dirpart)
|
|
|
|
|
2024-09-04 00:04:58 +02:00
|
|
|
if (libraryItemGroup[_path]) {
|
|
|
|
// Directory already has files, add file
|
2022-12-22 23:38:55 +01:00
|
|
|
const relpath = Path.posix.join(dirparts.join('/'), item.name)
|
2022-05-25 02:06:44 +02:00
|
|
|
libraryItemGroup[_path].push(relpath)
|
|
|
|
return
|
2024-09-04 00:04:58 +02:00
|
|
|
} else if (!dirparts.length) {
|
|
|
|
// This is the last directory, create group
|
2022-05-25 02:06:44 +02:00
|
|
|
libraryItemGroup[_path] = [item.name]
|
|
|
|
return
|
2024-12-02 03:41:38 +01:00
|
|
|
} else if (dirparts.length === 1 && /^(cd|dis[ck])\s*\d{1,3}$/i.test(dirparts[0])) {
|
2024-09-04 00:04:58 +02:00
|
|
|
// Next directory is the last and is a CD dir, create group
|
2022-05-25 02:06:44 +02:00
|
|
|
libraryItemGroup[_path] = [Path.posix.join(dirparts[0], item.name)]
|
|
|
|
return
|
2022-05-24 21:56:10 +02:00
|
|
|
}
|
2022-05-25 02:06:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Step 4: Add other files into library item groups
|
|
|
|
otherFileItems.forEach((item) => {
|
2022-12-22 23:38:55 +01:00
|
|
|
const dirparts = item.reldirpath.split('/')
|
|
|
|
const numparts = dirparts.length
|
|
|
|
let _path = ''
|
2022-05-25 02:06:44 +02:00
|
|
|
|
|
|
|
// Iterate over directories in path
|
|
|
|
for (let i = 0; i < numparts; i++) {
|
2022-12-22 23:38:55 +01:00
|
|
|
const dirpart = dirparts.shift()
|
2022-05-25 02:06:44 +02:00
|
|
|
_path = Path.posix.join(_path, dirpart)
|
2024-09-04 00:04:58 +02:00
|
|
|
if (libraryItemGroup[_path]) {
|
|
|
|
// Directory is audiobook group
|
2022-12-22 23:38:55 +01:00
|
|
|
const relpath = Path.posix.join(dirparts.join('/'), item.name)
|
2022-05-25 02:06:44 +02:00
|
|
|
libraryItemGroup[_path].push(relpath)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return libraryItemGroup
|
2021-09-11 02:55:02 +02:00
|
|
|
}
|
2023-08-26 23:33:27 +02:00
|
|
|
module.exports.groupFileItemsIntoLibraryItemDirs = groupFileItemsIntoLibraryItemDirs
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get LibraryFile from filepath
|
2024-09-04 00:04:58 +02:00
|
|
|
* @param {string} libraryItemPath
|
|
|
|
* @param {string[]} files
|
2023-08-26 23:33:27 +02:00
|
|
|
* @returns {import('../objects/files/LibraryFile')}
|
|
|
|
*/
|
|
|
|
function buildLibraryFile(libraryItemPath, files) {
|
2024-09-04 00:04:58 +02:00
|
|
|
return Promise.all(
|
|
|
|
files.map(async (file) => {
|
|
|
|
const filePath = Path.posix.join(libraryItemPath, file)
|
|
|
|
const newLibraryFile = new LibraryFile()
|
|
|
|
await newLibraryFile.setDataFromPath(filePath, file)
|
|
|
|
return newLibraryFile
|
|
|
|
})
|
|
|
|
)
|
2021-09-11 02:55:02 +02:00
|
|
|
}
|
2023-08-26 23:33:27 +02:00
|
|
|
module.exports.buildLibraryFile = buildLibraryFile
|
2021-09-11 02:55:02 +02:00
|
|
|
|
2023-10-09 00:10:43 +02:00
|
|
|
/**
|
|
|
|
* Get details parsed from filenames
|
2024-09-04 00:04:58 +02:00
|
|
|
*
|
|
|
|
* @param {string} relPath
|
|
|
|
* @param {boolean} parseSubtitle
|
2023-10-09 00:10:43 +02:00
|
|
|
* @returns {LibraryItemFilenameMetadata}
|
|
|
|
*/
|
|
|
|
function getBookDataFromDir(relPath, parseSubtitle = false) {
|
|
|
|
const splitDir = relPath.split('/')
|
2022-05-25 02:06:44 +02:00
|
|
|
|
|
|
|
var folder = splitDir.pop() // Audio files will always be in the directory named for the title
|
2024-09-04 00:04:58 +02:00
|
|
|
series = splitDir.length > 1 ? splitDir.pop() : null // If there are at least 2 more directories, next furthest will be the series
|
|
|
|
author = splitDir.length > 0 ? splitDir.pop() : null // There could be many more directories, but only the top 3 are used for naming /author/series/title/
|
2022-05-25 02:06:44 +02:00
|
|
|
|
|
|
|
// The may contain various other pieces of metadata, these functions extract it.
|
2023-10-14 22:04:16 +02:00
|
|
|
var [folder, asin] = getASIN(folder)
|
2022-05-25 02:06:44 +02:00
|
|
|
var [folder, narrators] = getNarrator(folder)
|
2022-05-25 03:49:45 +02:00
|
|
|
var [folder, sequence] = series ? getSequence(folder) : [folder, null]
|
2022-05-25 02:06:44 +02:00
|
|
|
var [folder, publishedYear] = getPublishedYear(folder)
|
2022-05-25 03:49:45 +02:00
|
|
|
var [title, subtitle] = parseSubtitle ? getSubtitle(folder) : [folder, null]
|
2022-05-25 02:06:44 +02:00
|
|
|
|
|
|
|
return {
|
2023-10-09 00:10:43 +02:00
|
|
|
title,
|
|
|
|
subtitle,
|
2023-10-14 22:04:16 +02:00
|
|
|
asin,
|
2023-10-09 00:10:43 +02:00
|
|
|
authors: parseNameString.parse(author)?.names || [],
|
|
|
|
narrators: parseNameString.parse(narrators)?.names || [],
|
|
|
|
seriesName: series,
|
|
|
|
seriesSequence: sequence,
|
|
|
|
publishedYear
|
2022-05-25 02:06:44 +02:00
|
|
|
}
|
2022-05-20 07:10:53 +02:00
|
|
|
}
|
2022-10-08 22:42:38 +02:00
|
|
|
module.exports.getBookDataFromDir = getBookDataFromDir
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2023-10-14 22:04:16 +02:00
|
|
|
/**
|
|
|
|
* Extract narrator from folder name
|
2024-09-04 00:04:58 +02:00
|
|
|
*
|
|
|
|
* @param {string} folder
|
2023-10-14 22:04:16 +02:00
|
|
|
* @returns {[string, string]} [folder, narrator]
|
|
|
|
*/
|
2022-05-20 07:10:53 +02:00
|
|
|
function getNarrator(folder) {
|
2022-05-25 02:06:44 +02:00
|
|
|
let pattern = /^(?<title>.*) \{(?<narrators>.*)\}$/
|
|
|
|
let match = folder.match(pattern)
|
|
|
|
return match ? [match.groups.title, match.groups.narrators] : [folder, null]
|
2022-05-20 07:10:53 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 22:04:16 +02:00
|
|
|
/**
|
|
|
|
* Extract series sequence from folder name
|
2024-09-04 00:04:58 +02:00
|
|
|
*
|
2023-10-14 22:04:16 +02:00
|
|
|
* @example
|
|
|
|
* 'Book 2 - Title - Subtitle'
|
|
|
|
* 'Title - Subtitle - Vol 12'
|
|
|
|
* 'Title - volume 9 - Subtitle'
|
|
|
|
* 'Vol. 3 Title Here - Subtitle'
|
|
|
|
* '1980 - Book 2 - Title'
|
|
|
|
* 'Volume 12. Title - Subtitle'
|
|
|
|
* '100 - Book Title'
|
|
|
|
* '6. Title'
|
|
|
|
* '0.5 - Book Title'
|
2024-09-04 00:04:58 +02:00
|
|
|
*
|
|
|
|
* @param {string} folder
|
2023-10-14 22:04:16 +02:00
|
|
|
* @returns {[string, string]} [folder, sequence]
|
|
|
|
*/
|
2022-05-25 01:21:58 +02:00
|
|
|
function getSequence(folder) {
|
2022-05-25 02:06:44 +02:00
|
|
|
// Matches a valid volume string. Also matches a book whose title starts with a 1 to 3 digit number. Will handle that later.
|
2022-10-08 22:42:38 +02:00
|
|
|
let pattern = /^(?<volumeLabel>vol\.? |volume |book )?(?<sequence>\d{0,3}(?:\.\d{1,2})?)(?<trailingDot>\.?)(?: (?<suffix>.*))?$/i
|
2022-05-25 02:06:44 +02:00
|
|
|
|
|
|
|
let volumeNumber = null
|
|
|
|
let parts = folder.split(' - ')
|
|
|
|
for (let i = 0; i < parts.length; i++) {
|
|
|
|
let match = parts[i].match(pattern)
|
|
|
|
// This excludes '101 Dalmations' but includes '101. Dalmations'
|
|
|
|
if (match && !(match.groups.suffix && !(match.groups.volumeLabel || match.groups.trailingDot))) {
|
2022-10-08 22:42:38 +02:00
|
|
|
volumeNumber = isNaN(match.groups.sequence) ? match.groups.sequence : Number(match.groups.sequence).toString()
|
2022-05-25 02:06:44 +02:00
|
|
|
parts[i] = match.groups.suffix
|
2024-09-04 00:04:58 +02:00
|
|
|
if (!parts[i]) {
|
|
|
|
parts.splice(i, 1)
|
|
|
|
}
|
2022-05-25 02:06:44 +02:00
|
|
|
break
|
2021-09-18 18:13:05 +02:00
|
|
|
}
|
2022-05-25 02:06:44 +02:00
|
|
|
}
|
2021-09-18 18:13:05 +02:00
|
|
|
|
2022-05-25 02:06:44 +02:00
|
|
|
folder = parts.join(' - ')
|
|
|
|
return [folder, volumeNumber]
|
2022-05-20 07:10:53 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 22:04:16 +02:00
|
|
|
/**
|
|
|
|
* Extract published year from folder name
|
2024-09-04 00:04:58 +02:00
|
|
|
*
|
|
|
|
* @param {string} folder
|
2023-10-14 22:04:16 +02:00
|
|
|
* @returns {[string, string]} [folder, publishedYear]
|
|
|
|
*/
|
2022-05-25 01:21:58 +02:00
|
|
|
function getPublishedYear(folder) {
|
2022-05-25 02:06:44 +02:00
|
|
|
var publishedYear = null
|
2022-05-20 07:55:00 +02:00
|
|
|
|
2022-05-25 02:06:44 +02:00
|
|
|
pattern = /^ *\(?([0-9]{4})\)? * - *(.+)/ //Matches #### - title or (####) - title
|
|
|
|
var match = folder.match(pattern)
|
|
|
|
if (match) {
|
|
|
|
publishedYear = match[1]
|
|
|
|
folder = match[2]
|
|
|
|
}
|
2022-05-20 07:55:00 +02:00
|
|
|
|
2022-05-25 02:06:44 +02:00
|
|
|
return [folder, publishedYear]
|
2022-05-20 04:42:45 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 22:04:16 +02:00
|
|
|
/**
|
|
|
|
* Extract subtitle from folder name
|
2024-09-04 00:04:58 +02:00
|
|
|
*
|
|
|
|
* @param {string} folder
|
2023-10-14 22:04:16 +02:00
|
|
|
* @returns {[string, string]} [folder, subtitle]
|
|
|
|
*/
|
2022-05-25 01:21:58 +02:00
|
|
|
function getSubtitle(folder) {
|
2022-05-25 02:06:44 +02:00
|
|
|
// Subtitle is everything after " - "
|
|
|
|
var splitTitle = folder.split(' - ')
|
|
|
|
return [splitTitle.shift(), splitTitle.join(' - ')]
|
2022-05-20 10:03:36 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 22:04:16 +02:00
|
|
|
/**
|
|
|
|
* Extract asin from folder name
|
2024-09-04 00:04:58 +02:00
|
|
|
*
|
|
|
|
* @param {string} folder
|
2023-10-14 22:04:16 +02:00
|
|
|
* @returns {[string, string]} [folder, asin]
|
|
|
|
*/
|
|
|
|
function getASIN(folder) {
|
|
|
|
let asin = null
|
|
|
|
|
|
|
|
let pattern = /(?: |^)\[([A-Z0-9]{10})](?= |$)/ // Matches "[B0015T963C]"
|
|
|
|
const match = folder.match(pattern)
|
|
|
|
if (match) {
|
|
|
|
asin = match[1]
|
|
|
|
folder = folder.replace(match[0], '')
|
|
|
|
}
|
|
|
|
return [folder.trim(), asin]
|
|
|
|
}
|
|
|
|
|
2023-10-09 00:10:43 +02:00
|
|
|
/**
|
2024-09-04 00:04:58 +02:00
|
|
|
*
|
|
|
|
* @param {string} relPath
|
2023-10-09 00:10:43 +02:00
|
|
|
* @returns {LibraryItemFilenameMetadata}
|
|
|
|
*/
|
|
|
|
function getPodcastDataFromDir(relPath) {
|
2022-12-22 23:38:55 +01:00
|
|
|
const splitDir = relPath.split('/')
|
2022-05-25 02:06:44 +02:00
|
|
|
|
|
|
|
// Audio files will always be in the directory named for the title
|
2022-12-22 23:38:55 +01:00
|
|
|
const title = splitDir.pop()
|
2022-05-25 02:06:44 +02:00
|
|
|
return {
|
2023-10-09 00:10:43 +02:00
|
|
|
title
|
2022-12-22 23:38:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 00:10:43 +02:00
|
|
|
/**
|
2024-09-04 00:04:58 +02:00
|
|
|
*
|
|
|
|
* @param {string} libraryMediaType
|
|
|
|
* @param {string} folderPath
|
|
|
|
* @param {string} relPath
|
2023-10-09 00:10:43 +02:00
|
|
|
* @returns {{ mediaMetadata: LibraryItemFilenameMetadata, relPath: string, path: string}}
|
|
|
|
*/
|
2023-04-12 23:45:52 +02:00
|
|
|
function getDataFromMediaDir(libraryMediaType, folderPath, relPath) {
|
2023-10-09 00:10:43 +02:00
|
|
|
relPath = filePathToPOSIX(relPath)
|
|
|
|
let fullPath = Path.posix.join(folderPath, relPath)
|
|
|
|
let mediaMetadata = null
|
|
|
|
|
2023-01-02 23:35:39 +01:00
|
|
|
if (libraryMediaType === 'podcast') {
|
2023-10-09 00:10:43 +02:00
|
|
|
mediaMetadata = getPodcastDataFromDir(relPath)
|
2024-09-04 00:04:58 +02:00
|
|
|
} else {
|
|
|
|
// book
|
2023-10-09 00:10:43 +02:00
|
|
|
mediaMetadata = getBookDataFromDir(relPath, !!global.ServerSettings.scannerParseSubtitle)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
mediaMetadata,
|
|
|
|
relPath,
|
|
|
|
path: fullPath
|
2022-05-25 02:06:44 +02:00
|
|
|
}
|
2022-03-13 00:45:32 +01:00
|
|
|
}
|
2023-08-26 23:33:27 +02:00
|
|
|
module.exports.getDataFromMediaDir = getDataFromMediaDir
|