From c45f7f84001c2731909db04dd82e1c1f290386eb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 31 Jan 2016 11:23:52 +0100 Subject: Infile code reorganization --- middlewares/reqValidators/index.js | 6 +++-- middlewares/reqValidators/pods.js | 10 +++++--- middlewares/reqValidators/remote.js | 34 +++++++++++++++---------- middlewares/reqValidators/utils.js | 11 +++++--- middlewares/reqValidators/videos.js | 51 ++++++++++++++++++++++--------------- 5 files changed, 69 insertions(+), 43 deletions(-) (limited to 'middlewares/reqValidators') diff --git a/middlewares/reqValidators/index.js b/middlewares/reqValidators/index.js index 1ea611031..34d34013c 100644 --- a/middlewares/reqValidators/index.js +++ b/middlewares/reqValidators/index.js @@ -1,11 +1,13 @@ ;(function () { 'use strict' - var reqValidator = { + var reqValidators = { videos: require('./videos'), pods: require('./pods'), remote: require('./remote') } - module.exports = reqValidator + // --------------------------------------------------------------------------- + + module.exports = reqValidators })() diff --git a/middlewares/reqValidators/pods.js b/middlewares/reqValidators/pods.js index 0d023842d..6ccfd7361 100644 --- a/middlewares/reqValidators/pods.js +++ b/middlewares/reqValidators/pods.js @@ -4,9 +4,11 @@ var checkErrors = require('./utils').checkErrors var logger = require('../../helpers/logger') - var pods = {} + var reqValidatorsPod = { + podsAdd: podsAdd + } - pods.podsAdd = function (req, res, next) { + function podsAdd (req, res, next) { req.checkBody('data.url', 'Should have an url').notEmpty().isURL({ require_protocol: true }) req.checkBody('data.publicKey', 'Should have a public key').notEmpty() @@ -15,5 +17,7 @@ checkErrors(req, res, next) } - module.exports = pods + // --------------------------------------------------------------------------- + + module.exports = reqValidatorsPod })() diff --git a/middlewares/reqValidators/remote.js b/middlewares/reqValidators/remote.js index 4b161e292..9b61481ad 100644 --- a/middlewares/reqValidators/remote.js +++ b/middlewares/reqValidators/remote.js @@ -4,20 +4,13 @@ var checkErrors = require('./utils').checkErrors var logger = require('../../helpers/logger') - var remote = {} - - remote.secureRequest = function (req, res, next) { - req.checkBody('signature.url', 'Should have a signature url').isURL() - req.checkBody('signature.signature', 'Should have a signature').notEmpty() - req.checkBody('key', 'Should have a key').notEmpty() - req.checkBody('data', 'Should have data').notEmpty() - - logger.debug('Checking secureRequest parameters', { parameters: { data: req.body.data, keyLength: req.body.key.length } }) - - checkErrors(req, res, next) + var reqValidatorsRemote = { + remoteVideosAdd: remoteVideosAdd, + remoteVideosRemove: remoteVideosRemove, + secureRequest: secureRequest } - remote.remoteVideosAdd = function (req, res, next) { + function remoteVideosAdd (req, res, next) { req.checkBody('data').isArray() req.checkBody('data').eachIsRemoteVideosAddValid() @@ -26,7 +19,7 @@ checkErrors(req, res, next) } - remote.remoteVideosRemove = function (req, res, next) { + function remoteVideosRemove (req, res, next) { req.checkBody('data').isArray() req.checkBody('data').eachIsRemoteVideosRemoveValid() @@ -35,5 +28,18 @@ checkErrors(req, res, next) } - module.exports = remote + function secureRequest (req, res, next) { + req.checkBody('signature.url', 'Should have a signature url').isURL() + req.checkBody('signature.signature', 'Should have a signature').notEmpty() + req.checkBody('key', 'Should have a key').notEmpty() + req.checkBody('data', 'Should have data').notEmpty() + + logger.debug('Checking secureRequest parameters', { parameters: { data: req.body.data, keyLength: req.body.key.length } }) + + checkErrors(req, res, next) + } + + // --------------------------------------------------------------------------- + + module.exports = reqValidatorsRemote })() diff --git a/middlewares/reqValidators/utils.js b/middlewares/reqValidators/utils.js index 5bc9f4f0b..c88f6df2e 100644 --- a/middlewares/reqValidators/utils.js +++ b/middlewares/reqValidators/utils.js @@ -2,11 +2,14 @@ 'use strict' var util = require('util') + var logger = require('../../helpers/logger') - var utils = {} + var reqValidatorsUtils = { + checkErrors: checkErrors + } - utils.checkErrors = function (req, res, next, status_code) { + function checkErrors (req, res, next, status_code) { if (status_code === undefined) status_code = 400 var errors = req.validationErrors() @@ -18,5 +21,7 @@ return next() } - module.exports = utils + // --------------------------------------------------------------------------- + + module.exports = reqValidatorsUtils })() diff --git a/middlewares/reqValidators/videos.js b/middlewares/reqValidators/videos.js index a34445f7a..3479c47c3 100644 --- a/middlewares/reqValidators/videos.js +++ b/middlewares/reqValidators/videos.js @@ -2,28 +2,17 @@ 'use strict' var checkErrors = require('./utils').checkErrors - var VideosDB = require('../../initializers/database').VideosDB var logger = require('../../helpers/logger') + var VideosDB = require('../../initializers/database').VideosDB - var videos = {} - - function findVideoById (id, callback) { - VideosDB.findById(id, { _id: 1, namePath: 1 }).limit(1).exec(function (err, video) { - if (err) throw err - - callback(video) - }) - } - - videos.videosSearch = function (req, res, next) { - req.checkParams('name', 'Should have a name').notEmpty() - - logger.debug('Checking videosSearch parameters', { parameters: req.params }) - - checkErrors(req, res, next) + var reqValidatorsVideos = { + videosAdd: videosAdd, + videosGet: videosGet, + videosRemove: videosRemove, + videosSearch: videosSearch } - videos.videosAdd = function (req, res, next) { + function videosAdd (req, res, next) { req.checkFiles('input_video[0].originalname', 'Should have an input video').notEmpty() req.checkFiles('input_video[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i) req.checkBody('name', 'Should have a name').isLength(1, 50) @@ -34,7 +23,7 @@ checkErrors(req, res, next) } - videos.videosGet = function (req, res, next) { + function videosGet (req, res, next) { req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() logger.debug('Checking videosGet parameters', { parameters: req.params }) @@ -48,7 +37,7 @@ }) } - videos.videosRemove = function (req, res, next) { + function videosRemove (req, res, next) { req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() logger.debug('Checking videosRemove parameters', { parameters: req.params }) @@ -63,5 +52,25 @@ }) } - module.exports = videos + function videosSearch (req, res, next) { + req.checkParams('name', 'Should have a name').notEmpty() + + logger.debug('Checking videosSearch parameters', { parameters: req.params }) + + checkErrors(req, res, next) + } + + // --------------------------------------------------------------------------- + + module.exports = reqValidatorsVideos + + // --------------------------------------------------------------------------- + + function findVideoById (id, callback) { + VideosDB.findById(id, { _id: 1, namePath: 1 }).limit(1).exec(function (err, video) { + if (err) throw err + + callback(video) + }) + } })() -- cgit v1.2.3