diff options
Diffstat (limited to 'server/middlewares')
-rw-r--r-- | server/middlewares/index.js | 2 | ||||
-rw-r--r-- | server/middlewares/reqValidators/index.js | 2 | ||||
-rw-r--r-- | server/middlewares/reqValidators/pagination.js | 6 | ||||
-rw-r--r-- | server/middlewares/reqValidators/sort.js | 23 | ||||
-rw-r--r-- | server/middlewares/sort.js | 15 |
5 files changed, 45 insertions, 3 deletions
diff --git a/server/middlewares/index.js b/server/middlewares/index.js index f0fad3418..35858da2c 100644 --- a/server/middlewares/index.js +++ b/server/middlewares/index.js | |||
@@ -3,12 +3,14 @@ | |||
3 | const oauth2 = require('./oauth2') | 3 | const oauth2 = require('./oauth2') |
4 | const pagination = require('./pagination') | 4 | const pagination = require('./pagination') |
5 | const reqValidatorsMiddleware = require('./reqValidators') | 5 | const reqValidatorsMiddleware = require('./reqValidators') |
6 | const sort = require('./sort') | ||
6 | const secureMiddleware = require('./secure') | 7 | const secureMiddleware = require('./secure') |
7 | 8 | ||
8 | const middlewares = { | 9 | const middlewares = { |
9 | oauth2: oauth2, | 10 | oauth2: oauth2, |
10 | pagination: pagination, | 11 | pagination: pagination, |
11 | reqValidators: reqValidatorsMiddleware, | 12 | reqValidators: reqValidatorsMiddleware, |
13 | sort: sort, | ||
12 | secure: secureMiddleware | 14 | secure: secureMiddleware |
13 | } | 15 | } |
14 | 16 | ||
diff --git a/server/middlewares/reqValidators/index.js b/server/middlewares/reqValidators/index.js index b732a27b6..be68f6a29 100644 --- a/server/middlewares/reqValidators/index.js +++ b/server/middlewares/reqValidators/index.js | |||
@@ -3,12 +3,14 @@ | |||
3 | const paginationReqValidators = require('./pagination') | 3 | const paginationReqValidators = require('./pagination') |
4 | const podsReqValidators = require('./pods') | 4 | const podsReqValidators = require('./pods') |
5 | const remoteReqValidators = require('./remote') | 5 | const remoteReqValidators = require('./remote') |
6 | const sortReqValidators = require('./sort') | ||
6 | const videosReqValidators = require('./videos') | 7 | const videosReqValidators = require('./videos') |
7 | 8 | ||
8 | const reqValidators = { | 9 | const reqValidators = { |
9 | pagination: paginationReqValidators, | 10 | pagination: paginationReqValidators, |
10 | pods: podsReqValidators, | 11 | pods: podsReqValidators, |
11 | remote: remoteReqValidators, | 12 | remote: remoteReqValidators, |
13 | sort: sortReqValidators, | ||
12 | videos: videosReqValidators | 14 | videos: videosReqValidators |
13 | } | 15 | } |
14 | 16 | ||
diff --git a/server/middlewares/reqValidators/pagination.js b/server/middlewares/reqValidators/pagination.js index ca8375396..e598f269a 100644 --- a/server/middlewares/reqValidators/pagination.js +++ b/server/middlewares/reqValidators/pagination.js | |||
@@ -8,10 +8,10 @@ const reqValidatorsPagination = { | |||
8 | } | 8 | } |
9 | 9 | ||
10 | function pagination (req, res, next) { | 10 | function pagination (req, res, next) { |
11 | req.checkParams('start', 'Should have a number start').optional().isInt() | 11 | req.checkQuery('start', 'Should have a number start').optional().isInt() |
12 | req.checkParams('count', 'Should have a number count').optional().isInt() | 12 | req.checkQuery('count', 'Should have a number count').optional().isInt() |
13 | 13 | ||
14 | logger.debug('Checking pagination parameters', { parameters: req.params }) | 14 | logger.debug('Checking pagination parameters', { parameters: req.query }) |
15 | 15 | ||
16 | checkErrors(req, res, next) | 16 | checkErrors(req, res, next) |
17 | } | 17 | } |
diff --git a/server/middlewares/reqValidators/sort.js b/server/middlewares/reqValidators/sort.js new file mode 100644 index 000000000..06e680ef4 --- /dev/null +++ b/server/middlewares/reqValidators/sort.js | |||
@@ -0,0 +1,23 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const checkErrors = require('./utils').checkErrors | ||
4 | const constants = require('../../initializers/constants') | ||
5 | const logger = require('../../helpers/logger') | ||
6 | |||
7 | const reqValidatorsSort = { | ||
8 | videosSort: videosSort | ||
9 | } | ||
10 | |||
11 | function 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 | |||
23 | module.exports = reqValidatorsSort | ||
diff --git a/server/middlewares/sort.js b/server/middlewares/sort.js new file mode 100644 index 000000000..9f52290a6 --- /dev/null +++ b/server/middlewares/sort.js | |||
@@ -0,0 +1,15 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const sortMiddleware = { | ||
4 | setVideosSort: setVideosSort | ||
5 | } | ||
6 | |||
7 | function setVideosSort (req, res, next) { | ||
8 | if (!req.query.sort) req.query.sort = '-createdDate' | ||
9 | |||
10 | return next() | ||
11 | } | ||
12 | |||
13 | // --------------------------------------------------------------------------- | ||
14 | |||
15 | module.exports = sortMiddleware | ||