diff options
author | Chocobozzz <me@florianbigard.com> | 2020-09-17 09:20:52 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-11-09 15:33:04 +0100 |
commit | c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e (patch) | |
tree | 79304b0152b0a38d33b26e65d4acdad0da4032a7 /server/middlewares | |
parent | 110d463fece85e87a26aca48a6048ae0017a27b3 (diff) | |
download | PeerTube-c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e.tar.gz PeerTube-c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e.tar.zst PeerTube-c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e.zip |
Live streaming implementation first step
Diffstat (limited to 'server/middlewares')
-rw-r--r-- | server/middlewares/validators/videos/video-live.ts | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/server/middlewares/validators/videos/video-live.ts b/server/middlewares/validators/videos/video-live.ts new file mode 100644 index 000000000..a4c364976 --- /dev/null +++ b/server/middlewares/validators/videos/video-live.ts | |||
@@ -0,0 +1,66 @@ | |||
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 { UserRight } from '@shared/models' | ||
5 | import { isIdOrUUIDValid, isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc' | ||
6 | import { isVideoNameValid } from '../../../helpers/custom-validators/videos' | ||
7 | import { cleanUpReqFiles } from '../../../helpers/express-utils' | ||
8 | import { logger } from '../../../helpers/logger' | ||
9 | import { CONFIG } from '../../../initializers/config' | ||
10 | import { areValidationErrors } from '../utils' | ||
11 | import { getCommonVideoEditAttributes } from './videos' | ||
12 | import { VideoLiveModel } from '@server/models/video/video-live' | ||
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 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
45 | logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body }) | ||
46 | |||
47 | if (CONFIG.LIVE.ENABLED !== true) { | ||
48 | return res.status(403) | ||
49 | .json({ error: 'Live is not enabled on this instance' }) | ||
50 | } | ||
51 | |||
52 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) | ||
53 | |||
54 | const user = res.locals.oauth.token.User | ||
55 | if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) | ||
56 | |||
57 | return next() | ||
58 | } | ||
59 | ]) | ||
60 | |||
61 | // --------------------------------------------------------------------------- | ||
62 | |||
63 | export { | ||
64 | videoLiveAddValidator, | ||
65 | videoLiveGetValidator | ||
66 | } | ||