diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-10-10 10:02:18 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-10-10 10:18:16 +0200 |
commit | 35bf0c83c80f59ca79f4b84fac8700f17adeb22d (patch) | |
tree | a9b2106096d6ba04d7219051b17fd32cfbe6a885 /server/middlewares/validators/videos.ts | |
parent | 769d332177a5b02d5c2ffc134687d3b4ed65bae9 (diff) | |
download | PeerTube-35bf0c83c80f59ca79f4b84fac8700f17adeb22d.tar.gz PeerTube-35bf0c83c80f59ca79f4b84fac8700f17adeb22d.tar.zst PeerTube-35bf0c83c80f59ca79f4b84fac8700f17adeb22d.zip |
Video blacklist refractoring
Diffstat (limited to 'server/middlewares/validators/videos.ts')
-rw-r--r-- | server/middlewares/validators/videos.ts | 58 |
1 files changed, 3 insertions, 55 deletions
diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts index 5f213f974..deed07524 100644 --- a/server/middlewares/validators/videos.ts +++ b/server/middlewares/validators/videos.ts | |||
@@ -1,7 +1,5 @@ | |||
1 | import { body, param, query } from 'express-validator/check' | 1 | import { body, param, query } from 'express-validator/check' |
2 | import * as express from 'express' | 2 | import * as express from 'express' |
3 | import * as Promise from 'bluebird' | ||
4 | import * as validator from 'validator' | ||
5 | 3 | ||
6 | import { database as db } from '../../initializers/database' | 4 | import { database as db } from '../../initializers/database' |
7 | import { checkErrors } from './utils' | 5 | import { checkErrors } from './utils' |
@@ -20,9 +18,9 @@ import { | |||
20 | isVideoIdOrUUIDValid, | 18 | isVideoIdOrUUIDValid, |
21 | isVideoAbuseReasonValid, | 19 | isVideoAbuseReasonValid, |
22 | isVideoRatingTypeValid, | 20 | isVideoRatingTypeValid, |
23 | getDurationFromVideoFile | 21 | getDurationFromVideoFile, |
22 | checkVideoExists | ||
24 | } from '../../helpers' | 23 | } from '../../helpers' |
25 | import { VideoInstance } from '../../models' | ||
26 | 24 | ||
27 | const videosAddValidator = [ | 25 | const videosAddValidator = [ |
28 | body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage('Should have a valid file'), | 26 | body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage('Should have a valid file'), |
@@ -186,20 +184,6 @@ const videoRateValidator = [ | |||
186 | } | 184 | } |
187 | ] | 185 | ] |
188 | 186 | ||
189 | const videosBlacklistValidator = [ | ||
190 | param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | ||
191 | |||
192 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
193 | logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) | ||
194 | |||
195 | checkErrors(req, res, () => { | ||
196 | checkVideoExists(req.params.id, res, () => { | ||
197 | checkVideoIsBlacklistable(req, res, next) | ||
198 | }) | ||
199 | }) | ||
200 | } | ||
201 | ] | ||
202 | |||
203 | // --------------------------------------------------------------------------- | 187 | // --------------------------------------------------------------------------- |
204 | 188 | ||
205 | export { | 189 | export { |
@@ -211,37 +195,11 @@ export { | |||
211 | 195 | ||
212 | videoAbuseReportValidator, | 196 | videoAbuseReportValidator, |
213 | 197 | ||
214 | videoRateValidator, | 198 | videoRateValidator |
215 | |||
216 | videosBlacklistValidator | ||
217 | } | 199 | } |
218 | 200 | ||
219 | // --------------------------------------------------------------------------- | 201 | // --------------------------------------------------------------------------- |
220 | 202 | ||
221 | function checkVideoExists (id: string, res: express.Response, callback: () => void) { | ||
222 | let promise: Promise<VideoInstance> | ||
223 | if (validator.isInt(id)) { | ||
224 | promise = db.Video.loadAndPopulateAuthorAndPodAndTags(+id) | ||
225 | } else { // UUID | ||
226 | promise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(id) | ||
227 | } | ||
228 | |||
229 | promise.then(video => { | ||
230 | if (!video) { | ||
231 | return res.status(404) | ||
232 | .json({ error: 'Video not found' }) | ||
233 | .end() | ||
234 | } | ||
235 | |||
236 | res.locals.video = video | ||
237 | callback() | ||
238 | }) | ||
239 | .catch(err => { | ||
240 | logger.error('Error in video request validator.', err) | ||
241 | return res.sendStatus(500) | ||
242 | }) | ||
243 | } | ||
244 | |||
245 | function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) { | 203 | function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) { |
246 | // Retrieve the user who did the request | 204 | // Retrieve the user who did the request |
247 | db.User.loadById(userId) | 205 | db.User.loadById(userId) |
@@ -269,13 +227,3 @@ function checkUserCanDeleteVideo (userId: number, res: express.Response, callbac | |||
269 | return res.sendStatus(500) | 227 | return res.sendStatus(500) |
270 | }) | 228 | }) |
271 | } | 229 | } |
272 | |||
273 | function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) { | ||
274 | if (res.locals.video.isOwned() === true) { | ||
275 | return res.status(403) | ||
276 | .json({ error: 'Cannot blacklist a local video' }) | ||
277 | .end() | ||
278 | } | ||
279 | |||
280 | callback() | ||
281 | } | ||