From 9f10b2928df655c3672d9607e864e667d4bc903a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 7 Feb 2016 11:23:23 +0100 Subject: Remove useless anonymous functions of files --- controllers/api/v1/index.js | 18 ++- controllers/api/v1/pods.js | 136 +++++++++++----------- controllers/api/v1/remoteVideos.js | 82 +++++++------ controllers/api/v1/videos.js | 232 ++++++++++++++++++------------------- 4 files changed, 230 insertions(+), 238 deletions(-) (limited to 'controllers/api') diff --git a/controllers/api/v1/index.js b/controllers/api/v1/index.js index b16eeb0f6..fc6bf4946 100644 --- a/controllers/api/v1/index.js +++ b/controllers/api/v1/index.js @@ -1,15 +1,13 @@ -;(function () { - 'use strict' +'use strict' - var express = require('express') +var express = require('express') - var router = express.Router() +var router = express.Router() - router.use('/pods', require('./pods')) - router.use('/remotevideos', require('./remoteVideos')) - router.use('/videos', require('./videos')) +router.use('/pods', require('./pods')) +router.use('/remotevideos', require('./remoteVideos')) +router.use('/videos', require('./videos')) - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - module.exports = router -})() +module.exports = router diff --git a/controllers/api/v1/pods.js b/controllers/api/v1/pods.js index 456f53dea..c93a86ee8 100644 --- a/controllers/api/v1/pods.js +++ b/controllers/api/v1/pods.js @@ -1,95 +1,93 @@ -;(function () { - 'use strict' +'use strict' - var express = require('express') - var fs = require('fs') +var express = require('express') +var fs = require('fs') - var logger = require('../../../helpers/logger') - var friends = require('../../../lib/friends') - var middleware = require('../../../middlewares') - var cacheMiddleware = middleware.cache - var peertubeCrypto = require('../../../helpers/peertubeCrypto') - var Pods = require('../../../models/pods') - var reqValidator = middleware.reqValidators.pods - var secureMiddleware = middleware.secure - var secureRequest = middleware.reqValidators.remote.secureRequest - var Videos = require('../../../models/videos') +var logger = require('../../../helpers/logger') +var friends = require('../../../lib/friends') +var middleware = require('../../../middlewares') +var cacheMiddleware = middleware.cache +var peertubeCrypto = require('../../../helpers/peertubeCrypto') +var Pods = require('../../../models/pods') +var reqValidator = middleware.reqValidators.pods +var secureMiddleware = middleware.secure +var secureRequest = middleware.reqValidators.remote.secureRequest +var Videos = require('../../../models/videos') - var router = express.Router() +var router = express.Router() - router.get('/', cacheMiddleware.cache(false), listPods) - router.post('/', reqValidator.podsAdd, cacheMiddleware.cache(false), addPods) - router.get('/makefriends', reqValidator.makeFriends, cacheMiddleware.cache(false), makeFriends) - router.get('/quitfriends', cacheMiddleware.cache(false), quitFriends) - // Post because this is a secured request - router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods) +router.get('/', cacheMiddleware.cache(false), listPods) +router.post('/', reqValidator.podsAdd, cacheMiddleware.cache(false), addPods) +router.get('/makefriends', reqValidator.makeFriends, cacheMiddleware.cache(false), makeFriends) +router.get('/quitfriends', cacheMiddleware.cache(false), quitFriends) +// Post because this is a secured request +router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods) - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - module.exports = router +module.exports = router - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - function addPods (req, res, next) { - var informations = req.body.data - Pods.add(informations, function (err) { - if (err) return next(err) +function addPods (req, res, next) { + var informations = req.body.data + Pods.add(informations, function (err) { + if (err) return next(err) - Videos.addRemotes(informations.videos) + Videos.addRemotes(informations.videos) - fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { + fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { + if (err) { + logger.error('Cannot read cert file.') + return next(err) + } + + Videos.listOwned(function (err, videos_list) { if (err) { - logger.error('Cannot read cert file.') + logger.error('Cannot get the list of owned videos.') return next(err) } - Videos.listOwned(function (err, videos_list) { - if (err) { - logger.error('Cannot get the list of owned videos.') - return next(err) - } - - res.json({ cert: cert, videos: videos_list }) - }) + res.json({ cert: cert, videos: videos_list }) }) }) - } + }) +} - function listPods (req, res, next) { - Pods.list(function (err, pods_list) { - if (err) return next(err) +function listPods (req, res, next) { + Pods.list(function (err, pods_list) { + if (err) return next(err) - res.json(pods_list) - }) - } + res.json(pods_list) + }) +} - function makeFriends (req, res, next) { - friends.makeFriends(function (err) { - if (err) return next(err) +function makeFriends (req, res, next) { + friends.makeFriends(function (err) { + if (err) return next(err) - res.sendStatus(204) - }) - } + res.sendStatus(204) + }) +} - function removePods (req, res, next) { - var url = req.body.signature.url - Pods.remove(url, function (err) { - if (err) return next(err) +function removePods (req, res, next) { + var url = req.body.signature.url + Pods.remove(url, function (err) { + if (err) return next(err) - Videos.removeAllRemotesOf(url, function (err) { - if (err) logger.error('Cannot remove all remote videos of %s.', url) - else logger.info('%s pod removed.', url) + Videos.removeAllRemotesOf(url, function (err) { + if (err) logger.error('Cannot remove all remote videos of %s.', url) + else logger.info('%s pod removed.', url) - res.sendStatus(204) - }) + res.sendStatus(204) }) - } + }) +} - function quitFriends (req, res, next) { - friends.quitFriends(function (err) { - if (err) return next(err) +function quitFriends (req, res, next) { + friends.quitFriends(function (err) { + if (err) return next(err) - res.sendStatus(204) - }) - } -})() + res.sendStatus(204) + }) +} diff --git a/controllers/api/v1/remoteVideos.js b/controllers/api/v1/remoteVideos.js index 58bb5f3cb..475a874cf 100644 --- a/controllers/api/v1/remoteVideos.js +++ b/controllers/api/v1/remoteVideos.js @@ -1,55 +1,53 @@ -;(function () { - 'use strict' +'use strict' - var express = require('express') - var pluck = require('lodash-node/compat/collection/pluck') +var express = require('express') +var pluck = require('lodash-node/compat/collection/pluck') - var middleware = require('../../../middlewares') - var secureMiddleware = middleware.secure - var cacheMiddleware = middleware.cache - var reqValidator = middleware.reqValidators.remote - var videos = require('../../../models/videos') +var middleware = require('../../../middlewares') +var secureMiddleware = middleware.secure +var cacheMiddleware = middleware.cache +var reqValidator = middleware.reqValidators.remote +var videos = require('../../../models/videos') - var router = express.Router() +var router = express.Router() - router.post('/add', - reqValidator.secureRequest, - secureMiddleware.decryptBody, - reqValidator.remoteVideosAdd, - cacheMiddleware.cache(false), - addRemoteVideos - ) +router.post('/add', + reqValidator.secureRequest, + secureMiddleware.decryptBody, + reqValidator.remoteVideosAdd, + cacheMiddleware.cache(false), + addRemoteVideos +) - router.post('/remove', - reqValidator.secureRequest, - secureMiddleware.decryptBody, - reqValidator.remoteVideosRemove, - cacheMiddleware.cache(false), - removeRemoteVideo - ) +router.post('/remove', + reqValidator.secureRequest, + secureMiddleware.decryptBody, + reqValidator.remoteVideosRemove, + cacheMiddleware.cache(false), + removeRemoteVideo +) - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - module.exports = router +module.exports = router - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - function addRemoteVideos (req, res, next) { - videos.addRemotes(req.body.data, function (err, videos) { - if (err) return next(err) +function addRemoteVideos (req, res, next) { + videos.addRemotes(req.body.data, function (err, videos) { + if (err) return next(err) - res.json(videos) - }) - } + res.json(videos) + }) +} - function removeRemoteVideo (req, res, next) { - var url = req.body.signature.url - var magnetUris = pluck(req.body.data, 'magnetUri') +function removeRemoteVideo (req, res, next) { + var url = req.body.signature.url + var magnetUris = pluck(req.body.data, 'magnetUri') - videos.removeRemotesOfByMagnetUris(url, magnetUris, function (err) { - if (err) return next(err) + videos.removeRemotesOfByMagnetUris(url, magnetUris, function (err) { + if (err) return next(err) - res.sendStatus(204) - }) - } -})() + res.sendStatus(204) + }) +} diff --git a/controllers/api/v1/videos.js b/controllers/api/v1/videos.js index 7dccfa7c3..620711925 100644 --- a/controllers/api/v1/videos.js +++ b/controllers/api/v1/videos.js @@ -1,146 +1,144 @@ -;(function () { - 'use strict' - - var config = require('config') - var crypto = require('crypto') - var express = require('express') - var multer = require('multer') - - var logger = require('../../../helpers/logger') - var friends = require('../../../lib/friends') - var middleware = require('../../../middlewares') - var cacheMiddleware = middleware.cache - var reqValidator = middleware.reqValidators.videos - var Videos = require('../../../models/videos') // model - var videos = require('../../../lib/videos') - var webtorrent = require('../../../lib/webtorrent') - - var router = express.Router() - var uploads = config.get('storage.uploads') - - // multer configuration - var storage = multer.diskStorage({ - destination: function (req, file, cb) { - cb(null, uploads) - }, - - filename: function (req, file, cb) { - var extension = '' - if (file.mimetype === 'video/webm') extension = 'webm' - else if (file.mimetype === 'video/mp4') extension = 'mp4' - else if (file.mimetype === 'video/ogg') extension = 'ogv' - crypto.pseudoRandomBytes(16, function (err, raw) { - var fieldname = err ? undefined : raw.toString('hex') - cb(null, fieldname + '.' + extension) - }) - } - }) +'use strict' + +var config = require('config') +var crypto = require('crypto') +var express = require('express') +var multer = require('multer') + +var logger = require('../../../helpers/logger') +var friends = require('../../../lib/friends') +var middleware = require('../../../middlewares') +var cacheMiddleware = middleware.cache +var reqValidator = middleware.reqValidators.videos +var Videos = require('../../../models/videos') // model +var videos = require('../../../lib/videos') +var webtorrent = require('../../../lib/webtorrent') + +var router = express.Router() +var uploads = config.get('storage.uploads') + +// multer configuration +var storage = multer.diskStorage({ + destination: function (req, file, cb) { + cb(null, uploads) + }, + + filename: function (req, file, cb) { + var extension = '' + if (file.mimetype === 'video/webm') extension = 'webm' + else if (file.mimetype === 'video/mp4') extension = 'mp4' + else if (file.mimetype === 'video/ogg') extension = 'ogv' + crypto.pseudoRandomBytes(16, function (err, raw) { + var fieldname = err ? undefined : raw.toString('hex') + cb(null, fieldname + '.' + extension) + }) + } +}) + +var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }]) - var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }]) +router.get('/', cacheMiddleware.cache(false), listVideos) +router.post('/', reqFiles, reqValidator.videosAdd, cacheMiddleware.cache(false), addVideo) +router.get('/:id', reqValidator.videosGet, cacheMiddleware.cache(false), getVideos) +router.delete('/:id', reqValidator.videosRemove, cacheMiddleware.cache(false), removeVideo) +router.get('/search/:name', reqValidator.videosSearch, cacheMiddleware.cache(false), searchVideos) - router.get('/', cacheMiddleware.cache(false), listVideos) - router.post('/', reqFiles, reqValidator.videosAdd, cacheMiddleware.cache(false), addVideo) - router.get('/:id', reqValidator.videosGet, cacheMiddleware.cache(false), getVideos) - router.delete('/:id', reqValidator.videosRemove, cacheMiddleware.cache(false), removeVideo) - router.get('/search/:name', reqValidator.videosSearch, cacheMiddleware.cache(false), searchVideos) +// --------------------------------------------------------------------------- - // --------------------------------------------------------------------------- +module.exports = router - module.exports = router +// --------------------------------------------------------------------------- - // --------------------------------------------------------------------------- +function addVideo (req, res, next) { + var video_file = req.files.input_video[0] + var video_infos = req.body + + videos.seed(video_file.path, function (err, torrent) { + if (err) { + logger.error('Cannot seed this video.') + return next(err) + } - function addVideo (req, res, next) { - var video_file = req.files.input_video[0] - var video_infos = req.body + var video_data = { + name: video_infos.name, + namePath: video_file.filename, + description: video_infos.description, + magnetUri: torrent.magnetURI + } - videos.seed(video_file.path, function (err, torrent) { + Videos.add(video_data, function (err) { if (err) { - logger.error('Cannot seed this video.') + // TODO unseed the video + logger.error('Cannot insert this video in the database.') return next(err) } - var video_data = { - name: video_infos.name, - namePath: video_file.filename, - description: video_infos.description, - magnetUri: torrent.magnetURI - } - - Videos.add(video_data, function (err) { - if (err) { - // TODO unseed the video - logger.error('Cannot insert this video in the database.') - return next(err) - } - - // Now we'll add the video's meta data to our friends - friends.addVideoToFriends(video_data) + // Now we'll add the video's meta data to our friends + friends.addVideoToFriends(video_data) - // TODO : include Location of the new video - res.sendStatus(201) - }) + // TODO : include Location of the new video + res.sendStatus(201) }) - } + }) +} - function getVideos (req, res, next) { - Videos.get(req.params.id, function (err, video) { - if (err) return next(err) +function getVideos (req, res, next) { + Videos.get(req.params.id, function (err, video) { + if (err) return next(err) - if (video === null) { - return res.sendStatus(404) - } + if (video === null) { + return res.sendStatus(404) + } - res.json(video) - }) - } + res.json(video) + }) +} - function listVideos (req, res, next) { - Videos.list(function (err, videos_list) { - if (err) return next(err) +function listVideos (req, res, next) { + Videos.list(function (err, videos_list) { + if (err) return next(err) - res.json(videos_list) - }) - } + res.json(videos_list) + }) +} - function removeVideo (req, res, next) { - var video_id = req.params.id - Videos.get(video_id, function (err, video) { - if (err) return next(err) +function removeVideo (req, res, next) { + var video_id = req.params.id + Videos.get(video_id, function (err, video) { + if (err) return next(err) - removeTorrent(video.magnetUri, function () { - Videos.removeOwned(req.params.id, function (err) { - if (err) return next(err) + removeTorrent(video.magnetUri, function () { + Videos.removeOwned(req.params.id, function (err) { + if (err) return next(err) - var params = { - name: video.name, - magnetUri: video.magnetUri - } + var params = { + name: video.name, + magnetUri: video.magnetUri + } - friends.removeVideoToFriends(params) - res.sendStatus(204) - }) + friends.removeVideoToFriends(params) + res.sendStatus(204) }) }) - } + }) +} - function searchVideos (req, res, next) { - Videos.search(req.params.name, function (err, videos_list) { - if (err) return next(err) +function searchVideos (req, res, next) { + Videos.search(req.params.name, function (err, videos_list) { + if (err) return next(err) - res.json(videos_list) - }) - } + res.json(videos_list) + }) +} - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process - function removeTorrent (magnetUri, callback) { - try { - webtorrent.remove(magnetUri, callback) - } catch (err) { - logger.warn('Cannot remove the torrent from WebTorrent', { err: err }) - return callback(null) - } +// Maybe the torrent is not seeded, but we catch the error to don't stop the removing process +function removeTorrent (magnetUri, callback) { + try { + webtorrent.remove(magnetUri, callback) + } catch (err) { + logger.warn('Cannot remove the torrent from WebTorrent', { err: err }) + return callback(null) } -})() +} -- cgit v1.2.3