aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/videos
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares/validators/videos')
-rw-r--r--server/middlewares/validators/videos/video-live.ts73
1 files changed, 65 insertions, 8 deletions
diff --git a/server/middlewares/validators/videos/video-live.ts b/server/middlewares/validators/videos/video-live.ts
index 6c7601e05..8e52c953f 100644
--- a/server/middlewares/validators/videos/video-live.ts
+++ b/server/middlewares/validators/videos/video-live.ts
@@ -1,12 +1,21 @@
1import express from 'express' 1import express from 'express'
2import { body } from 'express-validator' 2import { body } from 'express-validator'
3import { isLiveLatencyModeValid } from '@server/helpers/custom-validators/video-lives'
3import { CONSTRAINTS_FIELDS } from '@server/initializers/constants' 4import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'
4import { isLocalLiveVideoAccepted } from '@server/lib/moderation' 5import { isLocalLiveVideoAccepted } from '@server/lib/moderation'
5import { Hooks } from '@server/lib/plugins/hooks' 6import { Hooks } from '@server/lib/plugins/hooks'
6import { VideoModel } from '@server/models/video/video' 7import { VideoModel } from '@server/models/video/video'
7import { VideoLiveModel } from '@server/models/video/video-live' 8import { VideoLiveModel } from '@server/models/video/video-live'
8import { HttpStatusCode, ServerErrorCode, UserRight, VideoState } from '@shared/models' 9import {
9import { isBooleanValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc' 10 HttpStatusCode,
11 LiveVideoCreate,
12 LiveVideoLatencyMode,
13 LiveVideoUpdate,
14 ServerErrorCode,
15 UserRight,
16 VideoState
17} from '@shared/models'
18import { exists, isBooleanValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
10import { isVideoNameValid } from '../../../helpers/custom-validators/videos' 19import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
11import { cleanUpReqFiles } from '../../../helpers/express-utils' 20import { cleanUpReqFiles } from '../../../helpers/express-utils'
12import { logger } from '../../../helpers/logger' 21import { logger } from '../../../helpers/logger'
@@ -67,6 +76,12 @@ const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
67 .customSanitizer(toBooleanOrNull) 76 .customSanitizer(toBooleanOrNull)
68 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'), 77 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
69 78
79 body('latencyMode')
80 .optional()
81 .customSanitizer(toIntOrNull)
82 .custom(isLiveLatencyModeValid)
83 .withMessage('Should have a valid latency mode attribute'),
84
70 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 85 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
71 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body }) 86 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
72 87
@@ -82,7 +97,9 @@ const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
82 }) 97 })
83 } 98 }
84 99
85 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) { 100 const body: LiveVideoCreate = req.body
101
102 if (hasValidSaveReplay(body) !== true) {
86 cleanUpReqFiles(req) 103 cleanUpReqFiles(req)
87 104
88 return res.fail({ 105 return res.fail({
@@ -92,14 +109,23 @@ const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
92 }) 109 })
93 } 110 }
94 111
95 if (req.body.permanentLive && req.body.saveReplay) { 112 if (hasValidLatencyMode(body) !== true) {
113 cleanUpReqFiles(req)
114
115 return res.fail({
116 status: HttpStatusCode.FORBIDDEN_403,
117 message: 'Custom latency mode is not allowed by this instance'
118 })
119 }
120
121 if (body.permanentLive && body.saveReplay) {
96 cleanUpReqFiles(req) 122 cleanUpReqFiles(req)
97 123
98 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' }) 124 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
99 } 125 }
100 126
101 const user = res.locals.oauth.token.User 127 const user = res.locals.oauth.token.User
102 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) 128 if (!await doesVideoChannelOfAccountExist(body.channelId, user, res)) return cleanUpReqFiles(req)
103 129
104 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) { 130 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
105 const totalInstanceLives = await VideoModel.countLocalLives() 131 const totalInstanceLives = await VideoModel.countLocalLives()
@@ -141,19 +167,34 @@ const videoLiveUpdateValidator = [
141 .customSanitizer(toBooleanOrNull) 167 .customSanitizer(toBooleanOrNull)
142 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'), 168 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
143 169
170 body('latencyMode')
171 .optional()
172 .customSanitizer(toIntOrNull)
173 .custom(isLiveLatencyModeValid)
174 .withMessage('Should have a valid latency mode attribute'),
175
144 (req: express.Request, res: express.Response, next: express.NextFunction) => { 176 (req: express.Request, res: express.Response, next: express.NextFunction) => {
145 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body }) 177 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
146 178
147 if (areValidationErrors(req, res)) return 179 if (areValidationErrors(req, res)) return
148 180
149 if (req.body.permanentLive && req.body.saveReplay) { 181 const body: LiveVideoUpdate = req.body
182
183 if (body.permanentLive && body.saveReplay) {
150 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' }) 184 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
151 } 185 }
152 186
153 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) { 187 if (hasValidSaveReplay(body) !== true) {
154 return res.fail({ 188 return res.fail({
155 status: HttpStatusCode.FORBIDDEN_403, 189 status: HttpStatusCode.FORBIDDEN_403,
156 message: 'Saving live replay is not allowed instance' 190 message: 'Saving live replay is not allowed by this instance'
191 })
192 }
193
194 if (hasValidLatencyMode(body) !== true) {
195 return res.fail({
196 status: HttpStatusCode.FORBIDDEN_403,
197 message: 'Custom latency mode is not allowed by this instance'
157 }) 198 })
158 } 199 }
159 200
@@ -203,3 +244,19 @@ async function isLiveVideoAccepted (req: express.Request, res: express.Response)
203 244
204 return true 245 return true
205} 246}
247
248function hasValidSaveReplay (body: LiveVideoUpdate | LiveVideoCreate) {
249 if (CONFIG.LIVE.ALLOW_REPLAY !== true && body.saveReplay === true) return false
250
251 return true
252}
253
254function hasValidLatencyMode (body: LiveVideoUpdate | LiveVideoCreate) {
255 if (
256 CONFIG.LIVE.LATENCY_SETTING.ENABLED !== true &&
257 exists(body.latencyMode) &&
258 body.latencyMode !== LiveVideoLatencyMode.DEFAULT
259 ) return false
260
261 return true
262}