]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { body, param, query } from 'express-validator' | |
3 | import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies' | |
4 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | |
5 | import { exists, isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' | |
6 | import { isHostValid } from '../../helpers/custom-validators/servers' | |
7 | import { logger } from '../../helpers/logger' | |
8 | import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' | |
9 | import { ServerModel } from '../../models/server/server' | |
10 | import { areValidationErrors, doesVideoExist } from './shared' | |
11 | ||
12 | const videoFileRedundancyGetValidator = [ | |
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) => { | |
23 | logger.debug('Checking videoFileRedundancyGetValidator parameters', { parameters: req.params }) | |
24 | ||
25 | if (areValidationErrors(req, res)) return | |
26 | if (!await doesVideoExist(req.params.videoId, res)) return | |
27 | ||
28 | const video = res.locals.videoAll | |
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 | ||
33 | const videoFile = video.VideoFiles.find(f => { | |
34 | return f.resolution === paramResolution && (!req.params.fps || paramFPS) | |
35 | }) | |
36 | ||
37 | if (!videoFile) { | |
38 | return res.fail({ | |
39 | status: HttpStatusCode.NOT_FOUND_404, | |
40 | message: 'Video file not found.' | |
41 | }) | |
42 | } | |
43 | res.locals.videoFile = videoFile | |
44 | ||
45 | const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id) | |
46 | if (!videoRedundancy) { | |
47 | return res.fail({ | |
48 | status: HttpStatusCode.NOT_FOUND_404, | |
49 | message: 'Video redundancy not found.' | |
50 | }) | |
51 | } | |
52 | res.locals.videoRedundancy = videoRedundancy | |
53 | ||
54 | return next() | |
55 | } | |
56 | ] | |
57 | ||
58 | const videoPlaylistRedundancyGetValidator = [ | |
59 | param('videoId') | |
60 | .custom(isIdOrUUIDValid) | |
61 | .not().isEmpty().withMessage('Should have a valid video id'), | |
62 | param('streamingPlaylistType') | |
63 | .customSanitizer(toIntOrNull) | |
64 | .custom(exists).withMessage('Should have a valid streaming playlist type'), | |
65 | ||
66 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
67 | logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params }) | |
68 | ||
69 | if (areValidationErrors(req, res)) return | |
70 | if (!await doesVideoExist(req.params.videoId, res)) return | |
71 | ||
72 | const video = res.locals.videoAll | |
73 | ||
74 | const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above | |
75 | const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType) | |
76 | ||
77 | if (!videoStreamingPlaylist) { | |
78 | return res.fail({ | |
79 | status: HttpStatusCode.NOT_FOUND_404, | |
80 | message: 'Video playlist not found.' | |
81 | }) | |
82 | } | |
83 | res.locals.videoStreamingPlaylist = videoStreamingPlaylist | |
84 | ||
85 | const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id) | |
86 | if (!videoRedundancy) { | |
87 | return res.fail({ | |
88 | status: HttpStatusCode.NOT_FOUND_404, | |
89 | message: 'Video redundancy not found.' | |
90 | }) | |
91 | } | |
92 | res.locals.videoRedundancy = videoRedundancy | |
93 | ||
94 | return next() | |
95 | } | |
96 | ] | |
97 | ||
98 | const updateServerRedundancyValidator = [ | |
99 | param('host').custom(isHostValid).withMessage('Should have a valid host'), | |
100 | body('redundancyAllowed') | |
101 | .customSanitizer(toBooleanOrNull) | |
102 | .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'), | |
103 | ||
104 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
105 | logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params }) | |
106 | ||
107 | if (areValidationErrors(req, res)) return | |
108 | ||
109 | const server = await ServerModel.loadByHost(req.params.host) | |
110 | ||
111 | if (!server) { | |
112 | return res.fail({ | |
113 | status: HttpStatusCode.NOT_FOUND_404, | |
114 | message: `Server ${req.params.host} not found.` | |
115 | }) | |
116 | } | |
117 | ||
118 | res.locals.server = server | |
119 | return next() | |
120 | } | |
121 | ] | |
122 | ||
123 | const listVideoRedundanciesValidator = [ | |
124 | query('target') | |
125 | .custom(isVideoRedundancyTarget).withMessage('Should have a valid video redundancies target'), | |
126 | ||
127 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
128 | logger.debug('Checking listVideoRedundanciesValidator parameters', { parameters: req.query }) | |
129 | ||
130 | if (areValidationErrors(req, res)) return | |
131 | ||
132 | return next() | |
133 | } | |
134 | ] | |
135 | ||
136 | const addVideoRedundancyValidator = [ | |
137 | body('videoId') | |
138 | .custom(isIdValid) | |
139 | .withMessage('Should have a valid video id'), | |
140 | ||
141 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
142 | logger.debug('Checking addVideoRedundancyValidator parameters', { parameters: req.query }) | |
143 | ||
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 | .withMessage('Should have a valid redundancy id'), | |
172 | ||
173 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
174 | logger.debug('Checking removeVideoRedundancyValidator parameters', { parameters: req.query }) | |
175 | ||
176 | if (areValidationErrors(req, res)) return | |
177 | ||
178 | const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10)) | |
179 | if (!redundancy) { | |
180 | return res.fail({ | |
181 | status: HttpStatusCode.NOT_FOUND_404, | |
182 | message: 'Video redundancy not found' | |
183 | }) | |
184 | } | |
185 | ||
186 | res.locals.videoRedundancy = redundancy | |
187 | ||
188 | return next() | |
189 | } | |
190 | ] | |
191 | ||
192 | // --------------------------------------------------------------------------- | |
193 | ||
194 | export { | |
195 | videoFileRedundancyGetValidator, | |
196 | videoPlaylistRedundancyGetValidator, | |
197 | updateServerRedundancyValidator, | |
198 | listVideoRedundanciesValidator, | |
199 | addVideoRedundancyValidator, | |
200 | removeVideoRedundancyValidator | |
201 | } |