]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-studio.ts
Cleanup useless express validator messages
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-studio.ts
CommitLineData
c729caf6
C
1import express from 'express'
2import { body, param } from 'express-validator'
3import { isIdOrUUIDValid } from '@server/helpers/custom-validators/misc'
4import {
92e66e04
C
5 isStudioCutTaskValid,
6 isStudioTaskAddIntroOutroValid,
7 isStudioTaskAddWatermarkValid,
8 isValidStudioTasksArray
9} from '@server/helpers/custom-validators/video-studio'
c729caf6
C
10import { cleanUpReqFiles } from '@server/helpers/express-utils'
11import { CONFIG } from '@server/initializers/config'
92e66e04 12import { approximateIntroOutroAdditionalSize, getTaskFile } from '@server/lib/video-studio'
c729caf6 13import { isAudioFile } from '@shared/extra-utils'
92e66e04 14import { HttpStatusCode, UserRight, VideoState, VideoStudioCreateEdition, VideoStudioTask } from '@shared/models'
c729caf6
C
15import { logger } from '../../../helpers/logger'
16import { areValidationErrors, checkUserCanManageVideo, checkUserQuota, doesVideoExist } from '../shared'
17
92e66e04 18const videoStudioAddEditionValidator = [
396f6f01
C
19 param('videoId')
20 .custom(isIdOrUUIDValid).withMessage('Should have a valid video id/uuid/short uuid'),
c729caf6 21
396f6f01
C
22 body('tasks')
23 .custom(isValidStudioTasksArray).withMessage('Should have a valid array of tasks'),
c729caf6
C
24
25 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
92e66e04 26 logger.debug('Checking videoStudioAddEditionValidator parameters.', { parameters: req.params, body: req.body, files: req.files })
c729caf6 27
92e66e04 28 if (CONFIG.VIDEO_STUDIO.ENABLED !== true) {
c729caf6
C
29 res.fail({
30 status: HttpStatusCode.BAD_REQUEST_400,
92e66e04 31 message: 'Video studio is disabled on this instance'
c729caf6
C
32 })
33
34 return cleanUpReqFiles(req)
35 }
36
37 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
38
92e66e04 39 const body: VideoStudioCreateEdition = req.body
c729caf6
C
40 const files = req.files as Express.Multer.File[]
41
42 for (let i = 0; i < body.tasks.length; i++) {
43 const task = body.tasks[i]
44
45 if (!checkTask(req, task, i)) {
46 res.fail({
47 status: HttpStatusCode.BAD_REQUEST_400,
48 message: `Task ${task.name} is invalid`
49 })
50
51 return cleanUpReqFiles(req)
52 }
53
54 if (task.name === 'add-intro' || task.name === 'add-outro') {
55 const filePath = getTaskFile(files, i).path
56
57 // Our concat filter needs a video stream
58 if (await isAudioFile(filePath)) {
59 res.fail({
60 status: HttpStatusCode.BAD_REQUEST_400,
61 message: `Task ${task.name} is invalid: file does not contain a video stream`
62 })
63
64 return cleanUpReqFiles(req)
65 }
66 }
67 }
68
69 if (!await doesVideoExist(req.params.videoId, res)) return cleanUpReqFiles(req)
70
71 const video = res.locals.videoAll
72 if (video.state === VideoState.TO_TRANSCODE || video.state === VideoState.TO_EDIT) {
73 res.fail({
74 status: HttpStatusCode.CONFLICT_409,
75 message: 'Cannot edit video that is already waiting for transcoding/edition'
76 })
77
78 return cleanUpReqFiles(req)
79 }
80
81 const user = res.locals.oauth.token.User
82 if (!checkUserCanManageVideo(user, video, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
83
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)
87
88 return next()
89 }
90]
91
92// ---------------------------------------------------------------------------
93
94export {
92e66e04 95 videoStudioAddEditionValidator
c729caf6
C
96}
97
98// ---------------------------------------------------------------------------
99
100const taskCheckers: {
92e66e04 101 [id in VideoStudioTask['name']]: (task: VideoStudioTask, indice?: number, files?: Express.Multer.File[]) => boolean
c729caf6 102} = {
92e66e04
C
103 'cut': isStudioCutTaskValid,
104 'add-intro': isStudioTaskAddIntroOutroValid,
105 'add-outro': isStudioTaskAddIntroOutroValid,
106 'add-watermark': isStudioTaskAddWatermarkValid
c729caf6
C
107}
108
92e66e04 109function checkTask (req: express.Request, task: VideoStudioTask, indice?: number) {
c729caf6
C
110 const checker = taskCheckers[task.name]
111 if (!checker) return false
112
113 return checker(task, indice, req.files as Express.Multer.File[])
114}