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/index.js | 10 +++++--- middlewares/misc.js | 19 +++++++++----- 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 ++++++++++++++++++++++--------------- 7 files changed, 87 insertions(+), 54 deletions(-) (limited to 'middlewares') diff --git a/middlewares/index.js b/middlewares/index.js index e727202ba..311dfb6d2 100644 --- a/middlewares/index.js +++ b/middlewares/index.js @@ -1,10 +1,12 @@ ;(function () { 'use strict' - var middleware = { - reqValidators: require('./reqValidators'), - misc: require('./misc') + var middlewares = { + misc: require('./misc'), + reqValidators: require('./reqValidators') } - module.exports = middleware + // --------------------------------------------------------------------------- + + module.exports = middlewares })() diff --git a/middlewares/misc.js b/middlewares/misc.js index f814acd9f..dbb604db3 100644 --- a/middlewares/misc.js +++ b/middlewares/misc.js @@ -1,16 +1,19 @@ ;(function () { 'use strict' - var ursa = require('ursa') var fs = require('fs') + var ursa = require('ursa') var logger = require('../helpers/logger') - var utils = require('../helpers/utils') var PodsDB = require('../initializers/database').PodsDB + var utils = require('../helpers/utils') - var misc = {} + var miscMiddleware = { + cache: cache, + decryptBody: decryptBody + } - misc.cache = function (cache) { + function cache (cache) { return function (req, res, next) { // If we want explicitly a cache // Or if we don't specify if we want a cache or no and we are in production @@ -24,7 +27,7 @@ } } - misc.decryptBody = function (req, res, next) { + function decryptBody (req, res, next) { PodsDB.findOne({ url: req.body.signature.url }, function (err, pod) { if (err) { logger.error('Cannot get signed url in decryptBody.', { error: err }) @@ -42,7 +45,7 @@ var signature_ok = crt.hashAndVerify('sha256', new Buffer(req.body.signature.url).toString('hex'), req.body.signature.signature, 'hex') if (signature_ok === true) { - var myKey = ursa.createPrivateKey(fs.readFileSync(utils.certDir + 'peertube.key.pem')) + var myKey = ursa.createPrivateKey(fs.readFileSync(utils.getCertDir() + 'peertube.key.pem')) var decryptedKey = myKey.decrypt(req.body.key, 'hex', 'utf8') req.body.data = JSON.parse(utils.symetricDecrypt(req.body.data, decryptedKey)) delete req.body.key @@ -55,5 +58,7 @@ }) } - module.exports = misc + // --------------------------------------------------------------------------- + + module.exports = miscMiddleware })() 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