From f5104fb234badc16a0d659724337da3dcfbc0441 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 3 Jul 2024 09:09:20 +0200 Subject: [PATCH] Don't transcribe/encode videos with invalid state --- .../shared/shared-main/video/video.model.ts | 24 +++++++++++- .../videos/shared/video-validators.ts | 38 ++++++++++++++++++- .../validators/videos/video-captions.ts | 15 +------- .../validators/videos/video-transcoding.ts | 21 +++------- 4 files changed, 65 insertions(+), 33 deletions(-) diff --git a/client/src/app/shared/shared-main/video/video.model.ts b/client/src/app/shared/shared-main/video/video.model.ts index d46e6989e..d408b093d 100644 --- a/client/src/app/shared/shared-main/video/video.model.ts +++ b/client/src/app/shared/shared-main/video/video.model.ts @@ -273,12 +273,32 @@ export class Video implements VideoServerModel { this.hasWebVideos() } + // --------------------------------------------------------------------------- + canRunTranscoding (user: AuthUser) { - return this.isLocal && !this.isLive && user?.hasRight(UserRight.RUN_VIDEO_TRANSCODING) + return this.isLocal && + !this.isLive && + user?.hasRight(UserRight.RUN_VIDEO_TRANSCODING) && + this.state?.id && + !this.transcodingAndTranscriptionIncompatibleStates().has(this.state.id) } canGenerateTranscription (user: AuthUser, transcriptionEnabled: boolean) { - return transcriptionEnabled && this.isLocal && !this.isLive && user.hasRight(UserRight.UPDATE_ANY_VIDEO) + return transcriptionEnabled && + this.isLocal && + !this.isLive && + user.hasRight(UserRight.UPDATE_ANY_VIDEO) && + this.state?.id && + !this.transcodingAndTranscriptionIncompatibleStates().has(this.state.id) + } + + private transcodingAndTranscriptionIncompatibleStates () { + return new Set([ + VideoState.TO_IMPORT, + VideoState.TO_EDIT, + VideoState.TO_MOVE_TO_EXTERNAL_STORAGE, + VideoState.TO_MOVE_TO_FILE_SYSTEM + ]) } // --------------------------------------------------------------------------- diff --git a/server/core/middlewares/validators/videos/shared/video-validators.ts b/server/core/middlewares/validators/videos/shared/video-validators.ts index 470cd842f..b80765d42 100644 --- a/server/core/middlewares/validators/videos/shared/video-validators.ts +++ b/server/core/middlewares/validators/videos/shared/video-validators.ts @@ -1,11 +1,11 @@ -import express from 'express' import { HttpStatusCode, ServerErrorCode, ServerFilterHookName, VideoState, VideoStateType } from '@peertube/peertube-models' import { isVideoFileMimeTypeValid, isVideoFileSizeValid } from '@server/helpers/custom-validators/videos.js' import { logger } from '@server/helpers/logger.js' -import { CONSTRAINTS_FIELDS } from '@server/initializers/constants.js' +import { CONSTRAINTS_FIELDS, VIDEO_STATES } from '@server/initializers/constants.js' import { isLocalVideoFileAccepted } from '@server/lib/moderation.js' import { Hooks } from '@server/lib/plugins/hooks.js' import { MUserAccountId, MVideo } from '@server/types/models/index.js' +import express from 'express' import { checkUserQuota } from '../../shared/index.js' export async function commonVideoFileChecks (options: { @@ -103,3 +103,37 @@ export function checkVideoFileCanBeEdited (video: MVideo, res: express.Response) return true } + +export function checkVideoCanBeTranscribedOrTranscripted (video: MVideo, res: express.Response) { + if (video.remote) { + res.fail({ + status: HttpStatusCode.BAD_REQUEST_400, + message: 'Cannot run this task on a remote video' + }) + return false + } + + if (video.isLive) { + res.fail({ + status: HttpStatusCode.BAD_REQUEST_400, + message: 'Cannot run this task on a live' + }) + return false + } + + const incompatibleStates = new Set([ + VideoState.TO_IMPORT, + VideoState.TO_EDIT, + VideoState.TO_MOVE_TO_EXTERNAL_STORAGE, + VideoState.TO_MOVE_TO_FILE_SYSTEM + ]) + if (incompatibleStates.has(video.state)) { + res.fail({ + status: HttpStatusCode.BAD_REQUEST_400, + message: `Cannot run this task on a video with "${VIDEO_STATES[video.state]}" state` + }) + return false + } + + return true +} diff --git a/server/core/middlewares/validators/videos/video-captions.ts b/server/core/middlewares/validators/videos/video-captions.ts index 062b03fd6..4dc874a6f 100644 --- a/server/core/middlewares/validators/videos/video-captions.ts +++ b/server/core/middlewares/validators/videos/video-captions.ts @@ -17,6 +17,7 @@ import { isValidVideoIdParam, isValidVideoPasswordHeader } from '../shared/index.js' +import { checkVideoCanBeTranscribedOrTranscripted } from './shared/video-validators.js' export const addVideoCaptionValidator = [ isValidVideoIdParam('videoId'), @@ -67,19 +68,7 @@ export const generateVideoCaptionValidator = [ const video = res.locals.videoAll - if (video.remote) { - return res.fail({ - status: HttpStatusCode.BAD_REQUEST_400, - message: 'Cannot run transcription job on a remote video' - }) - } - - if (video.isLive) { - return res.fail({ - status: HttpStatusCode.BAD_REQUEST_400, - message: 'Cannot run transcription job on a live' - }) - } + if (!checkVideoCanBeTranscribedOrTranscripted(video, res)) return // Check if the user who did the request is able to update the video const user = res.locals.oauth.token.User diff --git a/server/core/middlewares/validators/videos/video-transcoding.ts b/server/core/middlewares/validators/videos/video-transcoding.ts index 19274a48c..8bc10d059 100644 --- a/server/core/middlewares/validators/videos/video-transcoding.ts +++ b/server/core/middlewares/validators/videos/video-transcoding.ts @@ -1,11 +1,12 @@ -import express from 'express' -import { body } from 'express-validator' +import { HttpStatusCode, ServerErrorCode, VideoTranscodingCreate } from '@peertube/peertube-models' import { isBooleanValid, toBooleanOrNull } from '@server/helpers/custom-validators/misc.js' import { isValidCreateTranscodingType } from '@server/helpers/custom-validators/video-transcoding.js' import { CONFIG } from '@server/initializers/config.js' import { VideoJobInfoModel } from '@server/models/video/video-job-info.js' -import { HttpStatusCode, ServerErrorCode, VideoTranscodingCreate } from '@peertube/peertube-models' +import express from 'express' +import { body } from 'express-validator' import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared/index.js' +import { checkVideoCanBeTranscribedOrTranscripted } from './shared/video-validators.js' const createTranscodingValidator = [ isValidVideoIdParam('videoId'), @@ -24,19 +25,7 @@ const createTranscodingValidator = [ const video = res.locals.videoAll - if (video.remote) { - return res.fail({ - status: HttpStatusCode.BAD_REQUEST_400, - message: 'Cannot run transcoding job on a remote video' - }) - } - - if (video.isLive) { - return res.fail({ - status: HttpStatusCode.BAD_REQUEST_400, - message: 'Cannot run transcoding job on a live' - }) - } + if (!checkVideoCanBeTranscribedOrTranscripted(video, res)) return if (CONFIG.TRANSCODING.ENABLED !== true) { return res.fail({