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