2021-08-18 00:01:11 +02:00
|
|
|
const Path = require('path')
|
2022-07-06 02:53:01 +02:00
|
|
|
const fs = require('../libs/fsExtra')
|
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-04-28 02:42:34 +02:00
|
|
|
function isMediaFile(mediaType, ext) {
|
2022-05-25 02:06:44 +02:00
|
|
|
if (!ext) return false
|
|
|
|
var extclean = ext.slice(1).toLowerCase()
|
2022-12-22 23:38:55 +01:00
|
|
|
if (mediaType === 'podcast' || mediaType === 'music') return globals.SupportedAudioTypes.includes(extclean)
|
2022-05-31 02:26:53 +02:00
|
|
|
else if (mediaType === 'video') return globals.SupportedVideoTypes.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
|
|
|
}
|
|
|
|
|
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
|
2022-05-24 01:15:15 +02:00
|
|
|
function groupFilesIntoLibraryItemPaths(mediaType, paths) {
|
2022-05-25 02:06:44 +02:00
|
|
|
// Step 1: Clean path, Remove leading "/", Filter out non-media files in root dir
|
2022-07-19 15:33:32 +02:00
|
|
|
var nonMediaFilePaths = []
|
2022-05-25 02:06:44 +02:00
|
|
|
var pathsFiltered = paths.map(path => {
|
|
|
|
return path.startsWith('/') ? path.slice(1) : path
|
|
|
|
}).filter(path => {
|
|
|
|
let parsedPath = Path.parse(path)
|
2022-07-19 15:33:32 +02:00
|
|
|
// Is not in root dir OR is a book media file
|
|
|
|
if (parsedPath.dir) {
|
|
|
|
if (!isMediaFile(mediaType, parsedPath.ext)) { // Seperate out non-media files
|
|
|
|
nonMediaFilePaths.push(path)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
} else if (mediaType === 'book' && isMediaFile(mediaType, parsedPath.ext)) { // (book media type supports single file audiobooks/ebooks in root dir)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2022-05-25 02:06:44 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// Step 2: Sort by least number of directories
|
|
|
|
pathsFiltered.sort((a, b) => {
|
|
|
|
var pathsA = Path.dirname(a).split('/').length
|
|
|
|
var pathsB = Path.dirname(b).split('/').length
|
|
|
|
return pathsA - pathsB
|
|
|
|
})
|
|
|
|
|
|
|
|
// Step 3: Group files in dirs
|
|
|
|
var itemGroup = {}
|
|
|
|
pathsFiltered.forEach((path) => {
|
|
|
|
var dirparts = Path.dirname(path).split('/').filter(p => !!p && p !== '.') // dirname returns . if no directory
|
|
|
|
var numparts = dirparts.length
|
|
|
|
var _path = ''
|
|
|
|
|
|
|
|
if (!numparts) {
|
|
|
|
// Media file in root
|
|
|
|
itemGroup[path] = path
|
|
|
|
} else {
|
|
|
|
// Iterate over directories in path
|
|
|
|
for (let i = 0; i < numparts; i++) {
|
|
|
|
var dirpart = dirparts.shift()
|
|
|
|
_path = Path.posix.join(_path, dirpart)
|
|
|
|
|
|
|
|
if (itemGroup[_path]) { // Directory already has files, add file
|
|
|
|
var relpath = Path.posix.join(dirparts.join('/'), Path.basename(path))
|
|
|
|
itemGroup[_path].push(relpath)
|
|
|
|
return
|
|
|
|
} else if (!dirparts.length) { // This is the last directory, create group
|
|
|
|
itemGroup[_path] = [Path.basename(path)]
|
|
|
|
return
|
|
|
|
} 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
|
|
|
|
itemGroup[_path] = [Path.posix.join(dirparts[0], Path.basename(path))]
|
|
|
|
return
|
2022-05-24 01:15:15 +02:00
|
|
|
}
|
2022-05-25 02:06:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-07-19 15:33:32 +02:00
|
|
|
|
|
|
|
// Step 4: Add in non-media files if they fit into item group
|
|
|
|
if (nonMediaFilePaths.length) {
|
|
|
|
for (const nonMediaFilePath of nonMediaFilePaths) {
|
|
|
|
const pathDir = Path.dirname(nonMediaFilePath)
|
|
|
|
if (itemGroup[pathDir]) {
|
|
|
|
itemGroup[pathDir].push(nonMediaFilePath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 02:06:44 +02: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
|
2022-03-26 20:29:49 +01:00
|
|
|
function groupFileItemsIntoLibraryItemDirs(mediaType, fileItems) {
|
2022-12-22 23:38:55 +01:00
|
|
|
// Handle music where every audio file is a library item
|
|
|
|
if (mediaType === 'music') {
|
|
|
|
const audioFileGroup = {}
|
|
|
|
fileItems.filter(i => isMediaFile(mediaType, i.extension)).forEach((item) => {
|
|
|
|
if (!item.reldirpath) {
|
|
|
|
audioFileGroup[item.name] = item.name
|
|
|
|
} else {
|
|
|
|
audioFileGroup[item.reldirpath] = [item.name]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return audioFileGroup
|
|
|
|
}
|
|
|
|
|
2022-05-25 02:06:44 +02:00
|
|
|
// Step 1: Filter out non-book-media files in root dir (with depth of 0)
|
2022-12-22 23:38:55 +01:00
|
|
|
const itemsFiltered = fileItems.filter(i => {
|
|
|
|
return i.deep > 0 || ((mediaType === 'book' || mediaType === 'video' || mediaType === 'music') && isMediaFile(mediaType, i.extension))
|
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
|
2022-12-22 23:38:55 +01:00
|
|
|
const mediaFileItems = []
|
|
|
|
const otherFileItems = []
|
2022-05-25 02:06:44 +02:00
|
|
|
itemsFiltered.forEach(item => {
|
|
|
|
if (isMediaFile(mediaType, item.extension)) mediaFileItems.push(item)
|
|
|
|
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) => {
|
2022-12-22 23:38:55 +01:00
|
|
|
const dirparts = item.reldirpath.split('/').filter(p => !!p)
|
|
|
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
} else if (!dirparts.length) { // This is the last directory, create group
|
|
|
|
libraryItemGroup[_path] = [item.name]
|
|
|
|
return
|
|
|
|
} 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
|
|
|
|
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)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-26 21:23:25 +01:00
|
|
|
function cleanFileObjects(libraryItemPath, files) {
|
2022-05-31 02:26:53 +02:00
|
|
|
return Promise.all(files.map(async (file) => {
|
2022-12-22 23:38:55 +01:00
|
|
|
const filePath = Path.posix.join(libraryItemPath, file)
|
|
|
|
const newLibraryFile = new LibraryFile()
|
2022-05-25 02:06:44 +02:00
|
|
|
await newLibraryFile.setDataFromPath(filePath, file)
|
|
|
|
return newLibraryFile
|
|
|
|
}))
|
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 = {}) {
|
2022-12-22 23:38:55 +01:00
|
|
|
const folderPath = folder.fullPath.replace(/\\/g, '/')
|
2022-05-25 02:06:44 +02:00
|
|
|
|
2022-12-22 23:38:55 +01:00
|
|
|
const pathExists = await fs.pathExists(folderPath)
|
2022-05-25 02:06:44 +02:00
|
|
|
if (!pathExists) {
|
|
|
|
Logger.error(`[scandir] Invalid folder path does not exist "${folderPath}"`)
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2022-12-22 23:38:55 +01:00
|
|
|
const fileItems = await recurseFiles(folderPath)
|
|
|
|
const libraryItemGrouping = groupFileItemsIntoLibraryItemDirs(libraryMediaType, fileItems)
|
2022-05-25 02:06:44 +02:00
|
|
|
|
|
|
|
if (!Object.keys(libraryItemGrouping).length) {
|
|
|
|
Logger.error(`Root path has no media folders: ${folderPath}`)
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2022-12-22 23:38:55 +01:00
|
|
|
const items = []
|
2022-05-25 02:06:44 +02:00
|
|
|
for (const libraryItemPath in libraryItemGrouping) {
|
2022-12-22 23:38:55 +01:00
|
|
|
let isFile = false // item is not in a folder
|
|
|
|
let libraryItemData = null
|
|
|
|
let fileObjs = []
|
2022-05-25 02:06:44 +02:00
|
|
|
if (libraryItemPath === libraryItemGrouping[libraryItemPath]) {
|
|
|
|
// Media file in root only get title
|
|
|
|
libraryItemData = {
|
|
|
|
mediaMetadata: {
|
|
|
|
title: Path.basename(libraryItemPath, Path.extname(libraryItemPath))
|
|
|
|
},
|
|
|
|
path: Path.posix.join(folderPath, libraryItemPath),
|
|
|
|
relPath: libraryItemPath
|
|
|
|
}
|
|
|
|
fileObjs = await cleanFileObjects(folderPath, [libraryItemPath])
|
|
|
|
isFile = true
|
|
|
|
} else {
|
2022-12-22 23:38:55 +01:00
|
|
|
libraryItemData = getDataFromMediaDir(libraryMediaType, folderPath, libraryItemPath, serverSettings, libraryItemGrouping[libraryItemPath])
|
2022-05-25 02:06:44 +02:00
|
|
|
fileObjs = await cleanFileObjects(libraryItemData.path, libraryItemGrouping[libraryItemPath])
|
2022-05-24 21:56:10 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 23:38:55 +01:00
|
|
|
const libraryItemFolderStats = await getFileTimestampsWithIno(libraryItemData.path)
|
2022-05-25 02:06:44 +02:00
|
|
|
items.push({
|
|
|
|
folderId: folder.id,
|
|
|
|
libraryId: folder.libraryId,
|
|
|
|
ino: libraryItemFolderStats.ino,
|
|
|
|
mtimeMs: libraryItemFolderStats.mtimeMs || 0,
|
|
|
|
ctimeMs: libraryItemFolderStats.ctimeMs || 0,
|
|
|
|
birthtimeMs: libraryItemFolderStats.birthtimeMs || 0,
|
|
|
|
path: libraryItemData.path,
|
|
|
|
relPath: libraryItemData.relPath,
|
|
|
|
isFile,
|
|
|
|
media: {
|
|
|
|
metadata: libraryItemData.mediaMetadata || null
|
|
|
|
},
|
|
|
|
libraryFiles: fileObjs
|
|
|
|
})
|
|
|
|
}
|
|
|
|
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) {
|
2022-05-25 02:06:44 +02:00
|
|
|
relPath = relPath.replace(/\\/g, '/')
|
|
|
|
var splitDir = relPath.split('/')
|
|
|
|
|
|
|
|
var folder = splitDir.pop() // Audio files will always be in the directory named for the title
|
|
|
|
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/
|
|
|
|
|
|
|
|
// The may contain various other pieces of metadata, these functions extract it.
|
|
|
|
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 {
|
|
|
|
mediaMetadata: {
|
|
|
|
author,
|
|
|
|
title,
|
|
|
|
subtitle,
|
|
|
|
series,
|
|
|
|
sequence,
|
|
|
|
publishedYear,
|
|
|
|
narrators,
|
|
|
|
},
|
|
|
|
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-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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-05-25 01:21:58 +02:00
|
|
|
function getSequence(folder) {
|
2022-05-25 02:06:44 +02:00
|
|
|
// Valid ways of including a volume number:
|
|
|
|
// [
|
|
|
|
// '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',
|
|
|
|
// '2 - Book Title',
|
|
|
|
// '6. Title',
|
|
|
|
// '0.5 - Book Title'
|
|
|
|
// ]
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if (!parts[i]) { parts.splice(i, 1) }
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-22 01:24:38 +01:00
|
|
|
function getPodcastDataFromDir(folderPath, relPath) {
|
2022-05-25 02:06:44 +02:00
|
|
|
relPath = relPath.replace(/\\/g, '/')
|
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 {
|
|
|
|
mediaMetadata: {
|
|
|
|
title
|
|
|
|
},
|
2022-12-22 23:38:55 +01:00
|
|
|
relPath: relPath, // relative podcast path i.e. /Podcast Name/..
|
|
|
|
path: Path.posix.join(folderPath, relPath) // i.e. /podcasts/Podcast Name/..
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMusicDataFromDir(folderPath, relPath, fileNames) {
|
|
|
|
relPath = relPath.replace(/\\/g, '/')
|
|
|
|
|
|
|
|
const firstFileName = fileNames.length ? fileNames[0] : ''
|
|
|
|
return {
|
|
|
|
mediaMetadata: {
|
|
|
|
title: Path.basename(firstFileName, Path.extname(firstFileName))
|
|
|
|
},
|
|
|
|
relPath: relPath, // relative music audio file path i.e. /Some Folder/..
|
|
|
|
path: Path.posix.join(folderPath, relPath) // i.e. /music/Some Folder/..
|
2022-05-25 02:06:44 +02:00
|
|
|
}
|
2022-03-22 01:24:38 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 23:38:55 +01:00
|
|
|
function getDataFromMediaDir(libraryMediaType, folderPath, relPath, serverSettings, fileNames) {
|
|
|
|
if (libraryMediaType === 'music') {
|
|
|
|
return getMusicDataFromDir(folderPath, relPath, fileNames)
|
|
|
|
} else if (libraryMediaType === 'podcast') {
|
2022-05-25 02:06:44 +02:00
|
|
|
return getPodcastDataFromDir(folderPath, relPath)
|
2022-05-31 02:26:53 +02:00
|
|
|
} else if (libraryMediaType === 'book') {
|
2022-05-25 02:06:44 +02:00
|
|
|
var parseSubtitle = !!serverSettings.scannerParseSubtitle
|
|
|
|
return getBookDataFromDir(folderPath, relPath, parseSubtitle)
|
2022-05-31 02:26:53 +02:00
|
|
|
} else {
|
2022-10-08 22:42:38 +02:00
|
|
|
return getPodcastDataFromDir(folderPath, relPath)
|
2022-05-25 02:06:44 +02:00
|
|
|
}
|
2022-03-13 00:45:32 +01:00
|
|
|
}
|
|
|
|
|
2022-04-28 02:42:34 +02:00
|
|
|
// Called from Scanner.js
|
2022-05-24 01:15:15 +02:00
|
|
|
async function getLibraryItemFileData(libraryMediaType, folder, libraryItemPath, isSingleMediaItem, serverSettings = {}) {
|
2022-05-25 02:06:44 +02:00
|
|
|
libraryItemPath = libraryItemPath.replace(/\\/g, '/')
|
|
|
|
var folderFullPath = folder.fullPath.replace(/\\/g, '/')
|
|
|
|
|
|
|
|
var libraryItemDir = libraryItemPath.replace(folderFullPath, '').slice(1)
|
|
|
|
var libraryItemData = {}
|
|
|
|
|
|
|
|
var fileItems = []
|
|
|
|
|
|
|
|
if (isSingleMediaItem) { // Single media item in root of folder
|
2022-05-25 02:30:16 +02:00
|
|
|
fileItems = [
|
|
|
|
{
|
2022-05-31 02:26:53 +02:00
|
|
|
fullpath: libraryItemPath,
|
|
|
|
path: libraryItemDir // actually the relPath (only filename here)
|
|
|
|
}
|
|
|
|
]
|
2022-05-25 02:06:44 +02:00
|
|
|
libraryItemData = {
|
|
|
|
path: libraryItemPath, // full path
|
|
|
|
relPath: libraryItemDir, // only filename
|
|
|
|
mediaMetadata: {
|
|
|
|
title: Path.basename(libraryItemDir, Path.extname(libraryItemDir))
|
|
|
|
}
|
2022-05-24 01:15:15 +02:00
|
|
|
}
|
2022-05-25 02:06:44 +02:00
|
|
|
} else {
|
|
|
|
fileItems = await recurseFiles(libraryItemPath)
|
2022-12-22 23:38:55 +01:00
|
|
|
const fileNames = fileItems.map(i => i.name)
|
|
|
|
libraryItemData = getDataFromMediaDir(libraryMediaType, folderFullPath, libraryItemDir, serverSettings, fileNames)
|
2022-05-25 02:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var libraryItemDirStats = await getFileTimestampsWithIno(libraryItemData.path)
|
|
|
|
var libraryItem = {
|
|
|
|
ino: libraryItemDirStats.ino,
|
|
|
|
mtimeMs: libraryItemDirStats.mtimeMs || 0,
|
|
|
|
ctimeMs: libraryItemDirStats.ctimeMs || 0,
|
|
|
|
birthtimeMs: libraryItemDirStats.birthtimeMs || 0,
|
|
|
|
folderId: folder.id,
|
|
|
|
libraryId: folder.libraryId,
|
|
|
|
path: libraryItemData.path,
|
|
|
|
relPath: libraryItemData.relPath,
|
|
|
|
isFile: isSingleMediaItem,
|
|
|
|
media: {
|
|
|
|
metadata: libraryItemData.mediaMetadata || null
|
|
|
|
},
|
|
|
|
libraryFiles: []
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < fileItems.length; i++) {
|
2022-12-22 23:38:55 +01:00
|
|
|
const fileItem = fileItems[i]
|
|
|
|
const newLibraryFile = new LibraryFile()
|
2022-05-25 02:30:16 +02:00
|
|
|
// fileItem.path is the relative path
|
2022-05-25 02:06:44 +02:00
|
|
|
await newLibraryFile.setDataFromPath(fileItem.fullpath, fileItem.path)
|
|
|
|
libraryItem.libraryFiles.push(newLibraryFile)
|
|
|
|
}
|
|
|
|
return libraryItem
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2022-05-25 02:30:16 +02:00
|
|
|
module.exports.getLibraryItemFileData = getLibraryItemFileData
|