aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-10 10:02:18 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-10 10:18:16 +0200
commit35bf0c83c80f59ca79f4b84fac8700f17adeb22d (patch)
treea9b2106096d6ba04d7219051b17fd32cfbe6a885 /server/middlewares/validators
parent769d332177a5b02d5c2ffc134687d3b4ed65bae9 (diff)
downloadPeerTube-35bf0c83c80f59ca79f4b84fac8700f17adeb22d.tar.gz
PeerTube-35bf0c83c80f59ca79f4b84fac8700f17adeb22d.tar.zst
PeerTube-35bf0c83c80f59ca79f4b84fac8700f17adeb22d.zip
Video blacklist refractoring
Diffstat (limited to 'server/middlewares/validators')
-rw-r--r--server/middlewares/validators/blacklist.ts35
-rw-r--r--server/middlewares/validators/index.ts2
-rw-r--r--server/middlewares/validators/video-blacklist.ts67
-rw-r--r--server/middlewares/validators/videos.ts58
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 @@
1import { param } from 'express-validator/check'
2import * as express from 'express'
3
4import { database as db } from '../../initializers/database'
5import { checkErrors } from './utils'
6import { logger } from '../../helpers'
7
8const 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
33export {
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'
4export * from './sort' 4export * from './sort'
5export * from './users' 5export * from './users'
6export * from './videos' 6export * from './videos'
7export * from './blacklist' 7export * 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 @@
1import { param } from 'express-validator/check'
2import * as express from 'express'
3
4import { database as db } from '../../initializers/database'
5import { checkErrors } from './utils'
6import { logger, isVideoIdOrUUIDValid, checkVideoExists } from '../../helpers'
7
8const 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
22const 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
38export {
39 videosBlacklistAddValidator,
40 videosBlacklistRemoveValidator
41}
42// ---------------------------------------------------------------------------
43
44function 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
54function 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 @@
1import { body, param, query } from 'express-validator/check' 1import { body, param, query } from 'express-validator/check'
2import * as express from 'express' 2import * as express from 'express'
3import * as Promise from 'bluebird'
4import * as validator from 'validator'
5 3
6import { database as db } from '../../initializers/database' 4import { database as db } from '../../initializers/database'
7import { checkErrors } from './utils' 5import { 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'
25import { VideoInstance } from '../../models'
26 24
27const videosAddValidator = [ 25const 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
189const 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
205export { 189export {
@@ -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
221function 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
245function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) { 203function 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
273function 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}