aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/reqValidators
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-07-01 16:16:40 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-07-01 16:16:40 +0200
commitfc51fde048f2c3ce1dd3e85f5528335040bae894 (patch)
tree6de1eeac1291d9398cc99cda926b189c39b3ea0b /server/middlewares/reqValidators
parent69b0a27cbbd69ca019eb7db5f917b1dd06dc82cd (diff)
downloadPeerTube-fc51fde048f2c3ce1dd3e85f5528335040bae894.tar.gz
PeerTube-fc51fde048f2c3ce1dd3e85f5528335040bae894.tar.zst
PeerTube-fc51fde048f2c3ce1dd3e85f5528335040bae894.zip
reqValidators --> validators
Diffstat (limited to 'server/middlewares/reqValidators')
-rw-r--r--server/middlewares/reqValidators/index.js19
-rw-r--r--server/middlewares/reqValidators/pagination.js21
-rw-r--r--server/middlewares/reqValidators/pods.js41
-rw-r--r--server/middlewares/reqValidators/remote.js41
-rw-r--r--server/middlewares/reqValidators/sort.js23
-rw-r--r--server/middlewares/reqValidators/utils.js25
-rw-r--r--server/middlewares/reqValidators/videos.js97
7 files changed, 0 insertions, 267 deletions
diff --git a/server/middlewares/reqValidators/index.js b/server/middlewares/reqValidators/index.js
deleted file mode 100644
index be68f6a29..000000000
--- a/server/middlewares/reqValidators/index.js
+++ /dev/null
@@ -1,19 +0,0 @@
1'use strict'
2
3const paginationReqValidators = require('./pagination')
4const podsReqValidators = require('./pods')
5const remoteReqValidators = require('./remote')
6const sortReqValidators = require('./sort')
7const videosReqValidators = require('./videos')
8
9const reqValidators = {
10 pagination: paginationReqValidators,
11 pods: podsReqValidators,
12 remote: remoteReqValidators,
13 sort: sortReqValidators,
14 videos: videosReqValidators
15}
16
17// ---------------------------------------------------------------------------
18
19module.exports = reqValidators
diff --git a/server/middlewares/reqValidators/pagination.js b/server/middlewares/reqValidators/pagination.js
deleted file mode 100644
index e598f269a..000000000
--- a/server/middlewares/reqValidators/pagination.js
+++ /dev/null
@@ -1,21 +0,0 @@
1'use strict'
2
3const checkErrors = require('./utils').checkErrors
4const logger = require('../../helpers/logger')
5
6const reqValidatorsPagination = {
7 pagination: pagination
8}
9
10function pagination (req, res, next) {
11 req.checkQuery('start', 'Should have a number start').optional().isInt()
12 req.checkQuery('count', 'Should have a number count').optional().isInt()
13
14 logger.debug('Checking pagination parameters', { parameters: req.query })
15
16 checkErrors(req, res, next)
17}
18
19// ---------------------------------------------------------------------------
20
21module.exports = reqValidatorsPagination
diff --git a/server/middlewares/reqValidators/pods.js b/server/middlewares/reqValidators/pods.js
deleted file mode 100644
index 78a4b76c1..000000000
--- a/server/middlewares/reqValidators/pods.js
+++ /dev/null
@@ -1,41 +0,0 @@
1'use strict'
2
3const checkErrors = require('./utils').checkErrors
4const friends = require('../../lib/friends')
5const logger = require('../../helpers/logger')
6
7const reqValidatorsPod = {
8 makeFriends: makeFriends,
9 podsAdd: podsAdd
10}
11
12function makeFriends (req, res, next) {
13 friends.hasFriends(function (err, hasFriends) {
14 if (err) {
15 logger.error('Cannot know if we have friends.', { error: err })
16 res.sendStatus(500)
17 }
18
19 if (hasFriends === true) {
20 // We need to quit our friends before make new ones
21 res.sendStatus(409)
22 } else {
23 return next()
24 }
25 })
26}
27
28function podsAdd (req, res, next) {
29 req.checkBody('url', 'Should have an url').notEmpty().isURL({ require_protocol: true })
30 req.checkBody('publicKey', 'Should have a public key').notEmpty()
31
32 // TODO: check we don't have it already
33
34 logger.debug('Checking podsAdd parameters', { parameters: req.body })
35
36 checkErrors(req, res, next)
37}
38
39// ---------------------------------------------------------------------------
40
41module.exports = reqValidatorsPod
diff --git a/server/middlewares/reqValidators/remote.js b/server/middlewares/reqValidators/remote.js
deleted file mode 100644
index dd8ee5f6e..000000000
--- a/server/middlewares/reqValidators/remote.js
+++ /dev/null
@@ -1,41 +0,0 @@
1'use strict'
2
3const checkErrors = require('./utils').checkErrors
4const logger = require('../../helpers/logger')
5
6const reqValidatorsRemote = {
7 dataToDecrypt: dataToDecrypt,
8 remoteVideos: remoteVideos,
9 signature: signature
10}
11
12function dataToDecrypt (req, res, next) {
13 req.checkBody('key', 'Should have a key').notEmpty()
14 req.checkBody('data', 'Should have data').notEmpty()
15
16 logger.debug('Checking dataToDecrypt parameters', { parameters: { keyLength: req.body.key.length, bodyLength: req.body.data.length } })
17
18 checkErrors(req, res, next)
19}
20
21function remoteVideos (req, res, next) {
22 req.checkBody('data').isArray()
23 req.checkBody('data').isEachRemoteVideosValid()
24
25 logger.debug('Checking remoteVideos parameters', { parameters: req.body })
26
27 checkErrors(req, res, next)
28}
29
30function signature (req, res, next) {
31 req.checkBody('signature.url', 'Should have a signature url').isURL()
32 req.checkBody('signature.signature', 'Should have a signature').notEmpty()
33
34 logger.debug('Checking signature parameters', { parameters: { signatureUrl: req.body.signature.url } })
35
36 checkErrors(req, res, next)
37}
38
39// ---------------------------------------------------------------------------
40
41module.exports = reqValidatorsRemote
diff --git a/server/middlewares/reqValidators/sort.js b/server/middlewares/reqValidators/sort.js
deleted file mode 100644
index 06e680ef4..000000000
--- a/server/middlewares/reqValidators/sort.js
+++ /dev/null
@@ -1,23 +0,0 @@
1'use strict'
2
3const checkErrors = require('./utils').checkErrors
4const constants = require('../../initializers/constants')
5const logger = require('../../helpers/logger')
6
7const reqValidatorsSort = {
8 videosSort: videosSort
9}
10
11function videosSort (req, res, next) {
12 const sortableColumns = constants.SORTABLE_COLUMNS.VIDEOS
13
14 req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns)
15
16 logger.debug('Checking sort parameters', { parameters: req.query })
17
18 checkErrors(req, res, next)
19}
20
21// ---------------------------------------------------------------------------
22
23module.exports = reqValidatorsSort
diff --git a/server/middlewares/reqValidators/utils.js b/server/middlewares/reqValidators/utils.js
deleted file mode 100644
index 198ed8d26..000000000
--- a/server/middlewares/reqValidators/utils.js
+++ /dev/null
@@ -1,25 +0,0 @@
1'use strict'
2
3const util = require('util')
4
5const logger = require('../../helpers/logger')
6
7const reqValidatorsUtils = {
8 checkErrors: checkErrors
9}
10
11function checkErrors (req, res, next, statusCode) {
12 if (statusCode === undefined) statusCode = 400
13 const errors = req.validationErrors()
14
15 if (errors) {
16 logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors })
17 return res.status(statusCode).send('There have been validation errors: ' + util.inspect(errors))
18 }
19
20 return next()
21}
22
23// ---------------------------------------------------------------------------
24
25module.exports = reqValidatorsUtils
diff --git a/server/middlewares/reqValidators/videos.js b/server/middlewares/reqValidators/videos.js
deleted file mode 100644
index 452fbc859..000000000
--- a/server/middlewares/reqValidators/videos.js
+++ /dev/null
@@ -1,97 +0,0 @@
1'use strict'
2
3const mongoose = require('mongoose')
4
5const checkErrors = require('./utils').checkErrors
6const constants = require('../../initializers/constants')
7const customValidators = require('../../helpers/customValidators')
8const logger = require('../../helpers/logger')
9
10const Video = mongoose.model('Video')
11
12const reqValidatorsVideos = {
13 videosAdd: videosAdd,
14 videosGet: videosGet,
15 videosRemove: videosRemove,
16 videosSearch: videosSearch
17}
18
19function videosAdd (req, res, next) {
20 req.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty()
21 req.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i)
22 req.checkBody('name', 'Should have a valid name').isVideoNameValid()
23 req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
24 req.checkBody('tags', 'Should have correct tags').isVideoTagsValid()
25
26 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
27
28 checkErrors(req, res, function () {
29 const videoFile = req.files.videofile[0]
30
31 Video.getDurationFromFile(videoFile.path, function (err, duration) {
32 if (err) {
33 return res.status(400).send('Cannot retrieve metadata of the file.')
34 }
35
36 if (!customValidators.isVideoDurationValid(duration)) {
37 return res.status(400).send('Duration of the video file is too big (max: ' + constants.VIDEOS_CONSTRAINTS_FIELDS.DURATION.max + 's).')
38 }
39
40 videoFile.duration = duration
41 next()
42 })
43 })
44}
45
46function videosGet (req, res, next) {
47 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
48
49 logger.debug('Checking videosGet parameters', { parameters: req.params })
50
51 checkErrors(req, res, function () {
52 Video.load(req.params.id, function (err, video) {
53 if (err) {
54 logger.error('Error in videosGet request validator.', { error: err })
55 return res.sendStatus(500)
56 }
57
58 if (!video) return res.status(404).send('Video not found')
59
60 next()
61 })
62 })
63}
64
65function videosRemove (req, res, next) {
66 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
67
68 logger.debug('Checking videosRemove parameters', { parameters: req.params })
69
70 checkErrors(req, res, function () {
71 Video.load(req.params.id, function (err, video) {
72 if (err) {
73 logger.error('Error in videosRemove request validator.', { error: err })
74 return res.sendStatus(500)
75 }
76
77 if (!video) return res.status(404).send('Video not found')
78 else if (video.isOwned() === false) return res.status(403).send('Cannot remove video of another pod')
79
80 next()
81 })
82 })
83}
84
85function videosSearch (req, res, next) {
86 const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS
87 req.checkParams('value', 'Should have a valid search').notEmpty()
88 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
89
90 logger.debug('Checking videosSearch parameters', { parameters: req.params })
91
92 checkErrors(req, res, next)
93}
94
95// ---------------------------------------------------------------------------
96
97module.exports = reqValidatorsVideos