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