From fc51fde048f2c3ce1dd3e85f5528335040bae894 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 1 Jul 2016 16:16:40 +0200 Subject: reqValidators --> validators --- server/middlewares/validators/index.js | 19 ++++++ server/middlewares/validators/pagination.js | 21 +++++++ server/middlewares/validators/pods.js | 41 ++++++++++++ server/middlewares/validators/remote.js | 41 ++++++++++++ server/middlewares/validators/sort.js | 23 +++++++ server/middlewares/validators/utils.js | 25 ++++++++ server/middlewares/validators/videos.js | 97 +++++++++++++++++++++++++++++ 7 files changed, 267 insertions(+) create mode 100644 server/middlewares/validators/index.js create mode 100644 server/middlewares/validators/pagination.js create mode 100644 server/middlewares/validators/pods.js create mode 100644 server/middlewares/validators/remote.js create mode 100644 server/middlewares/validators/sort.js create mode 100644 server/middlewares/validators/utils.js create mode 100644 server/middlewares/validators/videos.js (limited to 'server/middlewares/validators') diff --git a/server/middlewares/validators/index.js b/server/middlewares/validators/index.js new file mode 100644 index 000000000..0471b3f92 --- /dev/null +++ b/server/middlewares/validators/index.js @@ -0,0 +1,19 @@ +'use strict' + +const paginationValidators = require('./pagination') +const podsValidators = require('./pods') +const remoteValidators = require('./remote') +const sortValidators = require('./sort') +const videosValidators = require('./videos') + +const validators = { + pagination: paginationValidators, + pods: podsValidators, + remote: remoteValidators, + sort: sortValidators, + videos: videosValidators +} + +// --------------------------------------------------------------------------- + +module.exports = validators diff --git a/server/middlewares/validators/pagination.js b/server/middlewares/validators/pagination.js new file mode 100644 index 000000000..8e9a01053 --- /dev/null +++ b/server/middlewares/validators/pagination.js @@ -0,0 +1,21 @@ +'use strict' + +const checkErrors = require('./utils').checkErrors +const logger = require('../../helpers/logger') + +const validatorsPagination = { + pagination: pagination +} + +function pagination (req, res, next) { + req.checkQuery('start', 'Should have a number start').optional().isInt() + req.checkQuery('count', 'Should have a number count').optional().isInt() + + logger.debug('Checking pagination parameters', { parameters: req.query }) + + checkErrors(req, res, next) +} + +// --------------------------------------------------------------------------- + +module.exports = validatorsPagination diff --git a/server/middlewares/validators/pods.js b/server/middlewares/validators/pods.js new file mode 100644 index 000000000..fda2e865f --- /dev/null +++ b/server/middlewares/validators/pods.js @@ -0,0 +1,41 @@ +'use strict' + +const checkErrors = require('./utils').checkErrors +const friends = require('../../lib/friends') +const logger = require('../../helpers/logger') + +const validatorsPod = { + makeFriends: makeFriends, + podsAdd: podsAdd +} + +function makeFriends (req, res, next) { + friends.hasFriends(function (err, hasFriends) { + if (err) { + logger.error('Cannot know if we have friends.', { error: err }) + res.sendStatus(500) + } + + if (hasFriends === true) { + // We need to quit our friends before make new ones + res.sendStatus(409) + } else { + return next() + } + }) +} + +function podsAdd (req, res, next) { + req.checkBody('url', 'Should have an url').notEmpty().isURL({ require_protocol: true }) + req.checkBody('publicKey', 'Should have a public key').notEmpty() + + // TODO: check we don't have it already + + logger.debug('Checking podsAdd parameters', { parameters: req.body }) + + checkErrors(req, res, next) +} + +// --------------------------------------------------------------------------- + +module.exports = validatorsPod diff --git a/server/middlewares/validators/remote.js b/server/middlewares/validators/remote.js new file mode 100644 index 000000000..1be119458 --- /dev/null +++ b/server/middlewares/validators/remote.js @@ -0,0 +1,41 @@ +'use strict' + +const checkErrors = require('./utils').checkErrors +const logger = require('../../helpers/logger') + +const validatorsRemote = { + dataToDecrypt: dataToDecrypt, + remoteVideos: remoteVideos, + signature: signature +} + +function dataToDecrypt (req, res, next) { + req.checkBody('key', 'Should have a key').notEmpty() + req.checkBody('data', 'Should have data').notEmpty() + + logger.debug('Checking dataToDecrypt parameters', { parameters: { keyLength: req.body.key.length, bodyLength: req.body.data.length } }) + + checkErrors(req, res, next) +} + +function remoteVideos (req, res, next) { + req.checkBody('data').isArray() + req.checkBody('data').isEachRemoteVideosValid() + + logger.debug('Checking remoteVideos parameters', { parameters: req.body }) + + checkErrors(req, res, next) +} + +function signature (req, res, next) { + req.checkBody('signature.url', 'Should have a signature url').isURL() + req.checkBody('signature.signature', 'Should have a signature').notEmpty() + + logger.debug('Checking signature parameters', { parameters: { signatureUrl: req.body.signature.url } }) + + checkErrors(req, res, next) +} + +// --------------------------------------------------------------------------- + +module.exports = validatorsRemote diff --git a/server/middlewares/validators/sort.js b/server/middlewares/validators/sort.js new file mode 100644 index 000000000..56b63cc8b --- /dev/null +++ b/server/middlewares/validators/sort.js @@ -0,0 +1,23 @@ +'use strict' + +const checkErrors = require('./utils').checkErrors +const constants = require('../../initializers/constants') +const logger = require('../../helpers/logger') + +const validatorsSort = { + videosSort: videosSort +} + +function videosSort (req, res, next) { + const sortableColumns = constants.SORTABLE_COLUMNS.VIDEOS + + req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns) + + logger.debug('Checking sort parameters', { parameters: req.query }) + + checkErrors(req, res, next) +} + +// --------------------------------------------------------------------------- + +module.exports = validatorsSort diff --git a/server/middlewares/validators/utils.js b/server/middlewares/validators/utils.js new file mode 100644 index 000000000..f6e5b2b38 --- /dev/null +++ b/server/middlewares/validators/utils.js @@ -0,0 +1,25 @@ +'use strict' + +const util = require('util') + +const logger = require('../../helpers/logger') + +const validatorsUtils = { + checkErrors: checkErrors +} + +function checkErrors (req, res, next, statusCode) { + if (statusCode === undefined) statusCode = 400 + const errors = req.validationErrors() + + if (errors) { + logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors }) + return res.status(statusCode).send('There have been validation errors: ' + util.inspect(errors)) + } + + return next() +} + +// --------------------------------------------------------------------------- + +module.exports = validatorsUtils diff --git a/server/middlewares/validators/videos.js b/server/middlewares/validators/videos.js new file mode 100644 index 000000000..24e2299dc --- /dev/null +++ b/server/middlewares/validators/videos.js @@ -0,0 +1,97 @@ +'use strict' + +const mongoose = require('mongoose') + +const checkErrors = require('./utils').checkErrors +const constants = require('../../initializers/constants') +const customValidators = require('../../helpers/customValidators') +const logger = require('../../helpers/logger') + +const Video = mongoose.model('Video') + +const validatorsVideos = { + videosAdd: videosAdd, + videosGet: videosGet, + videosRemove: videosRemove, + videosSearch: videosSearch +} + +function videosAdd (req, res, next) { + req.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty() + req.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i) + req.checkBody('name', 'Should have a valid name').isVideoNameValid() + req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid() + req.checkBody('tags', 'Should have correct tags').isVideoTagsValid() + + logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) + + checkErrors(req, res, function () { + const videoFile = req.files.videofile[0] + + Video.getDurationFromFile(videoFile.path, function (err, duration) { + if (err) { + return res.status(400).send('Cannot retrieve metadata of the file.') + } + + if (!customValidators.isVideoDurationValid(duration)) { + return res.status(400).send('Duration of the video file is too big (max: ' + constants.VIDEOS_CONSTRAINTS_FIELDS.DURATION.max + 's).') + } + + videoFile.duration = duration + next() + }) + }) +} + +function videosGet (req, res, next) { + req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() + + logger.debug('Checking videosGet parameters', { parameters: req.params }) + + checkErrors(req, res, function () { + Video.load(req.params.id, function (err, video) { + if (err) { + logger.error('Error in videosGet request validator.', { error: err }) + return res.sendStatus(500) + } + + if (!video) return res.status(404).send('Video not found') + + next() + }) + }) +} + +function videosRemove (req, res, next) { + req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() + + logger.debug('Checking videosRemove parameters', { parameters: req.params }) + + checkErrors(req, res, function () { + Video.load(req.params.id, function (err, video) { + if (err) { + logger.error('Error in videosRemove request validator.', { error: err }) + return res.sendStatus(500) + } + + if (!video) return res.status(404).send('Video not found') + else if (video.isOwned() === false) return res.status(403).send('Cannot remove video of another pod') + + next() + }) + }) +} + +function videosSearch (req, res, next) { + const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS + req.checkParams('value', 'Should have a valid search').notEmpty() + req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns) + + logger.debug('Checking videosSearch parameters', { parameters: req.params }) + + checkErrors(req, res, next) +} + +// --------------------------------------------------------------------------- + +module.exports = validatorsVideos -- cgit v1.2.3