aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-10-28 15:24:40 +0100
committerChocobozzz <chocobozzz@cpy.re>2020-11-09 15:33:04 +0100
commita056ca4813c82f490dcd31ac97a64d6bf76d3dcc (patch)
tree11a0638cb92eee94f404e294f54632212836a4a6 /server/middlewares/validators
parentd846d99c6c81028bb7bd3cb20abd433cbf396a22 (diff)
downloadPeerTube-a056ca4813c82f490dcd31ac97a64d6bf76d3dcc.tar.gz
PeerTube-a056ca4813c82f490dcd31ac97a64d6bf76d3dcc.tar.zst
PeerTube-a056ca4813c82f490dcd31ac97a64d6bf76d3dcc.zip
Add max lives limit
Diffstat (limited to 'server/middlewares/validators')
-rw-r--r--server/middlewares/validators/config.ts2
-rw-r--r--server/middlewares/validators/videos/video-live.ts35
2 files changed, 36 insertions, 1 deletions
diff --git a/server/middlewares/validators/config.ts b/server/middlewares/validators/config.ts
index 41a6ae4f9..d0071ccc1 100644
--- a/server/middlewares/validators/config.ts
+++ b/server/middlewares/validators/config.ts
@@ -65,6 +65,8 @@ const customConfigUpdateValidator = [
65 body('live.enabled').isBoolean().withMessage('Should have a valid live enabled boolean'), 65 body('live.enabled').isBoolean().withMessage('Should have a valid live enabled boolean'),
66 body('live.allowReplay').isBoolean().withMessage('Should have a valid live allow replay boolean'), 66 body('live.allowReplay').isBoolean().withMessage('Should have a valid live allow replay boolean'),
67 body('live.maxDuration').custom(isIntOrNull).withMessage('Should have a valid live max duration'), 67 body('live.maxDuration').custom(isIntOrNull).withMessage('Should have a valid live max duration'),
68 body('live.maxInstanceLives').custom(isIntOrNull).withMessage('Should have a valid max instance lives'),
69 body('live.maxUserLives').custom(isIntOrNull).withMessage('Should have a valid max user lives'),
68 body('live.transcoding.enabled').isBoolean().withMessage('Should have a valid live transcoding enabled boolean'), 70 body('live.transcoding.enabled').isBoolean().withMessage('Should have a valid live transcoding enabled boolean'),
69 body('live.transcoding.threads').isInt().withMessage('Should have a valid live transcoding threads'), 71 body('live.transcoding.threads').isInt().withMessage('Should have a valid live transcoding threads'),
70 body('live.transcoding.resolutions.240p').isBoolean().withMessage('Should have a valid transcoding 240p resolution enabled boolean'), 72 body('live.transcoding.resolutions.240p').isBoolean().withMessage('Should have a valid transcoding 240p resolution enabled boolean'),
diff --git a/server/middlewares/validators/videos/video-live.ts b/server/middlewares/validators/videos/video-live.ts
index ab57e67bf..69200cb60 100644
--- a/server/middlewares/validators/videos/video-live.ts
+++ b/server/middlewares/validators/videos/video-live.ts
@@ -2,7 +2,7 @@ import * as express from 'express'
2import { body, param } from 'express-validator' 2import { body, param } from 'express-validator'
3import { checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '@server/helpers/middlewares/videos' 3import { checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '@server/helpers/middlewares/videos'
4import { VideoLiveModel } from '@server/models/video/video-live' 4import { VideoLiveModel } from '@server/models/video/video-live'
5import { UserRight, VideoState } from '@shared/models' 5import { ServerErrorCode, UserRight, VideoState } from '@shared/models'
6import { isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc' 6import { isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
7import { isVideoNameValid } from '../../../helpers/custom-validators/videos' 7import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
8import { cleanUpReqFiles } from '../../../helpers/express-utils' 8import { cleanUpReqFiles } from '../../../helpers/express-utils'
@@ -10,6 +10,7 @@ import { logger } from '../../../helpers/logger'
10import { CONFIG } from '../../../initializers/config' 10import { CONFIG } from '../../../initializers/config'
11import { areValidationErrors } from '../utils' 11import { areValidationErrors } from '../utils'
12import { getCommonVideoEditAttributes } from './videos' 12import { getCommonVideoEditAttributes } from './videos'
13import { VideoModel } from '@server/models/video/video'
13 14
14const videoLiveGetValidator = [ 15const videoLiveGetValidator = [
15 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), 16 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
@@ -50,11 +51,15 @@ const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
50 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body }) 51 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
51 52
52 if (CONFIG.LIVE.ENABLED !== true) { 53 if (CONFIG.LIVE.ENABLED !== true) {
54 cleanUpReqFiles(req)
55
53 return res.status(403) 56 return res.status(403)
54 .json({ error: 'Live is not enabled on this instance' }) 57 .json({ error: 'Live is not enabled on this instance' })
55 } 58 }
56 59
57 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) { 60 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
61 cleanUpReqFiles(req)
62
58 return res.status(403) 63 return res.status(403)
59 .json({ error: 'Saving live replay is not allowed instance' }) 64 .json({ error: 'Saving live replay is not allowed instance' })
60 } 65 }
@@ -64,6 +69,34 @@ const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
64 const user = res.locals.oauth.token.User 69 const user = res.locals.oauth.token.User
65 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) 70 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
66 71
72 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
73 const totalInstanceLives = await VideoModel.countLocalLives()
74
75 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
76 cleanUpReqFiles(req)
77
78 return res.status(403)
79 .json({
80 code: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED,
81 error: 'Cannot create this live because the max instance lives limit is reached.'
82 })
83 }
84 }
85
86 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
87 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
88
89 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
90 cleanUpReqFiles(req)
91
92 return res.status(403)
93 .json({
94 code: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED,
95 error: 'Cannot create this live because the max user lives limit is reached.'
96 })
97 }
98 }
99
67 return next() 100 return next()
68 } 101 }
69]) 102])