From 419b520ca4434d17f3505013174e195c3a316716 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 19 Jan 2022 14:23:00 +0100 Subject: Add ability to cancel & delete video imports --- server/controllers/api/jobs.ts | 26 +++++++++++++++++- server/controllers/api/videos/import.ts | 48 +++++++++++++++++++++++++++++++-- 2 files changed, 71 insertions(+), 3 deletions(-) (limited to 'server/controllers/api') diff --git a/server/controllers/api/jobs.ts b/server/controllers/api/jobs.ts index eebd195b0..c61b7362f 100644 --- a/server/controllers/api/jobs.ts +++ b/server/controllers/api/jobs.ts @@ -1,5 +1,5 @@ import express from 'express' -import { Job, JobState, JobType, ResultList, UserRight } from '@shared/models' +import { HttpStatusCode, Job, JobState, JobType, ResultList, UserRight } from '@shared/models' import { isArray } from '../../helpers/custom-validators/misc' import { JobQueue } from '../../lib/job-queue' import { @@ -16,6 +16,18 @@ import { listJobsValidator } from '../../middlewares/validators/jobs' const jobsRouter = express.Router() +jobsRouter.post('/pause', + authenticate, + ensureUserHasRight(UserRight.MANAGE_JOBS), + asyncMiddleware(pauseJobQueue) +) + +jobsRouter.post('/resume', + authenticate, + ensureUserHasRight(UserRight.MANAGE_JOBS), + asyncMiddleware(resumeJobQueue) +) + jobsRouter.get('/:state?', openapiOperationDoc({ operationId: 'getJobs' }), authenticate, @@ -36,6 +48,18 @@ export { // --------------------------------------------------------------------------- +async function pauseJobQueue (req: express.Request, res: express.Response) { + await JobQueue.Instance.pause() + + return res.sendStatus(HttpStatusCode.NO_CONTENT_204) +} + +async function resumeJobQueue (req: express.Request, res: express.Response) { + await JobQueue.Instance.resume() + + return res.sendStatus(HttpStatusCode.NO_CONTENT_204) +} + async function listJobs (req: express.Request, res: express.Response) { const state = req.params.state as JobState const asc = req.query.sort === 'createdAt' diff --git a/server/controllers/api/videos/import.ts b/server/controllers/api/videos/import.ts index 08d69827b..8cbfd3286 100644 --- a/server/controllers/api/videos/import.ts +++ b/server/controllers/api/videos/import.ts @@ -19,7 +19,15 @@ import { MVideoWithBlacklistLight } from '@server/types/models' import { MVideoImportFormattable } from '@server/types/models/video/video-import' -import { ServerErrorCode, ThumbnailType, VideoImportCreate, VideoImportState, VideoPrivacy, VideoState } from '@shared/models' +import { + HttpStatusCode, + ServerErrorCode, + ThumbnailType, + VideoImportCreate, + VideoImportState, + VideoPrivacy, + VideoState +} from '@shared/models' import { auditLoggerFactory, getAuditIdFromRes, VideoImportAuditView } from '../../../helpers/audit-logger' import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils' import { isArray } from '../../../helpers/custom-validators/misc' @@ -34,7 +42,14 @@ import { getLocalVideoActivityPubUrl } from '../../../lib/activitypub/url' import { JobQueue } from '../../../lib/job-queue/job-queue' import { updateVideoMiniatureFromExisting, updateVideoMiniatureFromUrl } from '../../../lib/thumbnail' import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist' -import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoImportAddValidator } from '../../../middlewares' +import { + asyncMiddleware, + asyncRetryTransactionMiddleware, + authenticate, + videoImportAddValidator, + videoImportCancelValidator, + videoImportDeleteValidator +} from '../../../middlewares' import { VideoModel } from '../../../models/video/video' import { VideoCaptionModel } from '../../../models/video/video-caption' import { VideoImportModel } from '../../../models/video/video-import' @@ -59,6 +74,18 @@ videoImportsRouter.post('/imports', asyncRetryTransactionMiddleware(addVideoImport) ) +videoImportsRouter.post('/imports/:id/cancel', + authenticate, + asyncMiddleware(videoImportCancelValidator), + asyncRetryTransactionMiddleware(cancelVideoImport) +) + +videoImportsRouter.delete('/imports/:id', + authenticate, + asyncMiddleware(videoImportDeleteValidator), + asyncRetryTransactionMiddleware(deleteVideoImport) +) + // --------------------------------------------------------------------------- export { @@ -67,6 +94,23 @@ export { // --------------------------------------------------------------------------- +async function deleteVideoImport (req: express.Request, res: express.Response) { + const videoImport = res.locals.videoImport + + await videoImport.destroy() + + return res.sendStatus(HttpStatusCode.NO_CONTENT_204) +} + +async function cancelVideoImport (req: express.Request, res: express.Response) { + const videoImport = res.locals.videoImport + + videoImport.state = VideoImportState.CANCELLED + await videoImport.save() + + return res.sendStatus(HttpStatusCode.NO_CONTENT_204) +} + function addVideoImport (req: express.Request, res: express.Response) { if (req.body.targetUrl) return addYoutubeDLImport(req, res) -- cgit v1.2.3