]>
Commit | Line | Data |
---|---|---|
c48e82b5 | 1 | import * as express from 'express' |
b764380a C |
2 | import { body, param, query } from 'express-validator' |
3 | import { exists, isBooleanValid, isIdOrUUIDValid, isIdValid, 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' |
b764380a | 10 | import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies' |
c48e82b5 | 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 | |
453e83ea | 28 | const video = res.locals.videoAll |
d5d9b6d7 C |
29 | |
30 | const paramResolution = req.params.resolution as unknown as number // We casted to int above | |
31 | const paramFPS = req.params.fps as unknown as number // We casted to int above | |
32 | ||
c48e82b5 | 33 | const videoFile = video.VideoFiles.find(f => { |
d5d9b6d7 | 34 | return f.resolution === paramResolution && (!req.params.fps || paramFPS) |
c48e82b5 C |
35 | }) |
36 | ||
37 | if (!videoFile) return res.status(404).json({ error: 'Video file not found.' }) | |
38 | res.locals.videoFile = videoFile | |
39 | ||
46f8d69b | 40 | const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id) |
09209296 C |
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 | ||
48 | const videoPlaylistRedundancyGetValidator = [ | |
d5d9b6d7 C |
49 | param('videoId') |
50 | .custom(isIdOrUUIDValid) | |
51 | .not().isEmpty().withMessage('Should have a valid video id'), | |
52 | param('streamingPlaylistType') | |
53 | .customSanitizer(toIntOrNull) | |
54 | .custom(exists).withMessage('Should have a valid streaming playlist type'), | |
09209296 C |
55 | |
56 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
57 | logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params }) | |
58 | ||
59 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 60 | if (!await doesVideoExist(req.params.videoId, res)) return |
09209296 | 61 | |
453e83ea | 62 | const video = res.locals.videoAll |
d5d9b6d7 C |
63 | |
64 | const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above | |
65 | const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType) | |
09209296 C |
66 | |
67 | if (!videoStreamingPlaylist) return res.status(404).json({ error: 'Video playlist not found.' }) | |
68 | res.locals.videoStreamingPlaylist = videoStreamingPlaylist | |
69 | ||
70 | const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id) | |
71 | if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' }) | |
c48e82b5 C |
72 | res.locals.videoRedundancy = videoRedundancy |
73 | ||
74 | return next() | |
75 | } | |
76 | ] | |
77 | ||
78 | const updateServerRedundancyValidator = [ | |
79 | param('host').custom(isHostValid).withMessage('Should have a valid host'), | |
80 | body('redundancyAllowed') | |
c8861d5d | 81 | .customSanitizer(toBooleanOrNull) |
c48e82b5 C |
82 | .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'), |
83 | ||
84 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
85 | logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params }) | |
86 | ||
87 | if (areValidationErrors(req, res)) return | |
88 | ||
89 | const server = await ServerModel.loadByHost(req.params.host) | |
90 | ||
91 | if (!server) { | |
92 | return res | |
93 | .status(404) | |
94 | .json({ | |
95 | error: `Server ${req.params.host} not found.` | |
96 | }) | |
97 | .end() | |
98 | } | |
99 | ||
100 | res.locals.server = server | |
101 | return next() | |
102 | } | |
103 | ] | |
104 | ||
b764380a C |
105 | const listVideoRedundanciesValidator = [ |
106 | query('target') | |
107 | .custom(isVideoRedundancyTarget).withMessage('Should have a valid video redundancies target'), | |
108 | ||
a1587156 | 109 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b764380a C |
110 | logger.debug('Checking listVideoRedundanciesValidator parameters', { parameters: req.query }) |
111 | ||
112 | if (areValidationErrors(req, res)) return | |
113 | ||
114 | return next() | |
115 | } | |
116 | ] | |
117 | ||
118 | const addVideoRedundancyValidator = [ | |
119 | body('videoId') | |
120 | .custom(isIdValid) | |
121 | .withMessage('Should have a valid video id'), | |
122 | ||
123 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
124 | logger.debug('Checking addVideoRedundancyValidator parameters', { parameters: req.query }) | |
125 | ||
126 | if (areValidationErrors(req, res)) return | |
127 | ||
128 | if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return | |
129 | ||
130 | if (res.locals.onlyVideo.remote === false) { | |
131 | return res.status(400) | |
132 | .json({ error: 'Cannot create a redundancy on a local video' }) | |
133 | .end() | |
134 | } | |
135 | ||
136 | const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid) | |
137 | if (alreadyExists) { | |
138 | return res.status(409) | |
139 | .json({ error: 'This video is already duplicated by your instance.' }) | |
140 | } | |
141 | ||
142 | return next() | |
143 | } | |
144 | ] | |
145 | ||
146 | const removeVideoRedundancyValidator = [ | |
147 | param('redundancyId') | |
148 | .custom(isIdValid) | |
149 | .withMessage('Should have a valid redundancy id'), | |
150 | ||
151 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
152 | logger.debug('Checking removeVideoRedundancyValidator parameters', { parameters: req.query }) | |
153 | ||
154 | if (areValidationErrors(req, res)) return | |
155 | ||
156 | const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10)) | |
157 | if (!redundancy) { | |
158 | return res.status(404) | |
159 | .json({ error: 'Video redundancy not found' }) | |
160 | .end() | |
161 | } | |
162 | ||
163 | res.locals.videoRedundancy = redundancy | |
164 | ||
165 | return next() | |
166 | } | |
167 | ] | |
168 | ||
c48e82b5 C |
169 | // --------------------------------------------------------------------------- |
170 | ||
171 | export { | |
09209296 C |
172 | videoFileRedundancyGetValidator, |
173 | videoPlaylistRedundancyGetValidator, | |
b764380a C |
174 | updateServerRedundancyValidator, |
175 | listVideoRedundanciesValidator, | |
176 | addVideoRedundancyValidator, | |
177 | removeVideoRedundancyValidator | |
c48e82b5 | 178 | } |