]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-studio.ts
Cleanup useless express validator messages
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-studio.ts
1 import express from 'express'
2 import { body, param } from 'express-validator'
3 import { isIdOrUUIDValid } from '@server/helpers/custom-validators/misc'
4 import {
5 isStudioCutTaskValid,
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'
17
18 const videoStudioAddEditionValidator = [
19 param('videoId')
20 .custom(isIdOrUUIDValid).withMessage('Should have a valid video id/uuid/short uuid'),
21
22 body('tasks')
23 .custom(isValidStudioTasksArray).withMessage('Should have a valid array of tasks'),
24
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 })
27
28 if (CONFIG.VIDEO_STUDIO.ENABLED !== true) {
29 res.fail({
30 status: HttpStatusCode.BAD_REQUEST_400,
31 message: 'Video studio is disabled on this instance'
32 })
33
34 return cleanUpReqFiles(req)
35 }
36
37 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
38
39 const body: VideoStudioCreateEdition = req.body
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
94 export {
95 videoStudioAddEditionValidator
96 }
97
98 // ---------------------------------------------------------------------------
99
100 const taskCheckers: {
101 [id in VideoStudioTask['name']]: (task: VideoStudioTask, indice?: number, files?: Express.Multer.File[]) => boolean
102 } = {
103 'cut': isStudioCutTaskValid,
104 'add-intro': isStudioTaskAddIntroOutroValid,
105 'add-outro': isStudioTaskAddIntroOutroValid,
106 'add-watermark': isStudioTaskAddWatermarkValid
107 }
108
109 function checkTask (req: express.Request, task: VideoStudioTask, indice?: number) {
110 const checker = taskCheckers[task.name]
111 if (!checker) return false
112
113 return checker(task, indice, req.files as Express.Multer.File[])
114 }