]>
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) | |
25 | .custom(exists).withMessage('Should have a valid resolution'), | |
26 | param('fps') | |
27 | .optional() | |
28 | .customSanitizer(toIntOrNull) | |
29 | .custom(exists).withMessage('Should have a valid fps'), | |
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) | |
72 | .custom(exists).withMessage('Should have a valid streaming playlist type'), | |
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 = [ | |
107 | param('host').custom(isHostValid).withMessage('Should have a valid host'), | |
108 | body('redundancyAllowed') | |
c8861d5d | 109 | .customSanitizer(toBooleanOrNull) |
c48e82b5 C |
110 | .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'), |
111 | ||
112 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
113 | logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params }) | |
114 | ||
115 | if (areValidationErrors(req, res)) return | |
116 | ||
117 | const server = await ServerModel.loadByHost(req.params.host) | |
118 | ||
119 | if (!server) { | |
76148b27 RK |
120 | return res.fail({ |
121 | status: HttpStatusCode.NOT_FOUND_404, | |
122 | message: `Server ${req.params.host} not found.` | |
123 | }) | |
c48e82b5 C |
124 | } |
125 | ||
126 | res.locals.server = server | |
127 | return next() | |
128 | } | |
129 | ] | |
130 | ||
b764380a C |
131 | const listVideoRedundanciesValidator = [ |
132 | query('target') | |
133 | .custom(isVideoRedundancyTarget).withMessage('Should have a valid video redundancies target'), | |
134 | ||
a1587156 | 135 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b764380a C |
136 | logger.debug('Checking listVideoRedundanciesValidator parameters', { parameters: req.query }) |
137 | ||
138 | if (areValidationErrors(req, res)) return | |
139 | ||
140 | return next() | |
141 | } | |
142 | ] | |
143 | ||
144 | const addVideoRedundancyValidator = [ | |
145 | body('videoId') | |
d4a8e7a6 C |
146 | .customSanitizer(toCompleteUUID) |
147 | .custom(isIdOrUUIDValid) | |
b764380a C |
148 | .withMessage('Should have a valid video id'), |
149 | ||
150 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
151 | logger.debug('Checking addVideoRedundancyValidator parameters', { parameters: req.query }) | |
152 | ||
153 | if (areValidationErrors(req, res)) return | |
154 | ||
155 | if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return | |
156 | ||
157 | if (res.locals.onlyVideo.remote === false) { | |
76148b27 | 158 | return res.fail({ message: 'Cannot create a redundancy on a local video' }) |
17b7d4b3 C |
159 | } |
160 | ||
161 | if (res.locals.onlyVideo.isLive) { | |
76148b27 | 162 | return res.fail({ message: 'Cannot create a redundancy of a live video' }) |
b764380a C |
163 | } |
164 | ||
165 | const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid) | |
166 | if (alreadyExists) { | |
76148b27 RK |
167 | return res.fail({ |
168 | status: HttpStatusCode.CONFLICT_409, | |
169 | message: 'This video is already duplicated by your instance.' | |
170 | }) | |
b764380a C |
171 | } |
172 | ||
173 | return next() | |
174 | } | |
175 | ] | |
176 | ||
177 | const removeVideoRedundancyValidator = [ | |
178 | param('redundancyId') | |
179 | .custom(isIdValid) | |
180 | .withMessage('Should have a valid redundancy id'), | |
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 | } |