]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-live.ts
add video upload types, add doc middleware to more routes
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-live.ts
1 import * as express from 'express'
2 import { body, param } from 'express-validator'
3 import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'
4 import { isLocalLiveVideoAccepted } from '@server/lib/moderation'
5 import { Hooks } from '@server/lib/plugins/hooks'
6 import { VideoModel } from '@server/models/video/video'
7 import { VideoLiveModel } from '@server/models/video/video-live'
8 import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
9 import { ServerErrorCode, UserRight, VideoState } from '@shared/models'
10 import { isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
11 import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
12 import { cleanUpReqFiles } from '../../../helpers/express-utils'
13 import { logger } from '../../../helpers/logger'
14 import { CONFIG } from '../../../initializers/config'
15 import { areValidationErrors, checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '../shared'
16 import { getCommonVideoEditAttributes } from './videos'
17
18 const videoLiveGetValidator = [
19 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
20
21 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
22 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username })
23
24 if (areValidationErrors(req, res)) return
25 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
26
27 // Check if the user who did the request is able to get the live info
28 const user = res.locals.oauth.token.User
29 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return
30
31 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
32 if (!videoLive) {
33 return res.fail({
34 status: HttpStatusCode.NOT_FOUND_404,
35 message: 'Live video not found'
36 })
37 }
38
39 res.locals.videoLive = videoLive
40
41 return next()
42 }
43 ]
44
45 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
46 body('channelId')
47 .customSanitizer(toIntOrNull)
48 .custom(isIdValid).withMessage('Should have correct video channel id'),
49
50 body('name')
51 .custom(isVideoNameValid).withMessage(
52 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
53 ),
54
55 body('saveReplay')
56 .optional()
57 .customSanitizer(toBooleanOrNull)
58 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
59
60 body('permanentLive')
61 .optional()
62 .customSanitizer(toBooleanOrNull)
63 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
64
65 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
66 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
67
68 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
69
70 if (CONFIG.LIVE.ENABLED !== true) {
71 cleanUpReqFiles(req)
72
73 return res.fail({
74 status: HttpStatusCode.FORBIDDEN_403,
75 message: 'Live is not enabled on this instance',
76 type: ServerErrorCode.LIVE_NOT_ENABLED
77 })
78 }
79
80 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
81 cleanUpReqFiles(req)
82
83 return res.fail({
84 status: HttpStatusCode.FORBIDDEN_403,
85 message: 'Saving live replay is not enabled on this instance',
86 type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY
87 })
88 }
89
90 if (req.body.permanentLive && req.body.saveReplay) {
91 cleanUpReqFiles(req)
92
93 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
94 }
95
96 const user = res.locals.oauth.token.User
97 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
98
99 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
100 const totalInstanceLives = await VideoModel.countLocalLives()
101
102 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
103 cleanUpReqFiles(req)
104
105 return res.fail({
106 status: HttpStatusCode.FORBIDDEN_403,
107 message: 'Cannot create this live because the max instance lives limit is reached.',
108 type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED
109 })
110 }
111 }
112
113 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
114 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
115
116 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
117 cleanUpReqFiles(req)
118
119 return res.fail({
120 status: HttpStatusCode.FORBIDDEN_403,
121 message: 'Cannot create this live because the max user lives limit is reached.',
122 type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED
123 })
124 }
125 }
126
127 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
128
129 return next()
130 }
131 ])
132
133 const videoLiveUpdateValidator = [
134 body('saveReplay')
135 .optional()
136 .customSanitizer(toBooleanOrNull)
137 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
138
139 (req: express.Request, res: express.Response, next: express.NextFunction) => {
140 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
141
142 if (areValidationErrors(req, res)) return
143
144 if (req.body.permanentLive && req.body.saveReplay) {
145 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
146 }
147
148 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
149 return res.fail({
150 status: HttpStatusCode.FORBIDDEN_403,
151 message: 'Saving live replay is not allowed instance'
152 })
153 }
154
155 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
156 return res.fail({ message: 'Cannot update a live that has already started' })
157 }
158
159 // Check the user can manage the live
160 const user = res.locals.oauth.token.User
161 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
162
163 return next()
164 }
165 ]
166
167 // ---------------------------------------------------------------------------
168
169 export {
170 videoLiveAddValidator,
171 videoLiveUpdateValidator,
172 videoLiveGetValidator
173 }
174
175 // ---------------------------------------------------------------------------
176
177 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
178 // Check we accept this video
179 const acceptParameters = {
180 liveVideoBody: req.body,
181 user: res.locals.oauth.token.User
182 }
183 const acceptedResult = await Hooks.wrapFun(
184 isLocalLiveVideoAccepted,
185 acceptParameters,
186 'filter:api.live-video.create.accept.result'
187 )
188
189 if (!acceptedResult || acceptedResult.accepted !== true) {
190 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
191
192 res.fail({
193 status: HttpStatusCode.FORBIDDEN_403,
194 message: acceptedResult.errorMessage || 'Refused local live video'
195 })
196 return false
197 }
198
199 return true
200 }