]>
Commit | Line | Data |
---|---|---|
c48e82b5 | 1 | import * as express from 'express' |
b764380a | 2 | import { body, param, query } from 'express-validator' |
10363c74 C |
3 | import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies' |
4 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | |
b764380a | 5 | import { exists, isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' |
10363c74 | 6 | import { isHostValid } from '../../helpers/custom-validators/servers' |
c48e82b5 | 7 | import { logger } from '../../helpers/logger' |
c48e82b5 | 8 | import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' |
c48e82b5 | 9 | import { ServerModel } from '../../models/server/server' |
10363c74 | 10 | import { areValidationErrors, doesVideoExist } from './shared' |
c48e82b5 | 11 | |
09209296 | 12 | const videoFileRedundancyGetValidator = [ |
c48e82b5 C |
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) => { | |
09209296 | 23 | logger.debug('Checking videoFileRedundancyGetValidator parameters', { parameters: req.params }) |
c48e82b5 C |
24 | |
25 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 26 | if (!await doesVideoExist(req.params.videoId, res)) return |
c48e82b5 | 27 | |
453e83ea | 28 | const video = res.locals.videoAll |
d5d9b6d7 C |
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 | ||
c48e82b5 | 33 | const videoFile = video.VideoFiles.find(f => { |
d5d9b6d7 | 34 | return f.resolution === paramResolution && (!req.params.fps || paramFPS) |
c48e82b5 C |
35 | }) |
36 | ||
76148b27 RK |
37 | if (!videoFile) { |
38 | return res.fail({ | |
39 | status: HttpStatusCode.NOT_FOUND_404, | |
40 | message: 'Video file not found.' | |
41 | }) | |
42 | } | |
c48e82b5 C |
43 | res.locals.videoFile = videoFile |
44 | ||
46f8d69b | 45 | const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id) |
76148b27 RK |
46 | if (!videoRedundancy) { |
47 | return res.fail({ | |
48 | status: HttpStatusCode.NOT_FOUND_404, | |
49 | message: 'Video redundancy not found.' | |
50 | }) | |
51 | } | |
09209296 C |
52 | res.locals.videoRedundancy = videoRedundancy |
53 | ||
54 | return next() | |
55 | } | |
56 | ] | |
57 | ||
58 | const videoPlaylistRedundancyGetValidator = [ | |
d5d9b6d7 C |
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'), | |
09209296 C |
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 | |
0f6acda1 | 70 | if (!await doesVideoExist(req.params.videoId, res)) return |
09209296 | 71 | |
453e83ea | 72 | const video = res.locals.videoAll |
d5d9b6d7 C |
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) | |
09209296 | 76 | |
76148b27 RK |
77 | if (!videoStreamingPlaylist) { |
78 | return res.fail({ | |
79 | status: HttpStatusCode.NOT_FOUND_404, | |
80 | message: 'Video playlist not found.' | |
81 | }) | |
82 | } | |
09209296 C |
83 | res.locals.videoStreamingPlaylist = videoStreamingPlaylist |
84 | ||
85 | const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id) | |
76148b27 RK |
86 | if (!videoRedundancy) { |
87 | return res.fail({ | |
88 | status: HttpStatusCode.NOT_FOUND_404, | |
89 | message: 'Video redundancy not found.' | |
90 | }) | |
91 | } | |
c48e82b5 C |
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') | |
c8861d5d | 101 | .customSanitizer(toBooleanOrNull) |
c48e82b5 C |
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) { | |
76148b27 RK |
112 | return res.fail({ |
113 | status: HttpStatusCode.NOT_FOUND_404, | |
114 | message: `Server ${req.params.host} not found.` | |
115 | }) | |
c48e82b5 C |
116 | } |
117 | ||
118 | res.locals.server = server | |
119 | return next() | |
120 | } | |
121 | ] | |
122 | ||
b764380a C |
123 | const listVideoRedundanciesValidator = [ |
124 | query('target') | |
125 | .custom(isVideoRedundancyTarget).withMessage('Should have a valid video redundancies target'), | |
126 | ||
a1587156 | 127 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b764380a C |
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) { | |
76148b27 | 149 | return res.fail({ message: 'Cannot create a redundancy on a local video' }) |
17b7d4b3 C |
150 | } |
151 | ||
152 | if (res.locals.onlyVideo.isLive) { | |
76148b27 | 153 | return res.fail({ message: 'Cannot create a redundancy of a live video' }) |
b764380a C |
154 | } |
155 | ||
156 | const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid) | |
157 | if (alreadyExists) { | |
76148b27 RK |
158 | return res.fail({ |
159 | status: HttpStatusCode.CONFLICT_409, | |
160 | message: 'This video is already duplicated by your instance.' | |
161 | }) | |
b764380a C |
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) { | |
76148b27 RK |
180 | return res.fail({ |
181 | status: HttpStatusCode.NOT_FOUND_404, | |
182 | message: 'Video redundancy not found' | |
183 | }) | |
b764380a C |
184 | } |
185 | ||
186 | res.locals.videoRedundancy = redundancy | |
187 | ||
188 | return next() | |
189 | } | |
190 | ] | |
191 | ||
c48e82b5 C |
192 | // --------------------------------------------------------------------------- |
193 | ||
194 | export { | |
09209296 C |
195 | videoFileRedundancyGetValidator, |
196 | videoPlaylistRedundancyGetValidator, | |
b764380a C |
197 | updateServerRedundancyValidator, |
198 | listVideoRedundanciesValidator, | |
199 | addVideoRedundancyValidator, | |
200 | removeVideoRedundancyValidator | |
c48e82b5 | 201 | } |