]>
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 | |
453e83ea | 27 | const video = res.locals.videoAll |
d5d9b6d7 C |
28 | |
29 | const paramResolution = req.params.resolution as unknown as number // We casted to int above | |
30 | const paramFPS = req.params.fps as unknown as number // We casted to int above | |
31 | ||
c48e82b5 | 32 | const videoFile = video.VideoFiles.find(f => { |
d5d9b6d7 | 33 | return f.resolution === paramResolution && (!req.params.fps || paramFPS) |
c48e82b5 C |
34 | }) |
35 | ||
36 | if (!videoFile) return res.status(404).json({ error: 'Video file not found.' }) | |
37 | res.locals.videoFile = videoFile | |
38 | ||
46f8d69b | 39 | const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id) |
09209296 C |
40 | if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' }) |
41 | res.locals.videoRedundancy = videoRedundancy | |
42 | ||
43 | return next() | |
44 | } | |
45 | ] | |
46 | ||
47 | const videoPlaylistRedundancyGetValidator = [ | |
d5d9b6d7 C |
48 | param('videoId') |
49 | .custom(isIdOrUUIDValid) | |
50 | .not().isEmpty().withMessage('Should have a valid video id'), | |
51 | param('streamingPlaylistType') | |
52 | .customSanitizer(toIntOrNull) | |
53 | .custom(exists).withMessage('Should have a valid streaming playlist type'), | |
09209296 C |
54 | |
55 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
56 | logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params }) | |
57 | ||
58 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 59 | if (!await doesVideoExist(req.params.videoId, res)) return |
09209296 | 60 | |
453e83ea | 61 | const video = res.locals.videoAll |
d5d9b6d7 C |
62 | |
63 | const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above | |
64 | const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType) | |
09209296 C |
65 | |
66 | if (!videoStreamingPlaylist) return res.status(404).json({ error: 'Video playlist not found.' }) | |
67 | res.locals.videoStreamingPlaylist = videoStreamingPlaylist | |
68 | ||
69 | const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id) | |
70 | if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' }) | |
c48e82b5 C |
71 | res.locals.videoRedundancy = videoRedundancy |
72 | ||
73 | return next() | |
74 | } | |
75 | ] | |
76 | ||
77 | const updateServerRedundancyValidator = [ | |
78 | param('host').custom(isHostValid).withMessage('Should have a valid host'), | |
79 | body('redundancyAllowed') | |
c8861d5d | 80 | .customSanitizer(toBooleanOrNull) |
c48e82b5 C |
81 | .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'), |
82 | ||
83 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
84 | logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params }) | |
85 | ||
86 | if (areValidationErrors(req, res)) return | |
87 | ||
88 | const server = await ServerModel.loadByHost(req.params.host) | |
89 | ||
90 | if (!server) { | |
91 | return res | |
92 | .status(404) | |
93 | .json({ | |
94 | error: `Server ${req.params.host} not found.` | |
95 | }) | |
96 | .end() | |
97 | } | |
98 | ||
99 | res.locals.server = server | |
100 | return next() | |
101 | } | |
102 | ] | |
103 | ||
104 | // --------------------------------------------------------------------------- | |
105 | ||
106 | export { | |
09209296 C |
107 | videoFileRedundancyGetValidator, |
108 | videoPlaylistRedundancyGetValidator, | |
c48e82b5 C |
109 | updateServerRedundancyValidator |
110 | } |