aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/redundancy.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-09-11 16:27:07 +0200
committerChocobozzz <me@florianbigard.com>2018-09-13 14:05:49 +0200
commitc48e82b5e0478434de30626d14594a97f2402e7c (patch)
treea78e5272bd0fe4f5b41831e571e02d05f1515b82 /server/middlewares/validators/redundancy.ts
parenta651038487faa838bda3ce04695b08bc65baff70 (diff)
downloadPeerTube-c48e82b5e0478434de30626d14594a97f2402e7c.tar.gz
PeerTube-c48e82b5e0478434de30626d14594a97f2402e7c.tar.zst
PeerTube-c48e82b5e0478434de30626d14594a97f2402e7c.zip
Basic video redundancy implementation
Diffstat (limited to 'server/middlewares/validators/redundancy.ts')
-rw-r--r--server/middlewares/validators/redundancy.ts80
1 files changed, 80 insertions, 0 deletions
diff --git a/server/middlewares/validators/redundancy.ts b/server/middlewares/validators/redundancy.ts
new file mode 100644
index 000000000..d91b47574
--- /dev/null
+++ b/server/middlewares/validators/redundancy.ts
@@ -0,0 +1,80 @@
1import * as express from 'express'
2import 'express-validator'
3import { param, body } from 'express-validator/check'
4import { exists, isBooleanValid, isIdOrUUIDValid, toIntOrNull } from '../../helpers/custom-validators/misc'
5import { isVideoExist } from '../../helpers/custom-validators/videos'
6import { logger } from '../../helpers/logger'
7import { areValidationErrors } from './utils'
8import { VideoModel } from '../../models/video/video'
9import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
10import { isHostValid } from '../../helpers/custom-validators/servers'
11import { getServerActor } from '../../helpers/utils'
12import { ActorFollowModel } from '../../models/activitypub/actor-follow'
13import { SERVER_ACTOR_NAME } from '../../initializers'
14import { ServerModel } from '../../models/server/server'
15
16const videoRedundancyGetValidator = [
17 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
18 param('resolution')
19 .customSanitizer(toIntOrNull)
20 .custom(exists).withMessage('Should have a valid resolution'),
21 param('fps')
22 .optional()
23 .customSanitizer(toIntOrNull)
24 .custom(exists).withMessage('Should have a valid fps'),
25
26 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking videoRedundancyGetValidator parameters', { parameters: req.params })
28
29 if (areValidationErrors(req, res)) return
30 if (!await isVideoExist(req.params.videoId, res)) return
31
32 const video: VideoModel = res.locals.video
33 const videoFile = video.VideoFiles.find(f => {
34 return f.resolution === req.params.resolution && (!req.params.fps || f.fps === req.params.fps)
35 })
36
37 if (!videoFile) return res.status(404).json({ error: 'Video file not found.' })
38 res.locals.videoFile = videoFile
39
40 const videoRedundancy = await VideoRedundancyModel.loadByFileId(videoFile.id)
41 if (!videoRedundancy)return res.status(404).json({ error: 'Video redundancy not found.' })
42 res.locals.videoRedundancy = videoRedundancy
43
44 return next()
45 }
46]
47
48const updateServerRedundancyValidator = [
49 param('host').custom(isHostValid).withMessage('Should have a valid host'),
50 body('redundancyAllowed')
51 .toBoolean()
52 .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'),
53
54 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
55 logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params })
56
57 if (areValidationErrors(req, res)) return
58
59 const server = await ServerModel.loadByHost(req.params.host)
60
61 if (!server) {
62 return res
63 .status(404)
64 .json({
65 error: `Server ${req.params.host} not found.`
66 })
67 .end()
68 }
69
70 res.locals.server = server
71 return next()
72 }
73]
74
75// ---------------------------------------------------------------------------
76
77export {
78 videoRedundancyGetValidator,
79 updateServerRedundancyValidator
80}