From deea6702f01b3f5183e53d10a2f5151250fe11cf Mon Sep 17 00:00:00 2001 From: advplyr Date: Mon, 14 Mar 2022 09:56:24 -0500 Subject: [PATCH] Change Library object use mediaCategory, allow adding new manual folder path, validate folder paths, fix Watcher re-init after folder path updates --- .../{audiobooks-svg.vue => audiobook-svg.vue} | 0 .../icons/{books-svg.vue => book-svg.vue} | 0 .../icons/{comics-svg.vue => comic-svg.vue} | 0 .../{podcasts-svg.vue => podcast-svg.vue} | 0 .../modals/libraries/EditLibrary.vue | 37 ++++++++----- ...TypePicker.vue => MediaCategoryPicker.vue} | 10 ++-- server/Watcher.js | 13 +++-- server/controllers/LibraryController.js | 29 +++++++++- server/objects/Library.js | 55 +++++++++++-------- server/objects/files/LibraryFile.js | 1 - 10 files changed, 98 insertions(+), 47 deletions(-) rename client/components/icons/{audiobooks-svg.vue => audiobook-svg.vue} (100%) rename client/components/icons/{books-svg.vue => book-svg.vue} (100%) rename client/components/icons/{comics-svg.vue => comic-svg.vue} (100%) rename client/components/icons/{podcasts-svg.vue => podcast-svg.vue} (100%) rename client/components/ui/{MediaTypePicker.vue => MediaCategoryPicker.vue} (95%) diff --git a/client/components/icons/audiobooks-svg.vue b/client/components/icons/audiobook-svg.vue similarity index 100% rename from client/components/icons/audiobooks-svg.vue rename to client/components/icons/audiobook-svg.vue diff --git a/client/components/icons/books-svg.vue b/client/components/icons/book-svg.vue similarity index 100% rename from client/components/icons/books-svg.vue rename to client/components/icons/book-svg.vue diff --git a/client/components/icons/comics-svg.vue b/client/components/icons/comic-svg.vue similarity index 100% rename from client/components/icons/comics-svg.vue rename to client/components/icons/comic-svg.vue diff --git a/client/components/icons/podcasts-svg.vue b/client/components/icons/podcast-svg.vue similarity index 100% rename from client/components/icons/podcasts-svg.vue rename to client/components/icons/podcast-svg.vue diff --git a/client/components/modals/libraries/EditLibrary.vue b/client/components/modals/libraries/EditLibrary.vue index 2e275b9f..2c1b056a 100644 --- a/client/components/modals/libraries/EditLibrary.vue +++ b/client/components/modals/libraries/EditLibrary.vue @@ -11,7 +11,7 @@
- +
@@ -22,10 +22,16 @@

Folders

folder - + close
-

No folders

+ + +
+ folder + +
+ Browse for Folder
@@ -61,11 +67,12 @@ export default { data() { return { name: '', - provider: '', - mediaType: '', + provider: 'google', + mediaCategory: '', folders: [], showDirectoryPicker: false, - disableWatcher: false + disableWatcher: false, + newFolderPath: '' } }, computed: { @@ -83,7 +90,7 @@ export default { var newfolderpaths = this.folderPaths.join(',') var origfolderpaths = this.library.folders.map((f) => f.fullPath).join(',') - return newfolderpaths === origfolderpaths && this.name === this.library.name && this.provider === this.library.provider && this.disableWatcher === this.library.disableWatcher && this.mediaType === this.library.mediaType + return newfolderpaths === origfolderpaths && this.name === this.library.name && this.provider === this.library.provider && this.disableWatcher === this.library.disableWatcher && this.mediaCategory === this.library.mediaCategory && !this.newFolderPath }, providers() { return this.$store.state.scanners.providers @@ -103,10 +110,10 @@ export default { }, init() { this.name = this.library ? this.library.name : '' - this.provider = this.library ? this.library.provider : '' + this.provider = this.library ? this.library.provider : 'google' this.folders = this.library ? this.library.folders.map((p) => ({ ...p })) : [] this.disableWatcher = this.library ? !!this.library.disableWatcher : false - this.mediaType = this.library ? this.library.mediaType : 'default' + this.mediaCategory = this.library ? this.library.mediaCategory : 'default' this.showDirectoryPicker = false }, selectFolder(fullPath) { @@ -114,6 +121,10 @@ export default { this.showDirectoryPicker = false }, submit() { + if (this.newFolderPath) { + this.folders.push({ fullPath: this.newFolderPath }) + } + if (this.library) { this.updateLibrary() } else { @@ -133,8 +144,8 @@ export default { name: this.name, provider: this.provider, folders: this.folders, - mediaType: this.mediaType, - icon: this.mediaType, + mediaCategory: this.mediaCategory, + icon: this.mediaCategory, disableWatcher: this.disableWatcher } @@ -169,8 +180,8 @@ export default { name: this.name, provider: this.provider, folders: this.folders, - mediaType: this.mediaType, - icon: this.mediaType, + mediaCategory: this.mediaCategory, + icon: this.mediaCategory, disableWatcher: this.disableWatcher } diff --git a/client/components/ui/MediaTypePicker.vue b/client/components/ui/MediaCategoryPicker.vue similarity index 95% rename from client/components/ui/MediaTypePicker.vue rename to client/components/ui/MediaCategoryPicker.vue index 0a03780b..fa270412 100644 --- a/client/components/ui/MediaTypePicker.vue +++ b/client/components/ui/MediaCategoryPicker.vue @@ -34,7 +34,7 @@ export default { disabled: Boolean, label: { type: String, - default: 'Media Type' + default: 'Media Category' } }, data() { @@ -51,19 +51,19 @@ export default { name: 'Default' }, { - id: 'audiobooks', + id: 'audiobook', name: 'Audiobooks' }, { - id: 'books', + id: 'book', name: 'Books' }, { - id: 'podcasts', + id: 'podcast', name: 'Podcasts' }, { - id: 'comics', + id: 'comic', name: 'Comics' } ] diff --git a/server/Watcher.js b/server/Watcher.js index 18a828a4..2a54c9ef 100644 --- a/server/Watcher.js +++ b/server/Watcher.js @@ -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) } } } diff --git a/server/controllers/LibraryController.js b/server/controllers/LibraryController.js index 45edd679..50dfe75a 100644 --- a/server/controllers/LibraryController.js +++ b/server/controllers/LibraryController.js @@ -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), diff --git a/server/objects/Library.js b/server/objects/Library.js index af68d447..f5418c4c 100644 --- a/server/objects/Library.js +++ b/server/objects/Library.js @@ -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 diff --git a/server/objects/files/LibraryFile.js b/server/objects/files/LibraryFile.js index b5296dc3..9cbdc5f9 100644 --- a/server/objects/files/LibraryFile.js +++ b/server/objects/files/LibraryFile.js @@ -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 \ No newline at end of file