Skip to content

Commit

Permalink
Merge pull request #2249 from mikiher/watcher-update-api
Browse files Browse the repository at this point in the history
Add API to update a path on a watched library folder
  • Loading branch information
advplyr committed Oct 26, 2023
2 parents 0c23da7 + f9c4dd2 commit 5c1c511
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
48 changes: 48 additions & 0 deletions server/controllers/MiscController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
* @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`)
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.onFileAdded(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.onFileRename(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) {
Expand Down
2 changes: 2 additions & 0 deletions server/routers/ApiRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -308,6 +309,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) {
Expand Down

0 comments on commit 5c1c511

Please sign in to comment.