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