From e054b9a54ca392930901695e2a74b2e306c7a5b0 Mon Sep 17 00:00:00 2001 From: mikiher Date: Tue, 24 Oct 2023 13:35:43 +0000 Subject: [PATCH 1/2] Add API to update a path on a watched library folder --- server/controllers/MiscController.js | 48 ++++++++++++++++++++++++++++ server/routers/ApiRouter.js | 1 + 2 files changed, 49 insertions(+) diff --git a/server/controllers/MiscController.js b/server/controllers/MiscController.js index ffa4e2c2..fb6124df 100644 --- a/server/controllers/MiscController.js +++ b/server/controllers/MiscController.js @@ -527,6 +527,54 @@ class MiscController { }) } + /** + * POST: /api/watcher/update + * Update a watch path + * Req.body { libraryId, path, type, [oldPath] } + * type = add, unlink, rename + * oldPath = required only for rename + * @param {*} req + * @param {*} res + */ + updateWatchedPath(req, res) { + if (!req.user.isAdminOrUp) { + Logger.error(`[MiscController] Non-admin user attempted to updateWatchedPath`) + return res.sendStatus(404) + } + + const libraryId = req.body.libraryId + const path = req.body.path + const type = req.body.type + if (!libraryId || !path || !type) { + Logger.error(`[MiscController] Invalid request body for updateWatchedPath. libraryId: "${libraryId}", path: "${path}", type: "${type}"`) + return res.sendStatus(400) + } + + switch (type) { + case 'add': + this.watcher.onNewFile(libraryId, path) + break; + case 'unlink': + this.watcher.onFileRemoved(libraryId, path) + break; + case 'rename': + const oldPath = req.body.oldPath + if (!oldPath) { + Logger.error(`[MiscController] Invalid request body for updateWatchedPath. oldPath is required for rename.`) + return res.sendStatus(400) + } + this.watcher.onRename(libraryId, oldPath, path) + break; + default: + Logger.error(`[MiscController] Invalid type for updateWatchedPath. type: "${type}"`) + return res.sendStatus(400) + } + + res.sendStatus(200) + + } + + validateCronExpression(req, res) { const expression = req.body.expression if (!expression) { diff --git a/server/routers/ApiRouter.js b/server/routers/ApiRouter.js index b40c3d80..c4ac0327 100644 --- a/server/routers/ApiRouter.js +++ b/server/routers/ApiRouter.js @@ -308,6 +308,7 @@ class ApiRouter { this.router.post('/genres/rename', MiscController.renameGenre.bind(this)) this.router.delete('/genres/:genre', MiscController.deleteGenre.bind(this)) this.router.post('/validate-cron', MiscController.validateCronExpression.bind(this)) + this.router.post('/watcher/update', MiscController.updateWatchedPath.bind(this)) } async getDirectories(dir, relpath, excludedDirs, level = 0) { From f9c4dd24574600c317cf0b992de0defde4eca73b Mon Sep 17 00:00:00 2001 From: advplyr Date: Thu, 26 Oct 2023 16:41:54 -0500 Subject: [PATCH 2/2] Update watcher function calls, add js docs --- server/controllers/MiscController.js | 24 ++++++++++++------------ server/routers/ApiRouter.js | 1 + 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/server/controllers/MiscController.js b/server/controllers/MiscController.js index fb6124df..f4f1703d 100644 --- a/server/controllers/MiscController.js +++ b/server/controllers/MiscController.js @@ -528,14 +528,16 @@ class MiscController { } /** - * POST: /api/watcher/update - * Update a watch path - * Req.body { libraryId, path, type, [oldPath] } - * type = add, unlink, rename - * oldPath = required only for rename - * @param {*} req - * @param {*} res - */ + * POST: /api/watcher/update + * Update a watch path + * Req.body { libraryId, path, type, [oldPath] } + * type = add, unlink, rename + * oldPath = required only for rename + * @this import('../routers/ApiRouter') + * + * @param {import('express').Request} req + * @param {import('express').Response} res + */ updateWatchedPath(req, res) { if (!req.user.isAdminOrUp) { Logger.error(`[MiscController] Non-admin user attempted to updateWatchedPath`) @@ -552,7 +554,7 @@ class MiscController { switch (type) { case 'add': - this.watcher.onNewFile(libraryId, path) + this.watcher.onFileAdded(libraryId, path) break; case 'unlink': this.watcher.onFileRemoved(libraryId, path) @@ -563,7 +565,7 @@ class MiscController { Logger.error(`[MiscController] Invalid request body for updateWatchedPath. oldPath is required for rename.`) return res.sendStatus(400) } - this.watcher.onRename(libraryId, oldPath, path) + this.watcher.onFileRename(libraryId, oldPath, path) break; default: Logger.error(`[MiscController] Invalid type for updateWatchedPath. type: "${type}"`) @@ -571,10 +573,8 @@ class MiscController { } res.sendStatus(200) - } - validateCronExpression(req, res) { const expression = req.body.expression if (!expression) { diff --git a/server/routers/ApiRouter.js b/server/routers/ApiRouter.js index c4ac0327..41b24716 100644 --- a/server/routers/ApiRouter.js +++ b/server/routers/ApiRouter.js @@ -39,6 +39,7 @@ class ApiRouter { this.playbackSessionManager = Server.playbackSessionManager this.abMergeManager = Server.abMergeManager this.backupManager = Server.backupManager + /** @type {import('../Watcher')} */ this.watcher = Server.watcher this.podcastManager = Server.podcastManager this.audioMetadataManager = Server.audioMetadataManager