From 02d997897c8c947b5a2140970330060f46f5416d Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 2 Oct 2022 14:31:04 -0500 Subject: [PATCH] Add:Cancel m4b merge button #1008 --- client/pages/audiobook/_id/manage.vue | 21 +++++++++++++++++++-- server/controllers/MiscController.js | 27 +++++++++++++++++++++------ server/managers/AbMergeManager.js | 9 +++++++++ server/managers/TaskManager.js | 2 +- server/routers/ApiRouter.js | 3 ++- 5 files changed, 52 insertions(+), 10 deletions(-) diff --git a/client/pages/audiobook/_id/manage.vue b/client/pages/audiobook/_id/manage.vue index af277048..373ea627 100644 --- a/client/pages/audiobook/_id/manage.vue +++ b/client/pages/audiobook/_id/manage.vue @@ -67,6 +67,7 @@

Embed Finished!

+ Cancel Encode Start M4B Encode

M4B Failed! {{ taskError }}

M4B Finished!

@@ -166,7 +167,8 @@ export default { audiofilesFinished: {}, isFinished: false, toneObject: null, - selectedTool: 'embed' + selectedTool: 'embed', + isCancelingEncode: false } }, watch: { @@ -230,10 +232,25 @@ export default { } }, methods: { + cancelEncodeClick() { + this.isCancelingEncode = true + this.$axios + .$post(`/api/encode-m4b/${this.libraryItemId}/cancel`) + .then(() => { + this.$toast.success('Encode canceled') + }) + .catch((error) => { + console.error('Failed to cancel encode', error) + this.$toast.error('Failed to cancel encode') + }) + .finally(() => { + this.isCancelingEncode = false + }) + }, encodeM4bClick() { this.processing = true this.$axios - .$get(`/api/audiobook-merge/${this.libraryItemId}`) + .$get(`/api/encode-m4b/${this.libraryItemId}`) .then(() => { console.log('Ab m4b merge started') }) diff --git a/server/controllers/MiscController.js b/server/controllers/MiscController.js index fa8ca416..8cba1acc 100644 --- a/server/controllers/MiscController.js +++ b/server/controllers/MiscController.js @@ -82,26 +82,26 @@ class MiscController { res.sendStatus(200) } - // GET: api/audiobook-merge/:id - async mergeAudiobook(req, res) { + // GET: api/encode-m4b/:id + async encodeM4b(req, res) { if (!req.user.isAdminOrUp) { - Logger.error('[MiscController] mergeAudiobook: Non-admin user attempting to make m4b', req.user) + Logger.error('[MiscController] encodeM4b: Non-admin user attempting to make m4b', req.user) return res.sendStatus(403) } var libraryItem = this.db.getLibraryItem(req.params.id) if (!libraryItem || libraryItem.isMissing || libraryItem.isInvalid) { - Logger.error(`[MiscController] mergeAudiboook: library item not found or invalid ${req.params.id}`) + Logger.error(`[MiscController] encodeM4b: library item not found or invalid ${req.params.id}`) return res.status(404).send('Audiobook not found') } if (libraryItem.mediaType !== 'book') { - Logger.error(`[MiscController] mergeAudiboook: Invalid library item ${req.params.id}: not a book`) + Logger.error(`[MiscController] encodeM4b: Invalid library item ${req.params.id}: not a book`) return res.status(500).send('Invalid library item: not a book') } if (libraryItem.media.tracks.length <= 0) { - Logger.error(`[MiscController] mergeAudiboook: Invalid audiobook ${req.params.id}: no audio tracks`) + Logger.error(`[MiscController] encodeM4b: Invalid audiobook ${req.params.id}: no audio tracks`) return res.status(500).send('Invalid audiobook: no audio tracks') } @@ -110,6 +110,21 @@ class MiscController { res.sendStatus(200) } + // POST: api/encode-m4b/:id/cancel + async cancelM4bEncode(req, res) { + if (!req.user.isAdminOrUp) { + Logger.error('[MiscController] cancelM4bEncode: Non-admin user attempting to cancel m4b encode', req.user) + return res.sendStatus(403) + } + + const workerTask = this.abMergeManager.getPendingTaskByLibraryItemId(req.params.id) + if (!workerTask) return res.sendStatus(404) + + this.abMergeManager.cancelEncode(workerTask.task) + + res.sendStatus(200) + } + // GET: api/tasks getTasks(req, res) { res.json({ diff --git a/server/managers/AbMergeManager.js b/server/managers/AbMergeManager.js index 3fd7a9ba..1b3801c0 100644 --- a/server/managers/AbMergeManager.js +++ b/server/managers/AbMergeManager.js @@ -22,6 +22,14 @@ class AbMergeManager { this.pendingTasks = [] } + getPendingTaskByLibraryItemId(libraryItemId) { + return this.pendingTasks.find(t => t.task.data.libraryItemId === libraryItemId) + } + + cancelEncode(task) { + return this.removeTask(task, true) + } + async ensureDownloadDirPath() { // Creates download path if necessary and sets owner and permissions if (this.downloadDirPathExist) return @@ -223,6 +231,7 @@ class AbMergeManager { if (pendingDl.worker) { try { pendingDl.worker.postMessage('STOP') + return } catch (error) { Logger.error('[AbMergeManager] Error posting stop message to worker', error) } diff --git a/server/managers/TaskManager.js b/server/managers/TaskManager.js index acd573e6..d962d0c8 100644 --- a/server/managers/TaskManager.js +++ b/server/managers/TaskManager.js @@ -12,7 +12,7 @@ class TaskManager { taskFinished(task) { if (this.tasks.some(t => t.id === task.id)) { - this.tasks = this.tasks.filter(t => t !== task.id) + this.tasks = this.tasks.filter(t => t.id !== task.id) this.emitter('task_finished', task.toJSON()) } } diff --git a/server/routers/ApiRouter.js b/server/routers/ApiRouter.js index 1fac3fae..d5b46feb 100644 --- a/server/routers/ApiRouter.js +++ b/server/routers/ApiRouter.js @@ -224,7 +224,8 @@ class ApiRouter { // Misc Routes // this.router.post('/upload', MiscController.handleUpload.bind(this)) - this.router.get('/audiobook-merge/:id', MiscController.mergeAudiobook.bind(this)) + this.router.get('/encode-m4b/:id', MiscController.encodeM4b.bind(this)) + this.router.post('/encode-m4b/:id/cancel', MiscController.cancelM4bEncode.bind(this)) this.router.get('/tasks', MiscController.getTasks.bind(this)) this.router.patch('/settings', MiscController.updateServerSettings.bind(this)) // Root only this.router.post('/purgecache', MiscController.purgeCache.bind(this)) // Root only