diff options
Diffstat (limited to 'server/middlewares')
-rw-r--r-- | server/middlewares/validators/blacklist.ts | 35 | ||||
-rw-r--r-- | server/middlewares/validators/index.ts | 2 | ||||
-rw-r--r-- | server/middlewares/validators/video-blacklist.ts | 67 | ||||
-rw-r--r-- | server/middlewares/validators/videos.ts | 58 |
4 files changed, 71 insertions, 91 deletions
diff --git a/server/middlewares/validators/blacklist.ts b/server/middlewares/validators/blacklist.ts deleted file mode 100644 index fe8fa40a4..000000000 --- a/server/middlewares/validators/blacklist.ts +++ /dev/null | |||
@@ -1,35 +0,0 @@ | |||
1 | import { param } from 'express-validator/check' | ||
2 | import * as express from 'express' | ||
3 | |||
4 | import { database as db } from '../../initializers/database' | ||
5 | import { checkErrors } from './utils' | ||
6 | import { logger } from '../../helpers' | ||
7 | |||
8 | const blacklistRemoveValidator = [ | ||
9 | param('id').isNumeric().not().isEmpty().withMessage('Should have a valid id'), | ||
10 | |||
11 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
12 | logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) | ||
13 | |||
14 | checkErrors(req, res, () => { | ||
15 | db.BlacklistedVideo.loadById(req.params.id) | ||
16 | .then(entry => { | ||
17 | if (!entry) return res.status(404).send('Blacklisted video not found') | ||
18 | |||
19 | res.locals.blacklistEntryToRemove = entry | ||
20 | |||
21 | next() | ||
22 | }) | ||
23 | .catch(err => { | ||
24 | logger.error('Error in blacklistRemove request validator', { error: err }) | ||
25 | return res.sendStatus(500) | ||
26 | }) | ||
27 | }) | ||
28 | } | ||
29 | ] | ||
30 | |||
31 | // --------------------------------------------------------------------------- | ||
32 | |||
33 | export { | ||
34 | blacklistRemoveValidator | ||
35 | } | ||
diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts index a6198e22c..418fa5f1d 100644 --- a/server/middlewares/validators/index.ts +++ b/server/middlewares/validators/index.ts | |||
@@ -4,4 +4,4 @@ export * from './pods' | |||
4 | export * from './sort' | 4 | export * from './sort' |
5 | export * from './users' | 5 | export * from './users' |
6 | export * from './videos' | 6 | export * from './videos' |
7 | export * from './blacklist' | 7 | export * from './video-blacklist' |
diff --git a/server/middlewares/validators/video-blacklist.ts b/server/middlewares/validators/video-blacklist.ts new file mode 100644 index 000000000..30c6d4bd9 --- /dev/null +++ b/server/middlewares/validators/video-blacklist.ts | |||
@@ -0,0 +1,67 @@ | |||
1 | import { param } from 'express-validator/check' | ||
2 | import * as express from 'express' | ||
3 | |||
4 | import { database as db } from '../../initializers/database' | ||
5 | import { checkErrors } from './utils' | ||
6 | import { logger, isVideoIdOrUUIDValid, checkVideoExists } from '../../helpers' | ||
7 | |||
8 | const videosBlacklistRemoveValidator = [ | ||
9 | param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | ||
10 | |||
11 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
12 | logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) | ||
13 | |||
14 | checkErrors(req, res, () => { | ||
15 | checkVideoExists(req.params.videoId, res, () => { | ||
16 | checkVideoIsBlacklisted(req, res, next) | ||
17 | }) | ||
18 | }) | ||
19 | } | ||
20 | ] | ||
21 | |||
22 | const videosBlacklistAddValidator = [ | ||
23 | param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | ||
24 | |||
25 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
26 | logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) | ||
27 | |||
28 | checkErrors(req, res, () => { | ||
29 | checkVideoExists(req.params.videoId, res, () => { | ||
30 | checkVideoIsBlacklistable(req, res, next) | ||
31 | }) | ||
32 | }) | ||
33 | } | ||
34 | ] | ||
35 | |||
36 | // --------------------------------------------------------------------------- | ||
37 | |||
38 | export { | ||
39 | videosBlacklistAddValidator, | ||
40 | videosBlacklistRemoveValidator | ||
41 | } | ||
42 | // --------------------------------------------------------------------------- | ||
43 | |||
44 | function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) { | ||
45 | if (res.locals.video.isOwned() === true) { | ||
46 | return res.status(403) | ||
47 | .json({ error: 'Cannot blacklist a local video' }) | ||
48 | .end() | ||
49 | } | ||
50 | |||
51 | callback() | ||
52 | } | ||
53 | |||
54 | function checkVideoIsBlacklisted (req: express.Request, res: express.Response, callback: () => void) { | ||
55 | db.BlacklistedVideo.loadByVideoId(res.locals.video.id) | ||
56 | .then(blacklistedVideo => { | ||
57 | if (!blacklistedVideo) return res.status(404).send('Blacklisted video not found') | ||
58 | |||
59 | res.locals.blacklistedVideo = blacklistedVideo | ||
60 | |||
61 | callback() | ||
62 | }) | ||
63 | .catch(err => { | ||
64 | logger.error('Error in blacklistRemove request validator', { error: err }) | ||
65 | return res.sendStatus(500) | ||
66 | }) | ||
67 | } | ||
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 | } | ||