]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
b764380a | 2 | import { body, param, query } from 'express-validator' |
10363c74 | 3 | import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies' |
c0e8b12e | 4 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' |
d4a8e7a6 C |
5 | import { |
6 | exists, | |
7 | isBooleanValid, | |
8 | isIdOrUUIDValid, | |
9 | isIdValid, | |
10 | toBooleanOrNull, | |
11 | toCompleteUUID, | |
12 | toIntOrNull | |
13 | } from '../../helpers/custom-validators/misc' | |
10363c74 | 14 | import { isHostValid } from '../../helpers/custom-validators/servers' |
c48e82b5 | 15 | import { logger } from '../../helpers/logger' |
c48e82b5 | 16 | import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' |
c48e82b5 | 17 | import { ServerModel } from '../../models/server/server' |
d4a8e7a6 | 18 | import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from './shared' |
c48e82b5 | 19 | |
09209296 | 20 | const videoFileRedundancyGetValidator = [ |
d4a8e7a6 C |
21 | isValidVideoIdParam('videoId'), |
22 | ||
c48e82b5 C |
23 | param('resolution') |
24 | .customSanitizer(toIntOrNull) | |
396f6f01 | 25 | .custom(exists), |
c48e82b5 C |
26 | param('fps') |
27 | .optional() | |
28 | .customSanitizer(toIntOrNull) | |
396f6f01 | 29 | .custom(exists), |
c48e82b5 C |
30 | |
31 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
09209296 | 32 | logger.debug('Checking videoFileRedundancyGetValidator parameters', { parameters: req.params }) |
c48e82b5 C |
33 | |
34 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 35 | if (!await doesVideoExist(req.params.videoId, res)) return |
c48e82b5 | 36 | |
453e83ea | 37 | const video = res.locals.videoAll |
d5d9b6d7 C |
38 | |
39 | const paramResolution = req.params.resolution as unknown as number // We casted to int above | |
40 | const paramFPS = req.params.fps as unknown as number // We casted to int above | |
41 | ||
c48e82b5 | 42 | const videoFile = video.VideoFiles.find(f => { |
d5d9b6d7 | 43 | return f.resolution === paramResolution && (!req.params.fps || paramFPS) |
c48e82b5 C |
44 | }) |
45 | ||
76148b27 RK |
46 | if (!videoFile) { |
47 | return res.fail({ | |
48 | status: HttpStatusCode.NOT_FOUND_404, | |
49 | message: 'Video file not found.' | |
50 | }) | |
51 | } | |
c48e82b5 C |
52 | res.locals.videoFile = videoFile |
53 | ||
46f8d69b | 54 | const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id) |
76148b27 RK |
55 | if (!videoRedundancy) { |
56 | return res.fail({ | |
57 | status: HttpStatusCode.NOT_FOUND_404, | |
58 | message: 'Video redundancy not found.' | |
59 | }) | |
60 | } | |
09209296 C |
61 | res.locals.videoRedundancy = videoRedundancy |
62 | ||
63 | return next() | |
64 | } | |
65 | ] | |
66 | ||
67 | const videoPlaylistRedundancyGetValidator = [ | |
d4a8e7a6 C |
68 | isValidVideoIdParam('videoId'), |
69 | ||
d5d9b6d7 C |
70 | param('streamingPlaylistType') |
71 | .customSanitizer(toIntOrNull) | |
396f6f01 | 72 | .custom(exists), |
09209296 C |
73 | |
74 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
75 | logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params }) | |
76 | ||
77 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 78 | if (!await doesVideoExist(req.params.videoId, res)) return |
09209296 | 79 | |
453e83ea | 80 | const video = res.locals.videoAll |
d5d9b6d7 C |
81 | |
82 | const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above | |
83 | const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType) | |
09209296 | 84 | |
76148b27 RK |
85 | if (!videoStreamingPlaylist) { |
86 | return res.fail({ | |
87 | status: HttpStatusCode.NOT_FOUND_404, | |
88 | message: 'Video playlist not found.' | |
89 | }) | |
90 | } | |
09209296 C |
91 | res.locals.videoStreamingPlaylist = videoStreamingPlaylist |
92 | ||
93 | const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id) | |
76148b27 RK |
94 | if (!videoRedundancy) { |
95 | return res.fail({ | |
96 | status: HttpStatusCode.NOT_FOUND_404, | |
97 | message: 'Video redundancy not found.' | |
98 | }) | |
99 | } | |
c48e82b5 C |
100 | res.locals.videoRedundancy = videoRedundancy |
101 | ||
102 | return next() | |
103 | } | |
104 | ] | |
105 | ||
106 | const updateServerRedundancyValidator = [ | |
396f6f01 C |
107 | param('host') |
108 | .custom(isHostValid), | |
109 | ||
c48e82b5 | 110 | body('redundancyAllowed') |
c8861d5d | 111 | .customSanitizer(toBooleanOrNull) |
396f6f01 | 112 | .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed boolean'), |
c48e82b5 C |
113 | |
114 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
115 | logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params }) | |
116 | ||
117 | if (areValidationErrors(req, res)) return | |
118 | ||
119 | const server = await ServerModel.loadByHost(req.params.host) | |
120 | ||
121 | if (!server) { | |
76148b27 RK |
122 | return res.fail({ |
123 | status: HttpStatusCode.NOT_FOUND_404, | |
124 | message: `Server ${req.params.host} not found.` | |
125 | }) | |
c48e82b5 C |
126 | } |
127 | ||
128 | res.locals.server = server | |
129 | return next() | |
130 | } | |
131 | ] | |
132 | ||
b764380a C |
133 | const listVideoRedundanciesValidator = [ |
134 | query('target') | |
396f6f01 | 135 | .custom(isVideoRedundancyTarget), |
b764380a | 136 | |
a1587156 | 137 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b764380a C |
138 | logger.debug('Checking listVideoRedundanciesValidator parameters', { parameters: req.query }) |
139 | ||
140 | if (areValidationErrors(req, res)) return | |
141 | ||
142 | return next() | |
143 | } | |
144 | ] | |
145 | ||
146 | const addVideoRedundancyValidator = [ | |
147 | body('videoId') | |
d4a8e7a6 | 148 | .customSanitizer(toCompleteUUID) |
396f6f01 | 149 | .custom(isIdOrUUIDValid), |
b764380a C |
150 | |
151 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
152 | logger.debug('Checking addVideoRedundancyValidator parameters', { parameters: req.query }) | |
153 | ||
154 | if (areValidationErrors(req, res)) return | |
155 | ||
156 | if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return | |
157 | ||
158 | if (res.locals.onlyVideo.remote === false) { | |
76148b27 | 159 | return res.fail({ message: 'Cannot create a redundancy on a local video' }) |
17b7d4b3 C |
160 | } |
161 | ||
162 | if (res.locals.onlyVideo.isLive) { | |
76148b27 | 163 | return res.fail({ message: 'Cannot create a redundancy of a live video' }) |
b764380a C |
164 | } |
165 | ||
166 | const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid) | |
167 | if (alreadyExists) { | |
76148b27 RK |
168 | return res.fail({ |
169 | status: HttpStatusCode.CONFLICT_409, | |
170 | message: 'This video is already duplicated by your instance.' | |
171 | }) | |
b764380a C |
172 | } |
173 | ||
174 | return next() | |
175 | } | |
176 | ] | |
177 | ||
178 | const removeVideoRedundancyValidator = [ | |
179 | param('redundancyId') | |
396f6f01 | 180 | .custom(isIdValid), |
b764380a C |
181 | |
182 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
183 | logger.debug('Checking removeVideoRedundancyValidator parameters', { parameters: req.query }) | |
184 | ||
185 | if (areValidationErrors(req, res)) return | |
186 | ||
187 | const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10)) | |
188 | if (!redundancy) { | |
76148b27 RK |
189 | return res.fail({ |
190 | status: HttpStatusCode.NOT_FOUND_404, | |
191 | message: 'Video redundancy not found' | |
192 | }) | |
b764380a C |
193 | } |
194 | ||
195 | res.locals.videoRedundancy = redundancy | |
196 | ||
197 | return next() | |
198 | } | |
199 | ] | |
200 | ||
c48e82b5 C |
201 | // --------------------------------------------------------------------------- |
202 | ||
203 | export { | |
09209296 C |
204 | videoFileRedundancyGetValidator, |
205 | videoPlaylistRedundancyGetValidator, | |
b764380a C |
206 | updateServerRedundancyValidator, |
207 | listVideoRedundanciesValidator, | |
208 | addVideoRedundancyValidator, | |
209 | removeVideoRedundancyValidator | |
c48e82b5 | 210 | } |