]>
Commit | Line | Data |
---|---|---|
c48e82b5 | 1 | import * as express from 'express' |
c8861d5d C |
2 | import { body, param } from 'express-validator' |
3 | import { exists, isBooleanValid, isIdOrUUIDValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' | |
c48e82b5 C |
4 | import { logger } from '../../helpers/logger' |
5 | import { areValidationErrors } from './utils' | |
c48e82b5 C |
6 | import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' |
7 | import { isHostValid } from '../../helpers/custom-validators/servers' | |
c48e82b5 | 8 | import { ServerModel } from '../../models/server/server' |
3e753302 | 9 | import { doesVideoExist } from '../../helpers/middlewares' |
c48e82b5 | 10 | |
09209296 | 11 | const videoFileRedundancyGetValidator = [ |
c48e82b5 C |
12 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), |
13 | param('resolution') | |
14 | .customSanitizer(toIntOrNull) | |
15 | .custom(exists).withMessage('Should have a valid resolution'), | |
16 | param('fps') | |
17 | .optional() | |
18 | .customSanitizer(toIntOrNull) | |
19 | .custom(exists).withMessage('Should have a valid fps'), | |
20 | ||
21 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
09209296 | 22 | logger.debug('Checking videoFileRedundancyGetValidator parameters', { parameters: req.params }) |
c48e82b5 C |
23 | |
24 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 25 | if (!await doesVideoExist(req.params.videoId, res)) return |
c48e82b5 | 26 | |
dae86118 | 27 | const video = res.locals.video |
c48e82b5 C |
28 | const videoFile = video.VideoFiles.find(f => { |
29 | return f.resolution === req.params.resolution && (!req.params.fps || f.fps === req.params.fps) | |
30 | }) | |
31 | ||
32 | if (!videoFile) return res.status(404).json({ error: 'Video file not found.' }) | |
33 | res.locals.videoFile = videoFile | |
34 | ||
46f8d69b | 35 | const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id) |
09209296 C |
36 | if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' }) |
37 | res.locals.videoRedundancy = videoRedundancy | |
38 | ||
39 | return next() | |
40 | } | |
41 | ] | |
42 | ||
43 | const videoPlaylistRedundancyGetValidator = [ | |
44 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), | |
45 | param('streamingPlaylistType').custom(exists).withMessage('Should have a valid streaming playlist type'), | |
46 | ||
47 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
48 | logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params }) | |
49 | ||
50 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 51 | if (!await doesVideoExist(req.params.videoId, res)) return |
09209296 | 52 | |
dae86118 | 53 | const video = res.locals.video |
09209296 C |
54 | const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p === req.params.streamingPlaylistType) |
55 | ||
56 | if (!videoStreamingPlaylist) return res.status(404).json({ error: 'Video playlist not found.' }) | |
57 | res.locals.videoStreamingPlaylist = videoStreamingPlaylist | |
58 | ||
59 | const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id) | |
60 | if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' }) | |
c48e82b5 C |
61 | res.locals.videoRedundancy = videoRedundancy |
62 | ||
63 | return next() | |
64 | } | |
65 | ] | |
66 | ||
67 | const updateServerRedundancyValidator = [ | |
68 | param('host').custom(isHostValid).withMessage('Should have a valid host'), | |
69 | body('redundancyAllowed') | |
c8861d5d | 70 | .customSanitizer(toBooleanOrNull) |
c48e82b5 C |
71 | .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'), |
72 | ||
73 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
74 | logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params }) | |
75 | ||
76 | if (areValidationErrors(req, res)) return | |
77 | ||
78 | const server = await ServerModel.loadByHost(req.params.host) | |
79 | ||
80 | if (!server) { | |
81 | return res | |
82 | .status(404) | |
83 | .json({ | |
84 | error: `Server ${req.params.host} not found.` | |
85 | }) | |
86 | .end() | |
87 | } | |
88 | ||
89 | res.locals.server = server | |
90 | return next() | |
91 | } | |
92 | ] | |
93 | ||
94 | // --------------------------------------------------------------------------- | |
95 | ||
96 | export { | |
09209296 C |
97 | videoFileRedundancyGetValidator, |
98 | videoPlaylistRedundancyGetValidator, | |
c48e82b5 C |
99 | updateServerRedundancyValidator |
100 | } |