Don't transcribe/encode videos with invalid state

pull/6416/merge
Chocobozzz 2024-07-03 09:09:20 +02:00
parent 2d26eff129
commit f5104fb234
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
4 changed files with 65 additions and 33 deletions

View File

@ -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<VideoStateType>([
VideoState.TO_IMPORT,
VideoState.TO_EDIT,
VideoState.TO_MOVE_TO_EXTERNAL_STORAGE,
VideoState.TO_MOVE_TO_FILE_SYSTEM
])
}
// ---------------------------------------------------------------------------

View File

@ -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<VideoStateType>([
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
}

View File

@ -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

View File

@ -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({