]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-studio.ts
Add ability to list comments on local videos
[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').custom(isIdOrUUIDValid).withMessage('Should have a valid video id/uuid'),
20
21 body('tasks').custom(isValidStudioTasksArray).withMessage('Should have a valid array of tasks'),
22
23 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
24 logger.debug('Checking videoStudioAddEditionValidator parameters.', { parameters: req.params, body: req.body, files: req.files })
25
26 if (CONFIG.VIDEO_STUDIO.ENABLED !== true) {
27 res.fail({
28 status: HttpStatusCode.BAD_REQUEST_400,
29 message: 'Video studio is disabled on this instance'
30 })
31
32 return cleanUpReqFiles(req)
33 }
34
35 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
36
37 const body: VideoStudioCreateEdition = req.body
38 const files = req.files as Express.Multer.File[]
39
40 for (let i = 0; i < body.tasks.length; i++) {
41 const task = body.tasks[i]
42
43 if (!checkTask(req, task, i)) {
44 res.fail({
45 status: HttpStatusCode.BAD_REQUEST_400,
46 message: `Task ${task.name} is invalid`
47 })
48
49 return cleanUpReqFiles(req)
50 }
51
52 if (task.name === 'add-intro' || task.name === 'add-outro') {
53 const filePath = getTaskFile(files, i).path
54
55 // Our concat filter needs a video stream
56 if (await isAudioFile(filePath)) {
57 res.fail({
58 status: HttpStatusCode.BAD_REQUEST_400,
59 message: `Task ${task.name} is invalid: file does not contain a video stream`
60 })
61
62 return cleanUpReqFiles(req)
63 }
64 }
65 }
66
67 if (!await doesVideoExist(req.params.videoId, res)) return cleanUpReqFiles(req)
68
69 const video = res.locals.videoAll
70 if (video.state === VideoState.TO_TRANSCODE || video.state === VideoState.TO_EDIT) {
71 res.fail({
72 status: HttpStatusCode.CONFLICT_409,
73 message: 'Cannot edit video that is already waiting for transcoding/edition'
74 })
75
76 return cleanUpReqFiles(req)
77 }
78
79 const user = res.locals.oauth.token.User
80 if (!checkUserCanManageVideo(user, video, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
81
82 // Try to make an approximation of bytes added by the intro/outro
83 const additionalBytes = await approximateIntroOutroAdditionalSize(video, body.tasks, i => getTaskFile(files, i).path)
84 if (await checkUserQuota(user, additionalBytes, res) === false) return cleanUpReqFiles(req)
85
86 return next()
87 }
88 ]
89
90 // ---------------------------------------------------------------------------
91
92 export {
93 videoStudioAddEditionValidator
94 }
95
96 // ---------------------------------------------------------------------------
97
98 const taskCheckers: {
99 [id in VideoStudioTask['name']]: (task: VideoStudioTask, indice?: number, files?: Express.Multer.File[]) => boolean
100 } = {
101 'cut': isStudioCutTaskValid,
102 'add-intro': isStudioTaskAddIntroOutroValid,
103 'add-outro': isStudioTaskAddIntroOutroValid,
104 'add-watermark': isStudioTaskAddWatermarkValid
105 }
106
107 function checkTask (req: express.Request, task: VideoStudioTask, indice?: number) {
108 const checker = taskCheckers[task.name]
109 if (!checker) return false
110
111 return checker(task, indice, req.files as Express.Multer.File[])
112 }