mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-12-23 04:09:38 +00:00
Change Library object use mediaCategory, allow adding new manual folder path, validate folder paths, fix Watcher re-init after folder path updates
This commit is contained in:
parent
7348432594
commit
deea6702f0
10 changed files with 98 additions and 47 deletions
|
|
@ -85,12 +85,15 @@ class FolderWatcher extends EventEmitter {
|
|||
if (libwatcher) {
|
||||
libwatcher.name = library.name
|
||||
|
||||
// If any folder paths were added or removed then re-init watcher
|
||||
var pathsToAdd = library.folderPaths.filter(path => !libwatcher.paths.includes(path))
|
||||
if (pathsToAdd.length) {
|
||||
Logger.info(`[Watcher] Adding paths to library watcher "${library.name}"`)
|
||||
libwatcher.paths = library.folderPaths
|
||||
libwatcher.folders = library.folders
|
||||
libwatcher.watcher.watchPaths(pathsToAdd)
|
||||
var pathsRemoved = libwatcher.paths.filter(path => !library.folderPaths.includes(path))
|
||||
if (pathsToAdd.length || pathsRemoved.length) {
|
||||
Logger.info(`[Watcher] Re-Initializing watcher for "${library.name}".`)
|
||||
|
||||
libwatcher.watcher.close()
|
||||
this.libraryWatchers = this.libraryWatchers.filter(lw => lw.id !== libwatcher.id)
|
||||
this.buildLibraryWatcher(library)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,6 @@
|
|||
const Path = require('path')
|
||||
const fs = require('fs-extra')
|
||||
const filePerms = require('../utils/filePerms')
|
||||
const Logger = require('../Logger')
|
||||
const Library = require('../objects/Library')
|
||||
const { sort, createNewSortInstance } = require('fast-sort')
|
||||
|
|
@ -51,6 +54,30 @@ class LibraryController {
|
|||
async update(req, res) {
|
||||
var library = req.library
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var hasUpdates = library.update(req.body)
|
||||
// TODO: Should check if this is an update to folder paths or name only
|
||||
if (hasUpdates) {
|
||||
|
|
@ -400,7 +427,7 @@ class LibraryController {
|
|||
})
|
||||
}
|
||||
})
|
||||
var itemKey = req.library.itemMediaType
|
||||
var itemKey = req.library.mediaType
|
||||
var results = {
|
||||
[itemKey]: itemMatches.slice(0, maxResults),
|
||||
tags: Object.values(tagMatches).slice(0, maxResults),
|
||||
|
|
|
|||
|
|
@ -8,7 +8,8 @@ class Library {
|
|||
this.folders = []
|
||||
this.displayOrder = 1
|
||||
this.icon = 'database'
|
||||
this.mediaType = 'default'
|
||||
this.mediaCategory = 'default' // default, podcast, book, audiobook, comic
|
||||
this.mediaType = 'book' // book, podcast
|
||||
this.provider = 'google'
|
||||
this.disableWatcher = false
|
||||
|
||||
|
|
@ -25,9 +26,6 @@ class Library {
|
|||
get folderPaths() {
|
||||
return this.folders.map(f => f.fullPath)
|
||||
}
|
||||
get itemMediaType() {
|
||||
return this.mediaType === 'podcast' ? 'podcast' : 'book'
|
||||
}
|
||||
|
||||
construct(library) {
|
||||
this.id = library.id
|
||||
|
|
@ -35,12 +33,31 @@ class Library {
|
|||
this.folders = (library.folders || []).map(f => new Folder(f))
|
||||
this.displayOrder = library.displayOrder || 1
|
||||
this.icon = library.icon || 'database'
|
||||
this.mediaType = library.mediaType || 'default'
|
||||
this.mediaCategory = library.mediaCategory || library.mediaType || 'default' // mediaCategory used to be mediaType
|
||||
this.mediaType = library.mediaType
|
||||
this.provider = library.provider || 'google'
|
||||
this.disableWatcher = !!library.disableWatcher
|
||||
|
||||
this.createdAt = library.createdAt
|
||||
this.lastUpdate = library.lastUpdate
|
||||
this.cleanOldValues() // mediaCategory and mediaType were changed for v2
|
||||
}
|
||||
|
||||
cleanOldValues() {
|
||||
if (this.mediaCategory.endsWith('s')) {
|
||||
this.mediaCategory = this.mediaCategory.slice(0, -1)
|
||||
}
|
||||
var availableCategories = ['default', 'audiobook', 'book', 'comic', 'podcast']
|
||||
if (!availableCategories.includes(this.mediaCategory)) {
|
||||
this.mediaCategory = 'default'
|
||||
}
|
||||
if (!availableCategories.includes(this.icon)) {
|
||||
this.icon = this.mediaCategory
|
||||
}
|
||||
if (!this.mediaType || (this.mediaType !== 'podcast' && this.mediaType !== 'book')) {
|
||||
if (this.mediaCategory === 'podcast') this.mediaType = 'podcast'
|
||||
else this.mediaType = 'book'
|
||||
}
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
|
|
@ -50,6 +67,7 @@ class Library {
|
|||
folders: (this.folders || []).map(f => f.toJSON()),
|
||||
displayOrder: this.displayOrder,
|
||||
icon: this.icon,
|
||||
mediaCategory: this.mediaCategory,
|
||||
mediaType: this.mediaType,
|
||||
provider: this.provider,
|
||||
disableWatcher: this.disableWatcher,
|
||||
|
|
@ -77,7 +95,8 @@ class Library {
|
|||
}
|
||||
this.displayOrder = data.displayOrder || 1
|
||||
this.icon = data.icon || 'database'
|
||||
this.mediaType = data.mediaType || 'default'
|
||||
this.mediaCategory = data.mediaCategory || 'default'
|
||||
this.mediaType = data.mediaType || 'book'
|
||||
this.disableWatcher = !!data.disableWatcher
|
||||
this.createdAt = Date.now()
|
||||
this.lastUpdate = Date.now()
|
||||
|
|
@ -85,22 +104,14 @@ class Library {
|
|||
|
||||
update(payload) {
|
||||
var hasUpdates = false
|
||||
if (payload.name && payload.name !== this.name) {
|
||||
this.name = payload.name
|
||||
hasUpdates = true
|
||||
}
|
||||
if (payload.provider && payload.provider !== this.provider) {
|
||||
this.provider = payload.provider
|
||||
hasUpdates = true
|
||||
}
|
||||
if (payload.mediaType && payload.mediaType !== this.mediaType) {
|
||||
this.mediaType = payload.mediaType
|
||||
hasUpdates = true
|
||||
}
|
||||
if (payload.icon && payload.icon !== this.icon) {
|
||||
this.icon = payload.icon
|
||||
hasUpdates = true
|
||||
}
|
||||
|
||||
var keysToCheck = ['name', 'provider', 'mediaCategory', 'mediaType', 'icon']
|
||||
keysToCheck.forEach((key) => {
|
||||
if (payload[key] && payload[key] !== this[key]) {
|
||||
this[key] = payload[key]
|
||||
hasUpdates = true
|
||||
}
|
||||
})
|
||||
|
||||
if (payload.disableWatcher !== this.disableWatcher) {
|
||||
this.disableWatcher = !!payload.disableWatcher
|
||||
|
|
|
|||
|
|
@ -65,7 +65,6 @@ class LibraryFile {
|
|||
this.metadata = fileMetadata
|
||||
this.addedAt = Date.now()
|
||||
this.updatedAt = Date.now()
|
||||
console.log('Library file set from path', path, 'rel path', relPath)
|
||||
}
|
||||
}
|
||||
module.exports = LibraryFile
|
||||
Loading…
Add table
Add a link
Reference in a new issue