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