1 import express from 'express'
2 import { body, param } from 'express-validator'
3 import { isIdOrUUIDValid } from '@server/helpers/custom-validators/misc'
6 isStudioTaskAddIntroOutroValid,
7 isStudioTaskAddWatermarkValid,
8 isValidStudioTasksArray
9 } from '@server/helpers/custom-validators/video-studio'
10 import { cleanUpReqFiles } from '@server/helpers/express-utils'
11 import { CONFIG } from '@server/initializers/config'
12 import { approximateIntroOutroAdditionalSize, getTaskFile } from '@server/lib/video-studio'
13 import { isAudioFile } from '@shared/extra-utils'
14 import { HttpStatusCode, UserRight, VideoState, VideoStudioCreateEdition, VideoStudioTask } from '@shared/models'
15 import { logger } from '../../../helpers/logger'
16 import { areValidationErrors, checkUserCanManageVideo, checkUserQuota, doesVideoExist } from '../shared'
18 const videoStudioAddEditionValidator = [
20 .custom(isIdOrUUIDValid).withMessage('Should have a valid video id/uuid/short uuid'),
23 .custom(isValidStudioTasksArray).withMessage('Should have a valid array of tasks'),
25 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 logger.debug('Checking videoStudioAddEditionValidator parameters.', { parameters: req.params, body: req.body, files: req.files })
28 if (CONFIG.VIDEO_STUDIO.ENABLED !== true) {
30 status: HttpStatusCode.BAD_REQUEST_400,
31 message: 'Video studio is disabled on this instance'
34 return cleanUpReqFiles(req)
37 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
39 const body: VideoStudioCreateEdition = req.body
40 const files = req.files as Express.Multer.File[]
42 for (let i = 0; i < body.tasks.length; i++) {
43 const task = body.tasks[i]
45 if (!checkTask(req, task, i)) {
47 status: HttpStatusCode.BAD_REQUEST_400,
48 message: `Task ${task.name} is invalid`
51 return cleanUpReqFiles(req)
54 if (task.name === 'add-intro' || task.name === 'add-outro') {
55 const filePath = getTaskFile(files, i).path
57 // Our concat filter needs a video stream
58 if (await isAudioFile(filePath)) {
60 status: HttpStatusCode.BAD_REQUEST_400,
61 message: `Task ${task.name} is invalid: file does not contain a video stream`
64 return cleanUpReqFiles(req)
69 if (!await doesVideoExist(req.params.videoId, res)) return cleanUpReqFiles(req)
71 const video = res.locals.videoAll
72 if (video.state === VideoState.TO_TRANSCODE || video.state === VideoState.TO_EDIT) {
74 status: HttpStatusCode.CONFLICT_409,
75 message: 'Cannot edit video that is already waiting for transcoding/edition'
78 return cleanUpReqFiles(req)
81 const user = res.locals.oauth.token.User
82 if (!checkUserCanManageVideo(user, video, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
84 // Try to make an approximation of bytes added by the intro/outro
85 const additionalBytes = await approximateIntroOutroAdditionalSize(video, body.tasks, i => getTaskFile(files, i).path)
86 if (await checkUserQuota(user, additionalBytes, res) === false) return cleanUpReqFiles(req)
92 // ---------------------------------------------------------------------------
95 videoStudioAddEditionValidator
98 // ---------------------------------------------------------------------------
100 const taskCheckers: {
101 [id in VideoStudioTask['name']]: (task: VideoStudioTask, indice?: number, files?: Express.Multer.File[]) => boolean
103 'cut': isStudioCutTaskValid,
104 'add-intro': isStudioTaskAddIntroOutroValid,
105 'add-outro': isStudioTaskAddIntroOutroValid,
106 'add-watermark': isStudioTaskAddWatermarkValid
109 function checkTask (req: express.Request, task: VideoStudioTask, indice?: number) {
110 const checker = taskCheckers[task.name]
111 if (!checker) return false
113 return checker(task, indice, req.files as Express.Multer.File[])