2022-03-14 15:56:24 +01:00
|
|
|
const Path = require('path')
|
|
|
|
const fs = require('fs-extra')
|
|
|
|
const filePerms = require('../utils/filePerms')
|
2021-11-22 03:00:40 +01:00
|
|
|
const Logger = require('../Logger')
|
|
|
|
const Library = require('../objects/Library')
|
2021-12-01 03:02:40 +01:00
|
|
|
const libraryHelpers = require('../utils/libraryHelpers')
|
2022-03-26 20:29:49 +01:00
|
|
|
const { sort, createNewSortInstance } = require('fast-sort')
|
2021-12-26 23:17:10 +01:00
|
|
|
const naturalSort = createNewSortInstance({
|
|
|
|
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare
|
|
|
|
})
|
2021-11-22 03:00:40 +01:00
|
|
|
class LibraryController {
|
|
|
|
constructor() { }
|
|
|
|
|
|
|
|
async create(req, res) {
|
|
|
|
var newLibraryPayload = {
|
|
|
|
...req.body
|
|
|
|
}
|
|
|
|
if (!newLibraryPayload.name || !newLibraryPayload.folders || !newLibraryPayload.folders.length) {
|
|
|
|
return res.status(500).send('Invalid request')
|
|
|
|
}
|
|
|
|
|
2022-03-19 12:41:54 +01:00
|
|
|
// Validate folder paths exist or can be created & resolve rel paths
|
|
|
|
// returns 400 if a folder fails to access
|
|
|
|
newLibraryPayload.folders = newLibraryPayload.folders.map(f => {
|
|
|
|
f.fullPath = Path.resolve(f.fullPath)
|
|
|
|
return f
|
|
|
|
})
|
|
|
|
for (var folder of newLibraryPayload.folders) {
|
2022-04-21 00:49:34 +02:00
|
|
|
try {
|
|
|
|
var direxists = await fs.pathExists(folder.fullPath)
|
|
|
|
if (!direxists) { // If folder does not exist try to make it and set file permissions/owner
|
|
|
|
await fs.mkdir(folder.fullPath)
|
|
|
|
await filePerms.setDefault(folder.fullPath)
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2022-03-19 12:41:54 +01:00
|
|
|
Logger.error(`[LibraryController] Failed to ensure folder dir "${folder.fullPath}"`, error)
|
|
|
|
return res.status(400).send(`Invalid folder directory "${folder.fullPath}"`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 03:00:40 +01:00
|
|
|
var library = new Library()
|
|
|
|
newLibraryPayload.displayOrder = this.db.libraries.length + 1
|
|
|
|
library.setData(newLibraryPayload)
|
|
|
|
await this.db.insertEntity('library', library)
|
|
|
|
this.emitter('library_added', library.toJSON())
|
|
|
|
|
|
|
|
// Add library watcher
|
|
|
|
this.watcher.addLibrary(library)
|
|
|
|
|
|
|
|
res.json(library)
|
|
|
|
}
|
|
|
|
|
|
|
|
findAll(req, res) {
|
2022-01-16 18:17:09 +01:00
|
|
|
var librariesAccessible = req.user.librariesAccessible || []
|
|
|
|
if (librariesAccessible && librariesAccessible.length) {
|
|
|
|
return res.json(this.db.libraries.filter(lib => librariesAccessible.includes(lib.id)).map(lib => lib.toJSON()))
|
|
|
|
}
|
|
|
|
|
2021-11-22 03:00:40 +01:00
|
|
|
res.json(this.db.libraries.map(lib => lib.toJSON()))
|
|
|
|
}
|
|
|
|
|
2021-12-01 03:02:40 +01:00
|
|
|
async findOne(req, res) {
|
|
|
|
if (req.query.include && req.query.include === 'filterdata') {
|
|
|
|
return res.json({
|
2022-03-11 01:45:02 +01:00
|
|
|
filterdata: libraryHelpers.getDistinctFilterDataNew(req.libraryItems),
|
2022-03-18 18:37:47 +01:00
|
|
|
issues: req.libraryItems.filter(li => li.hasIssues).length,
|
2021-12-01 03:02:40 +01:00
|
|
|
library: req.library
|
|
|
|
})
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2021-12-01 03:02:40 +01:00
|
|
|
return res.json(req.library)
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async update(req, res) {
|
2021-12-01 03:02:40 +01:00
|
|
|
var library = req.library
|
2022-02-03 23:39:05 +01:00
|
|
|
|
2022-03-14 15:56:24 +01:00
|
|
|
// Validate new folder paths exist or can be created & resolve rel paths
|
|
|
|
// returns 400 if a new folder fails to access
|
|
|
|
if (req.body.folders) {
|
|
|
|
var newFolderPaths = []
|
|
|
|
req.body.folders = req.body.folders.map(f => {
|
|
|
|
if (!f.id) {
|
|
|
|
f.fullPath = Path.resolve(f.fullPath)
|
|
|
|
newFolderPaths.push(f.fullPath)
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
})
|
|
|
|
for (var path of newFolderPaths) {
|
|
|
|
var success = await fs.ensureDir(path).then(() => true).catch((error) => {
|
|
|
|
Logger.error(`[LibraryController] Failed to ensure folder dir "${path}"`, error)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if (!success) {
|
|
|
|
return res.status(400).send(`Invalid folder directory "${path}"`)
|
|
|
|
} else {
|
|
|
|
await filePerms.setDefault(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 03:00:40 +01:00
|
|
|
var hasUpdates = library.update(req.body)
|
2022-02-03 23:39:05 +01:00
|
|
|
// TODO: Should check if this is an update to folder paths or name only
|
2021-11-22 03:00:40 +01:00
|
|
|
if (hasUpdates) {
|
|
|
|
// Update watcher
|
|
|
|
this.watcher.updateLibrary(library)
|
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
// Remove libraryItems no longer in library
|
|
|
|
var itemsToRemove = this.db.libraryItems.filter(li => li.libraryId === library.id && !library.checkFullPathInLibrary(li.path))
|
|
|
|
if (itemsToRemove.length) {
|
|
|
|
Logger.info(`[Scanner] Updating library, removing ${itemsToRemove.length} items`)
|
|
|
|
for (let i = 0; i < itemsToRemove.length; i++) {
|
|
|
|
await this.handleDeleteLibraryItem(itemsToRemove[i])
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
await this.db.updateEntity('library', library)
|
|
|
|
this.emitter('library_updated', library.toJSON())
|
|
|
|
}
|
|
|
|
return res.json(library.toJSON())
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(req, res) {
|
2021-12-01 03:02:40 +01:00
|
|
|
var library = req.library
|
2021-11-22 03:00:40 +01:00
|
|
|
|
|
|
|
// Remove library watcher
|
|
|
|
this.watcher.removeLibrary(library)
|
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
// Remove items in this library
|
|
|
|
var libraryItems = this.db.libraryItems.filter(li => li.libraryId === library.id)
|
|
|
|
Logger.info(`[Server] deleting library "${library.name}" with ${libraryItems.length} items"`)
|
|
|
|
for (let i = 0; i < libraryItems.length; i++) {
|
|
|
|
await this.handleDeleteLibraryItem(libraryItems[i])
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var libraryJson = library.toJSON()
|
|
|
|
await this.db.removeEntity('library', library.id)
|
|
|
|
this.emitter('library_removed', libraryJson)
|
|
|
|
return res.json(libraryJson)
|
|
|
|
}
|
|
|
|
|
2022-03-11 01:45:02 +01:00
|
|
|
// api/libraries/:id/items
|
2022-03-13 00:45:32 +01:00
|
|
|
// TODO: Optimize this method, items are iterated through several times but can be combined
|
2022-03-11 01:45:02 +01:00
|
|
|
getLibraryItems(req, res) {
|
2022-03-26 17:59:34 +01:00
|
|
|
var libraryItems = req.libraryItems
|
2022-03-11 01:45:02 +01:00
|
|
|
var payload = {
|
|
|
|
results: [],
|
|
|
|
total: libraryItems.length,
|
|
|
|
limit: req.query.limit && !isNaN(req.query.limit) ? Number(req.query.limit) : 0,
|
|
|
|
page: req.query.page && !isNaN(req.query.page) ? Number(req.query.page) : 0,
|
|
|
|
sortBy: req.query.sort,
|
|
|
|
sortDesc: req.query.desc === '1',
|
|
|
|
filterBy: req.query.filter,
|
2022-03-26 17:59:34 +01:00
|
|
|
mediaType: req.library.mediaType,
|
2022-03-11 01:45:02 +01:00
|
|
|
minified: req.query.minified === '1',
|
|
|
|
collapseseries: req.query.collapseseries === '1'
|
|
|
|
}
|
2021-11-29 02:36:44 +01:00
|
|
|
|
2022-04-10 02:44:46 +02:00
|
|
|
var filterSeries = null
|
2022-03-11 01:45:02 +01:00
|
|
|
if (payload.filterBy) {
|
2022-04-10 02:44:46 +02:00
|
|
|
// If filtering by series, will include seriesName and seriesSequence on media metadata
|
|
|
|
filterSeries = (payload.mediaType == 'book' && payload.filterBy.startsWith('series.')) ? libraryHelpers.decode(payload.filterBy.replace('series.', '')) : null
|
|
|
|
|
2022-03-11 01:45:02 +01:00
|
|
|
libraryItems = libraryHelpers.getFilteredLibraryItems(libraryItems, payload.filterBy, req.user)
|
|
|
|
payload.total = libraryItems.length
|
2021-11-29 02:36:44 +01:00
|
|
|
}
|
2021-11-22 03:00:40 +01:00
|
|
|
|
2022-03-11 01:45:02 +01:00
|
|
|
if (payload.sortBy) {
|
|
|
|
var sortKey = payload.sortBy
|
|
|
|
|
2022-03-17 18:25:12 +01:00
|
|
|
// old sort key TODO: should be mutated in dbMigration
|
2022-03-11 01:45:02 +01:00
|
|
|
if (sortKey.startsWith('book.')) {
|
|
|
|
sortKey = sortKey.replace('book.', 'media.metadata.')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle server setting sortingIgnorePrefix
|
|
|
|
if (sortKey === 'media.metadata.title' && this.db.serverSettings.sortingIgnorePrefix) {
|
|
|
|
// BookMetadata.js has titleIgnorePrefix getter
|
|
|
|
sortKey += 'IgnorePrefix'
|
|
|
|
}
|
|
|
|
|
2022-04-23 00:11:03 +02:00
|
|
|
// Start sort
|
2022-03-11 01:45:02 +01:00
|
|
|
var direction = payload.sortDesc ? 'desc' : 'asc'
|
2022-04-23 00:11:03 +02:00
|
|
|
var sortArray = [
|
|
|
|
{
|
|
|
|
[direction]: (li) => {
|
|
|
|
// Supports dot notation strings i.e. "media.metadata.title"
|
|
|
|
return sortKey.split('.').reduce((a, b) => a[b], li)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
// Secondary sort when sorting by book author use series sort title
|
|
|
|
if (payload.mediaType === 'book' && payload.sortBy.includes('author')) {
|
|
|
|
sortArray.push({
|
|
|
|
asc: (li) => {
|
|
|
|
if (li.media.metadata.series && li.media.metadata.series.length) {
|
|
|
|
return li.media.metadata.getSeriesSortTitle(li.media.metadata.series[0])
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
libraryItems = naturalSort(libraryItems).by(sortArray)
|
2021-11-29 02:36:44 +01:00
|
|
|
}
|
|
|
|
|
2022-03-11 01:45:02 +01:00
|
|
|
// TODO: Potentially implement collapse series again
|
2022-04-10 02:44:46 +02:00
|
|
|
if (payload.collapseseries) {
|
|
|
|
libraryItems = libraryHelpers.collapseBookSeries(libraryItems)
|
|
|
|
payload.total = libraryItems.length
|
|
|
|
} else if (filterSeries) {
|
|
|
|
// Book media when filtering series will include series object on media metadata
|
|
|
|
libraryItems = libraryItems.map(li => {
|
|
|
|
var series = li.media.metadata.getSeries(filterSeries)
|
|
|
|
var liJson = payload.minified ? li.toJSONMinified() : li.toJSON()
|
|
|
|
liJson.media.metadata.series = series
|
|
|
|
return liJson
|
|
|
|
})
|
|
|
|
libraryItems = naturalSort(libraryItems).asc(li => li.media.metadata.series.sequence)
|
|
|
|
} else {
|
|
|
|
libraryItems = libraryItems.map(li => payload.minified ? li.toJSONMinified() : li.toJSON())
|
|
|
|
}
|
2022-03-11 01:45:02 +01:00
|
|
|
|
|
|
|
if (payload.limit) {
|
|
|
|
var startIndex = payload.page * payload.limit
|
|
|
|
libraryItems = libraryItems.slice(startIndex, startIndex + payload.limit)
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2022-03-11 01:45:02 +01:00
|
|
|
payload.results = libraryItems
|
|
|
|
res.json(payload)
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
|
2021-12-01 03:02:40 +01:00
|
|
|
// api/libraries/:id/series
|
2021-12-02 02:07:03 +01:00
|
|
|
async getAllSeriesForLibrary(req, res) {
|
2022-03-13 01:50:31 +01:00
|
|
|
var libraryItems = req.libraryItems
|
2021-12-01 03:02:40 +01:00
|
|
|
var payload = {
|
|
|
|
results: [],
|
|
|
|
total: 0,
|
|
|
|
limit: req.query.limit && !isNaN(req.query.limit) ? Number(req.query.limit) : 0,
|
|
|
|
page: req.query.page && !isNaN(req.query.page) ? Number(req.query.page) : 0,
|
|
|
|
sortBy: req.query.sort,
|
|
|
|
sortDesc: req.query.desc === '1',
|
2021-12-24 23:37:57 +01:00
|
|
|
filterBy: req.query.filter,
|
|
|
|
minified: req.query.minified === '1'
|
2021-12-01 03:02:40 +01:00
|
|
|
}
|
|
|
|
|
2022-03-13 01:50:31 +01:00
|
|
|
var series = libraryHelpers.getSeriesFromBooks(libraryItems, payload.minified)
|
2022-02-13 22:00:59 +01:00
|
|
|
series = sort(series).asc(s => {
|
|
|
|
return s.name
|
|
|
|
})
|
2021-12-01 03:02:40 +01:00
|
|
|
payload.total = series.length
|
|
|
|
|
|
|
|
if (payload.limit) {
|
|
|
|
var startIndex = payload.page * payload.limit
|
|
|
|
series = series.slice(startIndex, startIndex + payload.limit)
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:26:12 +01:00
|
|
|
payload.results = series
|
2021-12-01 03:02:40 +01:00
|
|
|
res.json(payload)
|
|
|
|
}
|
|
|
|
|
2022-03-13 01:50:31 +01:00
|
|
|
// api/libraries/:id/collections
|
2021-12-01 03:02:40 +01:00
|
|
|
async getCollectionsForLibrary(req, res) {
|
2022-03-13 01:50:31 +01:00
|
|
|
var libraryItems = req.libraryItems
|
2021-12-01 03:02:40 +01:00
|
|
|
|
|
|
|
var payload = {
|
|
|
|
results: [],
|
|
|
|
total: 0,
|
|
|
|
limit: req.query.limit && !isNaN(req.query.limit) ? Number(req.query.limit) : 0,
|
|
|
|
page: req.query.page && !isNaN(req.query.page) ? Number(req.query.page) : 0,
|
|
|
|
sortBy: req.query.sort,
|
|
|
|
sortDesc: req.query.desc === '1',
|
2021-12-24 23:37:57 +01:00
|
|
|
filterBy: req.query.filter,
|
|
|
|
minified: req.query.minified === '1'
|
2021-12-01 03:02:40 +01:00
|
|
|
}
|
|
|
|
|
2022-04-22 02:29:15 +02:00
|
|
|
var collections = this.db.collections.filter(c => c.libraryId === req.library.id).map(c => {
|
|
|
|
var expanded = c.toJSONExpanded(libraryItems, payload.minified)
|
|
|
|
// If all books restricted to user in this collection then hide this collection
|
|
|
|
if (!expanded.books.length && c.books.length) return null
|
|
|
|
return expanded
|
|
|
|
}).filter(c => !!c)
|
|
|
|
|
2021-12-01 03:02:40 +01:00
|
|
|
payload.total = collections.length
|
|
|
|
|
|
|
|
if (payload.limit) {
|
|
|
|
var startIndex = payload.page * payload.limit
|
|
|
|
collections = collections.slice(startIndex, startIndex + payload.limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.results = collections
|
|
|
|
res.json(payload)
|
|
|
|
}
|
|
|
|
|
2022-03-11 01:45:02 +01:00
|
|
|
async getLibraryFilterData(req, res) {
|
|
|
|
res.json(libraryHelpers.getDistinctFilterDataNew(req.libraryItems))
|
|
|
|
}
|
|
|
|
|
2022-03-14 01:34:31 +01:00
|
|
|
// api/libraries/:id/personalized
|
2022-04-24 23:56:30 +02:00
|
|
|
// New and improved personalized call only loops through library items once
|
|
|
|
async getLibraryUserPersonalizedOptimal(req, res) {
|
|
|
|
const mediaType = req.library.mediaType
|
|
|
|
const libraryItems = req.libraryItems
|
|
|
|
const limitPerShelf = req.query.limit && !isNaN(req.query.limit) ? Number(req.query.limit) : 10
|
|
|
|
|
|
|
|
const categories = libraryHelpers.buildPersonalizedShelves(req.user, libraryItems, mediaType, this.db.series, this.db.authors, limitPerShelf)
|
|
|
|
res.json(categories)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Remove old personalized function with all its helper functions
|
|
|
|
// old personalized function looped through the library items many times
|
|
|
|
// api/libraries/:id/personalized-old
|
2022-03-11 01:45:02 +01:00
|
|
|
async getLibraryUserPersonalized(req, res) {
|
2022-03-26 23:41:26 +01:00
|
|
|
var mediaType = req.library.mediaType
|
|
|
|
var isPodcastLibrary = mediaType == 'podcast'
|
2022-03-11 01:45:02 +01:00
|
|
|
var libraryItems = req.libraryItems
|
|
|
|
var limitPerShelf = req.query.limit && !isNaN(req.query.limit) ? Number(req.query.limit) : 12
|
2022-04-24 23:56:30 +02:00
|
|
|
var minified = req.query.minified == '1'
|
2022-03-11 01:45:02 +01:00
|
|
|
|
2022-04-23 02:31:11 +02:00
|
|
|
var itemsWithUserProgress = libraryHelpers.getMediaProgressWithItems(req.user, libraryItems)
|
2022-03-11 01:45:02 +01:00
|
|
|
var categories = [
|
|
|
|
{
|
2022-03-26 23:41:26 +01:00
|
|
|
id: 'continue-listening',
|
|
|
|
label: 'Continue Listening',
|
2022-04-23 02:31:11 +02:00
|
|
|
type: isPodcastLibrary ? 'episode' : req.library.mediaType,
|
2022-03-17 18:25:12 +01:00
|
|
|
entities: libraryHelpers.getItemsMostRecentlyListened(itemsWithUserProgress, limitPerShelf, minified)
|
2022-03-11 01:45:02 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'recently-added',
|
|
|
|
label: 'Recently Added',
|
2022-03-17 18:25:12 +01:00
|
|
|
type: req.library.mediaType,
|
|
|
|
entities: libraryHelpers.getItemsMostRecentlyAdded(libraryItems, limitPerShelf, minified)
|
2022-03-11 01:45:02 +01:00
|
|
|
},
|
|
|
|
{
|
2022-03-26 23:41:26 +01:00
|
|
|
id: 'listen-again',
|
|
|
|
label: 'Listen Again',
|
2022-04-23 02:31:11 +02:00
|
|
|
type: isPodcastLibrary ? 'episode' : req.library.mediaType,
|
2022-03-17 18:25:12 +01:00
|
|
|
entities: libraryHelpers.getItemsMostRecentlyFinished(itemsWithUserProgress, limitPerShelf, minified)
|
2022-03-11 01:45:02 +01:00
|
|
|
}
|
|
|
|
].filter(cats => { // Remove categories with no items
|
|
|
|
return cats.entities.length
|
|
|
|
})
|
|
|
|
|
2022-03-27 23:16:08 +02:00
|
|
|
// New Series section
|
|
|
|
// TODO: optimize and move to libraryHelpers
|
|
|
|
if (!isPodcastLibrary) {
|
|
|
|
var series = this.db.series.map(se => {
|
|
|
|
var books = libraryItems.filter(li => li.media.metadata.hasSeries(se.id))
|
|
|
|
if (!books.length) return null
|
|
|
|
books = books.map(b => {
|
|
|
|
var json = b.toJSONMinified()
|
|
|
|
json.sequence = b.media.metadata.getSeriesSequence(se.id)
|
|
|
|
return json
|
|
|
|
})
|
|
|
|
books = naturalSort(books).asc(b => b.sequence)
|
|
|
|
return {
|
|
|
|
id: se.id,
|
|
|
|
name: se.name,
|
|
|
|
type: 'series',
|
|
|
|
addedAt: se.addedAt,
|
|
|
|
books
|
|
|
|
}
|
|
|
|
}).filter(se => se).sort((a, b) => a.addedAt - b.addedAt).slice(0, 5)
|
|
|
|
|
|
|
|
if (series.length) {
|
|
|
|
categories.push({
|
|
|
|
id: 'recent-series',
|
|
|
|
label: 'Recent Series',
|
|
|
|
type: 'series',
|
|
|
|
entities: series
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var authors = this.db.authors.map(author => {
|
|
|
|
var books = libraryItems.filter(li => li.media.metadata.hasAuthor(author.id))
|
|
|
|
if (!books.length) return null
|
|
|
|
// books = books.map(b => b.toJSONMinified())
|
|
|
|
return {
|
|
|
|
...author.toJSON(),
|
|
|
|
numBooks: books.length
|
|
|
|
}
|
|
|
|
}).filter(au => au).sort((a, b) => a.addedAt - b.addedAt).slice(0, 10)
|
|
|
|
if (authors.length) {
|
|
|
|
categories.push({
|
|
|
|
id: 'newest-authors',
|
|
|
|
label: 'Newest Authors',
|
|
|
|
type: 'authors',
|
|
|
|
entities: authors
|
|
|
|
})
|
|
|
|
}
|
2022-04-23 02:31:11 +02:00
|
|
|
} else {
|
|
|
|
var episodesRecentlyAdded = libraryHelpers.getEpisodesRecentlyAdded(libraryItems, limitPerShelf, minified)
|
|
|
|
if (episodesRecentlyAdded.length) {
|
|
|
|
categories.splice(1, 0, {
|
|
|
|
id: 'episodes-recently-added',
|
|
|
|
label: 'Newest Episodes',
|
|
|
|
type: 'episode',
|
|
|
|
entities: episodesRecentlyAdded
|
|
|
|
})
|
2021-12-01 03:02:40 +01:00
|
|
|
}
|
2022-04-23 02:31:11 +02:00
|
|
|
}
|
2021-12-01 03:02:40 +01:00
|
|
|
|
|
|
|
res.json(categories)
|
|
|
|
}
|
|
|
|
|
2021-11-22 03:00:40 +01:00
|
|
|
// PATCH: Change the order of libraries
|
|
|
|
async reorder(req, res) {
|
|
|
|
if (!req.user.isRoot) {
|
2022-03-18 01:10:47 +01:00
|
|
|
Logger.error('[LibraryController] ReorderLibraries invalid user', req.user)
|
2022-02-15 23:36:22 +01:00
|
|
|
return res.sendStatus(403)
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var orderdata = req.body
|
|
|
|
var hasUpdates = false
|
|
|
|
for (let i = 0; i < orderdata.length; i++) {
|
|
|
|
var library = this.db.libraries.find(lib => lib.id === orderdata[i].id)
|
|
|
|
if (!library) {
|
2022-03-18 01:10:47 +01:00
|
|
|
Logger.error(`[LibraryController] Invalid library not found in reorder ${orderdata[i].id}`)
|
2021-11-22 03:00:40 +01:00
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
if (library.update({ displayOrder: orderdata[i].newOrder })) {
|
|
|
|
hasUpdates = true
|
|
|
|
await this.db.updateEntity('library', library)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasUpdates) {
|
2022-03-27 16:45:28 +02:00
|
|
|
this.db.libraries.sort((a, b) => a.displayOrder - b.displayOrder)
|
|
|
|
Logger.debug(`[LibraryController] Updated library display orders`)
|
2021-11-22 03:00:40 +01:00
|
|
|
} else {
|
2022-03-27 16:45:28 +02:00
|
|
|
Logger.debug(`[LibraryController] Library orders were up to date`)
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var libraries = this.db.libraries.map(lib => lib.toJSON())
|
|
|
|
res.json(libraries)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET: Global library search
|
|
|
|
search(req, res) {
|
|
|
|
if (!req.query.q) {
|
|
|
|
return res.status(400).send('No query string')
|
|
|
|
}
|
2022-03-13 18:39:12 +01:00
|
|
|
var libraryItems = req.libraryItems
|
2021-12-02 02:07:03 +01:00
|
|
|
var maxResults = req.query.limit && !isNaN(req.query.limit) ? Number(req.query.limit) : 12
|
2021-11-22 03:00:40 +01:00
|
|
|
|
2022-03-13 18:39:12 +01:00
|
|
|
var itemMatches = []
|
2021-11-22 03:00:40 +01:00
|
|
|
var authorMatches = {}
|
|
|
|
var seriesMatches = {}
|
|
|
|
var tagMatches = {}
|
|
|
|
|
2022-03-13 18:39:12 +01:00
|
|
|
libraryItems.forEach((li) => {
|
|
|
|
var queryResult = li.searchQuery(req.query.q)
|
|
|
|
if (queryResult.matchKey) {
|
|
|
|
itemMatches.push({
|
2022-04-12 23:54:52 +02:00
|
|
|
libraryItem: li.toJSONExpanded(),
|
2022-03-13 18:39:12 +01:00
|
|
|
matchKey: queryResult.matchKey,
|
|
|
|
matchText: queryResult.matchText
|
|
|
|
})
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2022-03-13 18:39:12 +01:00
|
|
|
if (queryResult.series && queryResult.series.length) {
|
|
|
|
queryResult.series.forEach((se) => {
|
|
|
|
if (!seriesMatches[se.id]) {
|
|
|
|
var _series = this.db.series.find(_se => _se.id === se.id)
|
|
|
|
if (_series) seriesMatches[se.id] = { series: _series.toJSON(), books: [li.toJSON()] }
|
2021-12-03 02:02:38 +01:00
|
|
|
} else {
|
2022-03-13 18:39:12 +01:00
|
|
|
seriesMatches[se.id].books.push(li.toJSON())
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-03-13 18:39:12 +01:00
|
|
|
if (queryResult.authors && queryResult.authors.length) {
|
|
|
|
queryResult.authors.forEach((au) => {
|
|
|
|
if (!authorMatches[au.id]) {
|
|
|
|
var _author = this.db.authors.find(_au => _au.id === au.id)
|
|
|
|
if (_author) {
|
|
|
|
authorMatches[au.id] = _author.toJSON()
|
|
|
|
authorMatches[au.id].numBooks = 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
authorMatches[au.id].numBooks++
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2022-03-13 18:39:12 +01:00
|
|
|
})
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
if (queryResult.tags && queryResult.tags.length) {
|
|
|
|
queryResult.tags.forEach((tag) => {
|
|
|
|
if (!tagMatches[tag]) {
|
2022-03-13 18:39:12 +01:00
|
|
|
tagMatches[tag] = { name: tag, books: [li.toJSON()] }
|
2021-11-22 03:00:40 +01:00
|
|
|
} else {
|
2022-03-13 18:39:12 +01:00
|
|
|
tagMatches[tag].books.push(li.toJSON())
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2022-03-14 15:56:24 +01:00
|
|
|
var itemKey = req.library.mediaType
|
2021-12-02 02:07:03 +01:00
|
|
|
var results = {
|
2022-03-13 18:39:12 +01:00
|
|
|
[itemKey]: itemMatches.slice(0, maxResults),
|
2021-11-22 03:00:40 +01:00
|
|
|
tags: Object.values(tagMatches).slice(0, maxResults),
|
|
|
|
authors: Object.values(authorMatches).slice(0, maxResults),
|
|
|
|
series: Object.values(seriesMatches).slice(0, maxResults)
|
2021-12-02 02:07:03 +01:00
|
|
|
}
|
|
|
|
res.json(results)
|
|
|
|
}
|
|
|
|
|
|
|
|
async stats(req, res) {
|
2022-03-13 23:33:50 +01:00
|
|
|
var libraryItems = req.libraryItems
|
|
|
|
var authorsWithCount = libraryHelpers.getAuthorsWithCount(libraryItems)
|
|
|
|
var genresWithCount = libraryHelpers.getGenresWithCount(libraryItems)
|
|
|
|
var durationStats = libraryHelpers.getItemDurationStats(libraryItems)
|
2021-12-02 02:07:03 +01:00
|
|
|
var stats = {
|
2022-03-13 23:33:50 +01:00
|
|
|
totalItems: libraryItems.length,
|
2021-12-02 02:07:03 +01:00
|
|
|
totalAuthors: Object.keys(authorsWithCount).length,
|
|
|
|
totalGenres: Object.keys(genresWithCount).length,
|
2022-03-13 23:33:50 +01:00
|
|
|
totalDuration: durationStats.totalDuration,
|
|
|
|
longestItems: durationStats.longestItems,
|
|
|
|
numAudioTracks: durationStats.numAudioTracks,
|
|
|
|
totalSize: libraryHelpers.getLibraryItemsTotalSize(libraryItems),
|
2021-12-02 02:07:03 +01:00
|
|
|
authorsWithCount,
|
|
|
|
genresWithCount
|
|
|
|
}
|
|
|
|
res.json(stats)
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2021-12-01 03:02:40 +01:00
|
|
|
|
2021-12-03 02:02:38 +01:00
|
|
|
async getAuthors(req, res) {
|
2022-03-13 01:50:31 +01:00
|
|
|
var libraryItems = req.libraryItems
|
2021-12-03 02:02:38 +01:00
|
|
|
var authors = {}
|
2022-03-13 01:50:31 +01:00
|
|
|
libraryItems.forEach((li) => {
|
|
|
|
if (li.media.metadata.authors && li.media.metadata.authors.length) {
|
|
|
|
li.media.metadata.authors.forEach((au) => {
|
|
|
|
if (!authors[au.id]) {
|
|
|
|
var _author = this.db.authors.find(_au => _au.id === au.id)
|
|
|
|
if (_author) {
|
|
|
|
authors[au.id] = _author.toJSON()
|
|
|
|
authors[au.id].numBooks = 1
|
2021-12-03 02:02:38 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-03-13 01:50:31 +01:00
|
|
|
authors[au.id].numBooks++
|
2021-12-03 02:02:38 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2022-04-25 00:15:41 +02:00
|
|
|
|
|
|
|
res.json(naturalSort(Object.values(authors)).asc(au => au.name))
|
2021-12-03 02:02:38 +01:00
|
|
|
}
|
|
|
|
|
2022-04-21 01:05:09 +02:00
|
|
|
async matchAll(req, res) {
|
2022-02-15 23:36:22 +01:00
|
|
|
if (!req.user.isRoot) {
|
2022-04-21 01:05:09 +02:00
|
|
|
Logger.error(`[LibraryController] Non-root user attempted to match library items`, req.user)
|
2022-02-15 23:36:22 +01:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2022-04-21 01:05:09 +02:00
|
|
|
this.scanner.matchLibraryItems(req.library)
|
2022-02-15 23:36:22 +01:00
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2022-03-18 17:51:55 +01:00
|
|
|
// GET: api/scan (Root)
|
|
|
|
async scan(req, res) {
|
|
|
|
if (!req.user.isRoot) {
|
|
|
|
Logger.error(`[LibraryController] Non-root user attempted to scan library`, req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
var options = {
|
|
|
|
forceRescan: req.query.force == 1
|
|
|
|
}
|
|
|
|
res.sendStatus(200)
|
|
|
|
await this.scanner.scan(req.library, options)
|
|
|
|
Logger.info('[LibraryController] Scan complete')
|
|
|
|
}
|
|
|
|
|
2021-12-01 03:02:40 +01:00
|
|
|
middleware(req, res, next) {
|
2022-01-16 21:10:46 +01:00
|
|
|
var librariesAccessible = req.user.librariesAccessible || []
|
|
|
|
if (librariesAccessible && librariesAccessible.length && !librariesAccessible.includes(req.params.id)) {
|
|
|
|
Logger.warn(`[LibraryController] Library ${req.params.id} not accessible to user ${req.user.username}`)
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
2021-12-01 03:02:40 +01:00
|
|
|
var library = this.db.libraries.find(lib => lib.id === req.params.id)
|
|
|
|
if (!library) {
|
|
|
|
return res.status(404).send('Library not found')
|
|
|
|
}
|
|
|
|
req.library = library
|
2022-03-20 12:29:08 +01:00
|
|
|
req.libraryItems = this.db.libraryItems.filter(li => {
|
|
|
|
return li.libraryId === library.id && req.user.checkCanAccessLibraryItemWithTags(li.media.tags)
|
|
|
|
})
|
2021-12-01 03:02:40 +01:00
|
|
|
next()
|
|
|
|
}
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
module.exports = new LibraryController()
|