]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { body, param } from 'express-validator' | |
3 | import { checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '@server/helpers/middlewares/videos' | |
4 | import { VideoLiveModel } from '@server/models/video/video-live' | |
5 | import { UserRight, VideoState } from '@shared/models' | |
6 | import { isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc' | |
7 | import { isVideoNameValid } from '../../../helpers/custom-validators/videos' | |
8 | import { cleanUpReqFiles } from '../../../helpers/express-utils' | |
9 | import { logger } from '../../../helpers/logger' | |
10 | import { CONFIG } from '../../../initializers/config' | |
11 | import { areValidationErrors } from '../utils' | |
12 | import { getCommonVideoEditAttributes } from './videos' | |
13 | ||
14 | const videoLiveGetValidator = [ | |
15 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | |
16 | ||
17 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
18 | logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.body }) | |
19 | ||
20 | if (areValidationErrors(req, res)) return | |
21 | if (!await doesVideoExist(req.params.videoId, res, 'all')) return | |
22 | ||
23 | // Check if the user who did the request is able to update the video | |
24 | const user = res.locals.oauth.token.User | |
25 | if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return | |
26 | ||
27 | const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id) | |
28 | if (!videoLive) return res.sendStatus(404) | |
29 | ||
30 | res.locals.videoLive = videoLive | |
31 | ||
32 | return next() | |
33 | } | |
34 | ] | |
35 | ||
36 | const videoLiveAddValidator = getCommonVideoEditAttributes().concat([ | |
37 | body('channelId') | |
38 | .customSanitizer(toIntOrNull) | |
39 | .custom(isIdValid).withMessage('Should have correct video channel id'), | |
40 | ||
41 | body('name') | |
42 | .custom(isVideoNameValid).withMessage('Should have a valid name'), | |
43 | ||
44 | body('saveReplay') | |
45 | .optional() | |
46 | .customSanitizer(toBooleanOrNull) | |
47 | .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'), | |
48 | ||
49 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
50 | logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body }) | |
51 | ||
52 | if (CONFIG.LIVE.ENABLED !== true) { | |
53 | return res.status(403) | |
54 | .json({ error: 'Live is not enabled on this instance' }) | |
55 | } | |
56 | ||
57 | if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) { | |
58 | return res.status(403) | |
59 | .json({ error: 'Saving live replay is not allowed instance' }) | |
60 | } | |
61 | ||
62 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) | |
63 | ||
64 | const user = res.locals.oauth.token.User | |
65 | if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) | |
66 | ||
67 | return next() | |
68 | } | |
69 | ]) | |
70 | ||
71 | const videoLiveUpdateValidator = [ | |
72 | body('saveReplay') | |
73 | .optional() | |
74 | .customSanitizer(toBooleanOrNull) | |
75 | .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'), | |
76 | ||
77 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
78 | logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body }) | |
79 | ||
80 | if (areValidationErrors(req, res)) return | |
81 | ||
82 | if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) { | |
83 | return res.status(403) | |
84 | .json({ error: 'Saving live replay is not allowed instance' }) | |
85 | } | |
86 | ||
87 | if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) { | |
88 | return res.status(400) | |
89 | .json({ error: 'Cannot update a live that has already started' }) | |
90 | } | |
91 | ||
92 | return next() | |
93 | } | |
94 | ] | |
95 | ||
96 | // --------------------------------------------------------------------------- | |
97 | ||
98 | export { | |
99 | videoLiveAddValidator, | |
100 | videoLiveUpdateValidator, | |
101 | videoLiveGetValidator | |
102 | } |