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')
|
2022-03-13 00:45:32 +01:00
|
|
|
const LibraryFile = require('../objects/files/LibraryFile')
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
function isMediaFile(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
|
2022-03-13 00:45:32 +01:00
|
|
|
// Output: map of files grouped into potential item dirs
|
|
|
|
function groupFilesIntoLibraryItemPaths(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
|
2022-03-13 00:45:32 +01:00
|
|
|
var itemGroup = {}
|
2021-11-06 23:26:44 +01:00
|
|
|
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)
|
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
if (itemGroup[_path]) { // Directory already has files, add file
|
2021-11-06 23:26:44 +01:00
|
|
|
var relpath = Path.posix.join(dirparts.join('/'), Path.basename(path))
|
2022-03-13 00:45:32 +01:00
|
|
|
itemGroup[_path].push(relpath)
|
2021-11-06 23:26:44 +01:00
|
|
|
return
|
|
|
|
} else if (!dirparts.length) { // This is the last directory, create group
|
2022-03-13 00:45:32 +01:00
|
|
|
itemGroup[_path] = [Path.basename(path)]
|
2021-11-06 23:26:44 +01:00
|
|
|
return
|
2022-03-07 23:22:20 +01:00
|
|
|
} else if (dirparts.length === 1 && /^cd\d{1,3}$/i.test(dirparts[0])) { // Next directory is the last and is a CD dir, create group
|
2022-03-13 00:45:32 +01:00
|
|
|
itemGroup[_path] = [Path.posix.join(dirparts[0], Path.basename(path))]
|
2022-03-07 23:22:20 +01:00
|
|
|
return
|
2021-11-06 23:26:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-03-13 00:45:32 +01:00
|
|
|
return itemGroup
|
2021-11-06 23:26:44 +01:00
|
|
|
}
|
2022-03-13 00:45:32 +01:00
|
|
|
module.exports.groupFilesIntoLibraryItemPaths = groupFilesIntoLibraryItemPaths
|
2021-11-06 23:26:44 +01:00
|
|
|
|
|
|
|
// Input: array of relative file items (see recurseFiles)
|
2022-03-13 00:45:32 +01:00
|
|
|
// Output: map of files grouped into potential libarary item dirs
|
|
|
|
function groupFileItemsIntoLibraryItemDirs(fileItems) {
|
2021-11-06 23:26:44 +01:00
|
|
|
// Step 1: Filter out files in root dir (with depth of 0)
|
|
|
|
var itemsFiltered = fileItems.filter(i => i.deep > 0)
|
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
// Step 2: Seperate media files and other files
|
|
|
|
// - Directories without a media file will not be included
|
|
|
|
var mediaFileItems = []
|
2021-11-06 23:26:44 +01:00
|
|
|
var otherFileItems = []
|
|
|
|
itemsFiltered.forEach(item => {
|
2022-03-13 00:45:32 +01:00
|
|
|
if (isMediaFile(item.fullpath)) mediaFileItems.push(item)
|
2021-11-06 23:26:44 +01:00
|
|
|
else otherFileItems.push(item)
|
2021-09-18 01:40:30 +02:00
|
|
|
})
|
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
// Step 3: Group audio files in library items
|
|
|
|
var libraryItemGroup = {}
|
|
|
|
mediaFileItems.forEach((item) => {
|
2021-11-06 23:26:44 +01:00
|
|
|
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
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
if (libraryItemGroup[_path]) { // Directory already has files, add file
|
2021-11-06 23:26:44 +01:00
|
|
|
var relpath = Path.posix.join(dirparts.join('/'), item.name)
|
2022-03-13 00:45:32 +01:00
|
|
|
libraryItemGroup[_path].push(relpath)
|
2021-09-11 02:55:02 +02:00
|
|
|
return
|
2021-09-18 01:40:30 +02:00
|
|
|
} else if (!dirparts.length) { // This is the last directory, create group
|
2022-03-13 00:45:32 +01:00
|
|
|
libraryItemGroup[_path] = [item.name]
|
2021-09-11 02:55:02 +02:00
|
|
|
return
|
2022-03-07 23:22:20 +01:00
|
|
|
} else if (dirparts.length === 1 && /^cd\d{1,3}$/i.test(dirparts[0])) { // Next directory is the last and is a CD dir, create group
|
2022-03-13 00:45:32 +01:00
|
|
|
libraryItemGroup[_path] = [Path.posix.join(dirparts[0], item.name)]
|
2022-03-07 23:22:20 +01:00
|
|
|
return
|
2021-09-11 02:55:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-09-18 01:40:30 +02:00
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
// Step 4: Add other files into library item 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)
|
2022-03-13 00:45:32 +01:00
|
|
|
if (libraryItemGroup[_path]) { // Directory is audiobook group
|
2021-11-06 23:26:44 +01:00
|
|
|
var relpath = Path.posix.join(dirparts.join('/'), item.name)
|
2022-03-13 00:45:32 +01:00
|
|
|
libraryItemGroup[_path].push(relpath)
|
2021-09-18 01:40:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-03-13 00:45:32 +01:00
|
|
|
return libraryItemGroup
|
2021-09-11 02:55:02 +02:00
|
|
|
}
|
|
|
|
|
2022-03-14 01:34:31 +01:00
|
|
|
function cleanFileObjects(libraryItemPath, folderPath, files) {
|
2022-02-28 01:07:36 +01:00
|
|
|
return Promise.all(files.map(async (file) => {
|
2022-03-13 00:45:32 +01:00
|
|
|
var filePath = Path.posix.join(libraryItemPath, file)
|
2022-03-14 01:34:31 +01:00
|
|
|
var relFilePath = filePath.replace(folderPath, '')
|
2022-03-13 00:45:32 +01:00
|
|
|
var newLibraryFile = new LibraryFile()
|
|
|
|
await newLibraryFile.setDataFromPath(filePath, relFilePath)
|
|
|
|
return newLibraryFile
|
2022-02-28 01:07:36 +01:00
|
|
|
}))
|
2021-09-11 02:55:02 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
// Scan folder
|
2022-03-13 00:45:32 +01:00
|
|
|
async function scanFolder(libraryMediaType, folder, serverSettings = {}) {
|
2021-11-06 23:26:44 +01:00
|
|
|
var folderPath = folder.fullPath.replace(/\\/g, '/')
|
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
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
var libraryItemGrouping = groupFileItemsIntoLibraryItemDirs(fileItems)
|
2021-09-11 02:55:02 +02:00
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
if (!Object.keys(libraryItemGrouping).length) {
|
|
|
|
Logger.error('Root path has no media folders', fileItems.length)
|
2021-09-11 02:55:02 +02:00
|
|
|
return []
|
2021-09-07 03:14:04 +02:00
|
|
|
}
|
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
var items = []
|
|
|
|
for (const libraryItemPath in libraryItemGrouping) {
|
|
|
|
var libraryItemData = getDataFromMediaDir(libraryMediaType, folderPath, libraryItemPath, serverSettings)
|
2021-09-11 02:55:02 +02:00
|
|
|
|
2022-03-14 01:34:31 +01:00
|
|
|
var fileObjs = await cleanFileObjects(libraryItemData.path, folderPath, libraryItemGrouping[libraryItemPath])
|
2022-03-13 00:45:32 +01:00
|
|
|
var libraryItemFolderStats = await getFileTimestampsWithIno(libraryItemData.path)
|
|
|
|
items.push({
|
2021-10-05 05:11:42 +02:00
|
|
|
folderId: folder.id,
|
|
|
|
libraryId: folder.libraryId,
|
2022-03-13 00:45:32 +01:00
|
|
|
ino: libraryItemFolderStats.ino,
|
|
|
|
mtimeMs: libraryItemFolderStats.mtimeMs || 0,
|
|
|
|
ctimeMs: libraryItemFolderStats.ctimeMs || 0,
|
|
|
|
birthtimeMs: libraryItemFolderStats.birthtimeMs || 0,
|
2022-03-22 01:24:38 +01:00
|
|
|
path: libraryItemData.path,
|
|
|
|
relPath: libraryItemData.relPath,
|
|
|
|
media: {
|
|
|
|
metadata: libraryItemData.mediaMetadata || null
|
|
|
|
},
|
2022-03-13 00:45:32 +01:00
|
|
|
libraryFiles: fileObjs
|
2021-09-11 02:55:02 +02:00
|
|
|
})
|
|
|
|
}
|
2022-03-13 00:45:32 +01:00
|
|
|
return items
|
2021-09-11 02:55:02 +02:00
|
|
|
}
|
2022-03-13 00:45:32 +01:00
|
|
|
module.exports.scanFolder = scanFolder
|
2021-09-11 02:55:02 +02:00
|
|
|
|
|
|
|
// Input relative filepath, output all details that can be parsed
|
2022-03-13 00:45:32 +01:00
|
|
|
function getBookDataFromDir(folderPath, relPath, parseSubtitle = false) {
|
|
|
|
relPath = relPath.replace(/\\/g, '/')
|
|
|
|
var splitDir = relPath.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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 01:34:31 +01:00
|
|
|
var publishedYear = 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])) {
|
2022-03-14 01:34:31 +01:00
|
|
|
publishedYear = publishYearMatch[1]
|
2021-09-18 19:45:34 +02:00
|
|
|
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 {
|
2022-03-13 00:45:32 +01:00
|
|
|
mediaMetadata: {
|
|
|
|
author,
|
|
|
|
title,
|
|
|
|
subtitle,
|
|
|
|
series,
|
|
|
|
sequence: volumeNumber,
|
2022-03-14 01:34:31 +01:00
|
|
|
publishedYear,
|
2022-03-13 00:45:32 +01:00
|
|
|
},
|
|
|
|
relPath: relPath, // relative audiobook path i.e. /Author Name/Book Name/..
|
|
|
|
path: Path.posix.join(folderPath, relPath) // i.e. /audiobook/Author Name/Book Name/..
|
2021-09-07 03:14:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 01:24:38 +01:00
|
|
|
function getPodcastDataFromDir(folderPath, relPath) {
|
|
|
|
relPath = relPath.replace(/\\/g, '/')
|
|
|
|
return {
|
|
|
|
relPath: relPath, // relative audiobook path i.e. /Author Name/Book Name/..
|
|
|
|
path: Path.posix.join(folderPath, relPath) // i.e. /audiobook/Author Name/Book Name/..
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
function getDataFromMediaDir(libraryMediaType, folderPath, relPath, serverSettings) {
|
2021-09-07 03:14:04 +02:00
|
|
|
var parseSubtitle = !!serverSettings.scannerParseSubtitle
|
2022-03-22 01:24:38 +01:00
|
|
|
if (libraryMediaType === 'podcast') {
|
|
|
|
return getPodcastDataFromDir(folderPath, relPath, parseSubtitle)
|
|
|
|
} else {
|
|
|
|
return getBookDataFromDir(folderPath, relPath, parseSubtitle)
|
|
|
|
}
|
2022-03-13 00:45:32 +01:00
|
|
|
}
|
|
|
|
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
async function getLibraryItemFileData(libraryMediaType, folder, libraryItemPath, serverSettings = {}) {
|
2022-03-14 01:34:31 +01:00
|
|
|
var fileItems = await recurseFiles(libraryItemPath)
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
libraryItemPath = libraryItemPath.replace(/\\/g, '/')
|
2021-11-06 23:26:44 +01:00
|
|
|
var folderFullPath = folder.fullPath.replace(/\\/g, '/')
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
var libraryItemDir = libraryItemPath.replace(folderFullPath, '').slice(1)
|
|
|
|
var libraryItemData = getDataFromMediaDir(libraryMediaType, folderFullPath, libraryItemDir, serverSettings)
|
|
|
|
var libraryItemDirStats = await getFileTimestampsWithIno(libraryItemData.path)
|
|
|
|
var libraryItem = {
|
|
|
|
ino: libraryItemDirStats.ino,
|
|
|
|
mtimeMs: libraryItemDirStats.mtimeMs || 0,
|
|
|
|
ctimeMs: libraryItemDirStats.ctimeMs || 0,
|
|
|
|
birthtimeMs: libraryItemDirStats.birthtimeMs || 0,
|
2021-10-05 05:11:42 +02:00
|
|
|
folderId: folder.id,
|
|
|
|
libraryId: folder.libraryId,
|
2022-03-22 01:24:38 +01:00
|
|
|
path: libraryItemData.path,
|
|
|
|
relPath: libraryItemData.relPath,
|
|
|
|
media: {
|
|
|
|
metadata: libraryItemData.mediaMetadata || null
|
|
|
|
},
|
2022-03-13 00:45:32 +01:00
|
|
|
libraryFiles: []
|
2021-09-11 02:55:02 +02:00
|
|
|
}
|
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]
|
2022-03-13 00:45:32 +01:00
|
|
|
var newLibraryFile = new LibraryFile()
|
|
|
|
// fileItem.path is the relative path
|
|
|
|
await newLibraryFile.setDataFromPath(fileItem.fullpath, fileItem.path)
|
|
|
|
libraryItem.libraryFiles.push(newLibraryFile)
|
2021-09-27 13:52:21 +02:00
|
|
|
}
|
2022-03-13 00:45:32 +01:00
|
|
|
return libraryItem
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2022-03-13 00:45:32 +01:00
|
|
|
module.exports.getLibraryItemFileData = getLibraryItemFileData
|