Remove useless anonymous functions of files

This commit is contained in:
Chocobozzz 2016-02-07 11:23:23 +01:00
parent d7c01e7793
commit 9f10b2928d
37 changed files with 3115 additions and 3189 deletions

View File

@ -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('/pods', require('./pods'))
router.use('/remotevideos', require('./remoteVideos')) router.use('/remotevideos', require('./remoteVideos'))
router.use('/videos', require('./videos')) router.use('/videos', require('./videos'))
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = router module.exports = router
})()

View File

@ -1,36 +1,35 @@
;(function () { 'use strict'
'use strict'
var express = require('express') var express = require('express')
var fs = require('fs') var fs = require('fs')
var logger = require('../../../helpers/logger') var logger = require('../../../helpers/logger')
var friends = require('../../../lib/friends') var friends = require('../../../lib/friends')
var middleware = require('../../../middlewares') var middleware = require('../../../middlewares')
var cacheMiddleware = middleware.cache var cacheMiddleware = middleware.cache
var peertubeCrypto = require('../../../helpers/peertubeCrypto') var peertubeCrypto = require('../../../helpers/peertubeCrypto')
var Pods = require('../../../models/pods') var Pods = require('../../../models/pods')
var reqValidator = middleware.reqValidators.pods var reqValidator = middleware.reqValidators.pods
var secureMiddleware = middleware.secure var secureMiddleware = middleware.secure
var secureRequest = middleware.reqValidators.remote.secureRequest var secureRequest = middleware.reqValidators.remote.secureRequest
var Videos = require('../../../models/videos') var Videos = require('../../../models/videos')
var router = express.Router() var router = express.Router()
router.get('/', cacheMiddleware.cache(false), listPods) router.get('/', cacheMiddleware.cache(false), listPods)
router.post('/', reqValidator.podsAdd, cacheMiddleware.cache(false), addPods) router.post('/', reqValidator.podsAdd, cacheMiddleware.cache(false), addPods)
router.get('/makefriends', reqValidator.makeFriends, cacheMiddleware.cache(false), makeFriends) router.get('/makefriends', reqValidator.makeFriends, cacheMiddleware.cache(false), makeFriends)
router.get('/quitfriends', cacheMiddleware.cache(false), quitFriends) router.get('/quitfriends', cacheMiddleware.cache(false), quitFriends)
// Post because this is a secured request // Post because this is a secured request
router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods) router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = router module.exports = router
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function addPods (req, res, next) { function addPods (req, res, next) {
var informations = req.body.data var informations = req.body.data
Pods.add(informations, function (err) { Pods.add(informations, function (err) {
if (err) return next(err) if (err) return next(err)
@ -53,25 +52,25 @@
}) })
}) })
}) })
} }
function listPods (req, res, next) { function listPods (req, res, next) {
Pods.list(function (err, pods_list) { Pods.list(function (err, pods_list) {
if (err) return next(err) if (err) return next(err)
res.json(pods_list) res.json(pods_list)
}) })
} }
function makeFriends (req, res, next) { function makeFriends (req, res, next) {
friends.makeFriends(function (err) { friends.makeFriends(function (err) {
if (err) return next(err) if (err) return next(err)
res.sendStatus(204) res.sendStatus(204)
}) })
} }
function removePods (req, res, next) { function removePods (req, res, next) {
var url = req.body.signature.url var url = req.body.signature.url
Pods.remove(url, function (err) { Pods.remove(url, function (err) {
if (err) return next(err) if (err) return next(err)
@ -83,13 +82,12 @@
res.sendStatus(204) res.sendStatus(204)
}) })
}) })
} }
function quitFriends (req, res, next) { function quitFriends (req, res, next) {
friends.quitFriends(function (err) { friends.quitFriends(function (err) {
if (err) return next(err) if (err) return next(err)
res.sendStatus(204) res.sendStatus(204)
}) })
} }
})()

View File

@ -1,48 +1,47 @@
;(function () { 'use strict'
'use strict'
var express = require('express') var express = require('express')
var pluck = require('lodash-node/compat/collection/pluck') var pluck = require('lodash-node/compat/collection/pluck')
var middleware = require('../../../middlewares') var middleware = require('../../../middlewares')
var secureMiddleware = middleware.secure var secureMiddleware = middleware.secure
var cacheMiddleware = middleware.cache var cacheMiddleware = middleware.cache
var reqValidator = middleware.reqValidators.remote var reqValidator = middleware.reqValidators.remote
var videos = require('../../../models/videos') var videos = require('../../../models/videos')
var router = express.Router() var router = express.Router()
router.post('/add', router.post('/add',
reqValidator.secureRequest, reqValidator.secureRequest,
secureMiddleware.decryptBody, secureMiddleware.decryptBody,
reqValidator.remoteVideosAdd, reqValidator.remoteVideosAdd,
cacheMiddleware.cache(false), cacheMiddleware.cache(false),
addRemoteVideos addRemoteVideos
) )
router.post('/remove', router.post('/remove',
reqValidator.secureRequest, reqValidator.secureRequest,
secureMiddleware.decryptBody, secureMiddleware.decryptBody,
reqValidator.remoteVideosRemove, reqValidator.remoteVideosRemove,
cacheMiddleware.cache(false), cacheMiddleware.cache(false),
removeRemoteVideo removeRemoteVideo
) )
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = router module.exports = router
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function addRemoteVideos (req, res, next) { function addRemoteVideos (req, res, next) {
videos.addRemotes(req.body.data, function (err, videos) { videos.addRemotes(req.body.data, function (err, videos) {
if (err) return next(err) if (err) return next(err)
res.json(videos) res.json(videos)
}) })
} }
function removeRemoteVideo (req, res, next) { function removeRemoteVideo (req, res, next) {
var url = req.body.signature.url var url = req.body.signature.url
var magnetUris = pluck(req.body.data, 'magnetUri') var magnetUris = pluck(req.body.data, 'magnetUri')
@ -51,5 +50,4 @@
res.sendStatus(204) res.sendStatus(204)
}) })
} }
})()

View File

@ -1,25 +1,24 @@
;(function () { 'use strict'
'use strict'
var config = require('config') var config = require('config')
var crypto = require('crypto') var crypto = require('crypto')
var express = require('express') var express = require('express')
var multer = require('multer') var multer = require('multer')
var logger = require('../../../helpers/logger') var logger = require('../../../helpers/logger')
var friends = require('../../../lib/friends') var friends = require('../../../lib/friends')
var middleware = require('../../../middlewares') var middleware = require('../../../middlewares')
var cacheMiddleware = middleware.cache var cacheMiddleware = middleware.cache
var reqValidator = middleware.reqValidators.videos var reqValidator = middleware.reqValidators.videos
var Videos = require('../../../models/videos') // model var Videos = require('../../../models/videos') // model
var videos = require('../../../lib/videos') var videos = require('../../../lib/videos')
var webtorrent = require('../../../lib/webtorrent') var webtorrent = require('../../../lib/webtorrent')
var router = express.Router() var router = express.Router()
var uploads = config.get('storage.uploads') var uploads = config.get('storage.uploads')
// multer configuration // multer configuration
var storage = multer.diskStorage({ var storage = multer.diskStorage({
destination: function (req, file, cb) { destination: function (req, file, cb) {
cb(null, uploads) cb(null, uploads)
}, },
@ -34,23 +33,23 @@
cb(null, fieldname + '.' + extension) 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.get('/', cacheMiddleware.cache(false), listVideos)
router.post('/', reqFiles, reqValidator.videosAdd, cacheMiddleware.cache(false), addVideo) router.post('/', reqFiles, reqValidator.videosAdd, cacheMiddleware.cache(false), addVideo)
router.get('/:id', reqValidator.videosGet, cacheMiddleware.cache(false), getVideos) router.get('/:id', reqValidator.videosGet, cacheMiddleware.cache(false), getVideos)
router.delete('/:id', reqValidator.videosRemove, cacheMiddleware.cache(false), removeVideo) router.delete('/:id', reqValidator.videosRemove, cacheMiddleware.cache(false), removeVideo)
router.get('/search/:name', reqValidator.videosSearch, cacheMiddleware.cache(false), searchVideos) router.get('/search/:name', reqValidator.videosSearch, cacheMiddleware.cache(false), searchVideos)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = router module.exports = router
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function addVideo (req, res, next) { function addVideo (req, res, next) {
var video_file = req.files.input_video[0] var video_file = req.files.input_video[0]
var video_infos = req.body var video_infos = req.body
@ -81,9 +80,9 @@
res.sendStatus(201) res.sendStatus(201)
}) })
}) })
} }
function getVideos (req, res, next) { function getVideos (req, res, next) {
Videos.get(req.params.id, function (err, video) { Videos.get(req.params.id, function (err, video) {
if (err) return next(err) if (err) return next(err)
@ -93,17 +92,17 @@
res.json(video) res.json(video)
}) })
} }
function listVideos (req, res, next) { function listVideos (req, res, next) {
Videos.list(function (err, videos_list) { Videos.list(function (err, videos_list) {
if (err) return next(err) if (err) return next(err)
res.json(videos_list) res.json(videos_list)
}) })
} }
function removeVideo (req, res, next) { function removeVideo (req, res, next) {
var video_id = req.params.id var video_id = req.params.id
Videos.get(video_id, function (err, video) { Videos.get(video_id, function (err, video) {
if (err) return next(err) if (err) return next(err)
@ -122,25 +121,24 @@
}) })
}) })
}) })
} }
function searchVideos (req, res, next) { function searchVideos (req, res, next) {
Videos.search(req.params.name, function (err, videos_list) { Videos.search(req.params.name, function (err, videos_list) {
if (err) return next(err) 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 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
function removeTorrent (magnetUri, callback) { function removeTorrent (magnetUri, callback) {
try { try {
webtorrent.remove(magnetUri, callback) webtorrent.remove(magnetUri, callback)
} catch (err) { } catch (err) {
logger.warn('Cannot remove the torrent from WebTorrent', { err: err }) logger.warn('Cannot remove the torrent from WebTorrent', { err: err })
return callback(null) return callback(null)
} }
} }
})()

View File

@ -1,10 +1,8 @@
;(function () { 'use strict'
'use strict'
var constants = require('../initializers/constants') var constants = require('../initializers/constants')
module.exports = { module.exports = {
api: require('./api/' + constants.API_VERSION), api: require('./api/' + constants.API_VERSION),
views: require('./views') views: require('./views')
} }
})()

View File

@ -1,29 +1,27 @@
;(function () { 'use strict'
'use strict'
var express = require('express') var express = require('express')
var cacheMiddleware = require('../middlewares').cache var cacheMiddleware = require('../middlewares').cache
var router = express.Router() var router = express.Router()
router.get(/^\/(index)?$/, cacheMiddleware.cache(), getIndex) router.get(/^\/(index)?$/, cacheMiddleware.cache(), getIndex)
router.get('/partials/:directory/:name', cacheMiddleware.cache(), getPartial) router.get('/partials/:directory/:name', cacheMiddleware.cache(), getPartial)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = router module.exports = router
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function getIndex (req, res) { function getIndex (req, res) {
res.render('index') res.render('index')
} }
function getPartial (req, res) { function getPartial (req, res) {
var directory = req.params.directory var directory = req.params.directory
var name = req.params.name var name = req.params.name
res.render('partials/' + directory + '/' + name) res.render('partials/' + directory + '/' + name)
} }
})()

View File

@ -1,34 +1,32 @@
;(function () { 'use strict'
'use strict'
var validator = require('validator') var validator = require('validator')
var customValidators = { var customValidators = {
eachIsRemoteVideosAddValid: eachIsRemoteVideosAddValid, eachIsRemoteVideosAddValid: eachIsRemoteVideosAddValid,
eachIsRemoteVideosRemoveValid: eachIsRemoteVideosRemoveValid, eachIsRemoteVideosRemoveValid: eachIsRemoteVideosRemoveValid,
isArray: isArray isArray: isArray
} }
function eachIsRemoteVideosAddValid (values) { function eachIsRemoteVideosAddValid (values) {
return values.every(function (val) { return values.every(function (val) {
return validator.isLength(val.name, 1, 50) && return validator.isLength(val.name, 1, 50) &&
validator.isLength(val.description, 1, 50) && validator.isLength(val.description, 1, 50) &&
validator.isLength(val.magnetUri, 10) && validator.isLength(val.magnetUri, 10) &&
validator.isURL(val.podUrl) validator.isURL(val.podUrl)
}) })
} }
function eachIsRemoteVideosRemoveValid (values) { function eachIsRemoteVideosRemoveValid (values) {
return values.every(function (val) { return values.every(function (val) {
return validator.isLength(val.magnetUri, 10) return validator.isLength(val.magnetUri, 10)
}) })
} }
function isArray (value) { function isArray (value) {
return Array.isArray(value) return Array.isArray(value)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = customValidators module.exports = customValidators
})()

View File

@ -1,14 +1,13 @@
;(function () { // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
// Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ 'use strict'
'use strict'
var config = require('config') var config = require('config')
var path = require('path') var path = require('path')
var winston = require('winston') var winston = require('winston')
winston.emitErrs = true winston.emitErrs = true
var logDir = path.join(__dirname, '..', config.get('storage.logs')) var logDir = path.join(__dirname, '..', config.get('storage.logs'))
var logger = new winston.Logger({ var logger = new winston.Logger({
transports: [ transports: [
new winston.transports.File({ new winston.transports.File({
level: 'debug', level: 'debug',
@ -28,15 +27,14 @@
}) })
], ],
exitOnError: true exitOnError: true
}) })
logger.stream = { logger.stream = {
write: function (message, encoding) { write: function (message, encoding) {
logger.info(message) logger.info(message)
} }
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = logger module.exports = logger
})()

View File

@ -1,34 +1,33 @@
;(function () { 'use strict'
'use strict'
var config = require('config') var config = require('config')
var crypto = require('crypto') var crypto = require('crypto')
var fs = require('fs') var fs = require('fs')
var openssl = require('openssl-wrapper') var openssl = require('openssl-wrapper')
var path = require('path') var path = require('path')
var ursa = require('ursa') var ursa = require('ursa')
var logger = require('./logger') var logger = require('./logger')
var certDir = path.join(__dirname, '..', config.get('storage.certs')) var certDir = path.join(__dirname, '..', config.get('storage.certs'))
var algorithm = 'aes-256-ctr' var algorithm = 'aes-256-ctr'
var peertubeCrypto = { var peertubeCrypto = {
checkSignature: checkSignature, checkSignature: checkSignature,
createCertsIfNotExist: createCertsIfNotExist, createCertsIfNotExist: createCertsIfNotExist,
decrypt: decrypt, decrypt: decrypt,
encrypt: encrypt, encrypt: encrypt,
getCertDir: getCertDir, getCertDir: getCertDir,
sign: sign sign: sign
} }
function checkSignature (public_key, raw_data, hex_signature) { function checkSignature (public_key, raw_data, hex_signature) {
var crt = ursa.createPublicKey(public_key) var crt = ursa.createPublicKey(public_key)
var is_valid = crt.hashAndVerify('sha256', new Buffer(raw_data).toString('hex'), hex_signature, 'hex') var is_valid = crt.hashAndVerify('sha256', new Buffer(raw_data).toString('hex'), hex_signature, 'hex')
return is_valid return is_valid
} }
function createCertsIfNotExist (callback) { function createCertsIfNotExist (callback) {
certsExist(function (exist) { certsExist(function (exist) {
if (exist === true) { if (exist === true) {
return callback(null) return callback(null)
@ -38,9 +37,9 @@
return callback(err) return callback(err)
}) })
}) })
} }
function decrypt (key, data, callback) { function decrypt (key, data, callback) {
fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) { fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) {
if (err) return callback(err) if (err) return callback(err)
@ -50,9 +49,9 @@
return callback(null, decrypted_data) return callback(null, decrypted_data)
}) })
} }
function encrypt (public_key, data, callback) { function encrypt (public_key, data, callback) {
var crt = ursa.createPublicKey(public_key) var crt = ursa.createPublicKey(public_key)
symetricEncrypt(data, function (err, dataEncrypted) { symetricEncrypt(data, function (err, dataEncrypted) {
@ -66,32 +65,32 @@
callback(null, encrypted) callback(null, encrypted)
}) })
} }
function getCertDir () { function getCertDir () {
return certDir return certDir
} }
function sign (data) { function sign (data) {
var myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) var myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem'))
var signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') var signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')
return signature return signature
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = peertubeCrypto module.exports = peertubeCrypto
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function certsExist (callback) { function certsExist (callback) {
fs.exists(certDir + 'peertube.key.pem', function (exists) { fs.exists(certDir + 'peertube.key.pem', function (exists) {
return callback(exists) return callback(exists)
}) })
} }
function createCerts (callback) { function createCerts (callback) {
certsExist(function (exist) { certsExist(function (exist) {
if (exist === true) { if (exist === true) {
var string = 'Certs already exist.' var string = 'Certs already exist.'
@ -119,24 +118,24 @@
}) })
}) })
}) })
} }
function generatePassword (callback) { function generatePassword (callback) {
crypto.randomBytes(32, function (err, buf) { crypto.randomBytes(32, function (err, buf) {
if (err) return callback(err) if (err) return callback(err)
callback(null, buf.toString('utf8')) callback(null, buf.toString('utf8'))
}) })
} }
function symetricDecrypt (text, password) { function symetricDecrypt (text, password) {
var decipher = crypto.createDecipher(algorithm, password) var decipher = crypto.createDecipher(algorithm, password)
var dec = decipher.update(text, 'hex', 'utf8') var dec = decipher.update(text, 'hex', 'utf8')
dec += decipher.final('utf8') dec += decipher.final('utf8')
return dec return dec
} }
function symetricEncrypt (text, callback) { function symetricEncrypt (text, callback) {
generatePassword(function (err, password) { generatePassword(function (err, password) {
if (err) return callback(err) if (err) return callback(err)
@ -145,5 +144,4 @@
crypted += cipher.final('hex') crypted += cipher.final('hex')
callback(null, { crypted: crypted, password: password }) callback(null, { crypted: crypted, password: password })
}) })
} }
})()

View File

@ -1,24 +1,23 @@
;(function () { 'use strict'
'use strict'
var async = require('async') var async = require('async')
var config = require('config') var config = require('config')
var request = require('request') var request = require('request')
var replay = require('request-replay') var replay = require('request-replay')
var constants = require('../initializers/constants') var constants = require('../initializers/constants')
var logger = require('./logger') var logger = require('./logger')
var peertubeCrypto = require('./peertubeCrypto') var peertubeCrypto = require('./peertubeCrypto')
var http = config.get('webserver.https') ? 'https' : 'http' var http = config.get('webserver.https') ? 'https' : 'http'
var host = config.get('webserver.host') var host = config.get('webserver.host')
var port = config.get('webserver.port') var port = config.get('webserver.port')
var requests = { var requests = {
makeMultipleRetryRequest: makeMultipleRetryRequest makeMultipleRetryRequest: makeMultipleRetryRequest
} }
function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) { function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) {
if (!callback) { if (!callback) {
callback = callbackEach callback = callbackEach
callbackEach = null callbackEach = null
@ -74,15 +73,15 @@
makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
} }
}, callback) }, callback)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = requests module.exports = requests
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) { function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) {
// Append the signature // Append the signature
if (signature) { if (signature) {
params.json.signature = { params.json.signature = {
@ -107,5 +106,4 @@
logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.', logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.',
params.url, replay.error.code, replay.error.message, replay.number, replay.delay) params.url, replay.error.code, replay.error.message, replay.number, replay.delay)
}) })
} }
})()

View File

@ -1,18 +1,16 @@
;(function () { 'use strict'
'use strict'
var logger = require('./logger') var logger = require('./logger')
var utils = { var utils = {
cleanForExit: cleanForExit cleanForExit: cleanForExit
} }
function cleanForExit (webtorrent_process) { function cleanForExit (webtorrent_process) {
logger.info('Gracefully exiting.') logger.info('Gracefully exiting.')
process.kill(-webtorrent_process.pid) process.kill(-webtorrent_process.pid)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = utils module.exports = utils
})()

View File

@ -1,17 +1,16 @@
;(function () { 'use strict'
'use strict'
var config = require('config') var config = require('config')
var mkdirp = require('mkdirp') var mkdirp = require('mkdirp')
var path = require('path') var path = require('path')
var checker = { var checker = {
checkConfig: checkConfig, checkConfig: checkConfig,
createDirectoriesIfNotExist: createDirectoriesIfNotExist createDirectoriesIfNotExist: createDirectoriesIfNotExist
} }
// Check the config files // Check the config files
function checkConfig () { function checkConfig () {
var required = [ 'listen.port', var required = [ 'listen.port',
'webserver.https', 'webserver.host', 'webserver.port', 'webserver.https', 'webserver.host', 'webserver.port',
'database.host', 'database.port', 'database.suffix', 'database.host', 'database.port', 'database.suffix',
@ -26,10 +25,10 @@
} }
return miss return miss
} }
// Create directories for the storage if it doesn't exist // Create directories for the storage if it doesn't exist
function createDirectoriesIfNotExist () { function createDirectoriesIfNotExist () {
var storages = config.get('storage') var storages = config.get('storage')
for (var key of Object.keys(storages)) { for (var key of Object.keys(storages)) {
@ -42,9 +41,8 @@
process.exit(0) process.exit(0)
} }
} }
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = checker module.exports = checker
})()

View File

@ -1,44 +1,42 @@
;(function () { 'use strict'
'use strict'
// API version of our pod // API version of our pod
var API_VERSION = 'v1' var API_VERSION = 'v1'
// Score a pod has when we create it as a friend // Score a pod has when we create it as a friend
var FRIEND_BASE_SCORE = 100 var FRIEND_BASE_SCORE = 100
// Time to wait between requests to the friends // Time to wait between requests to the friends
var INTERVAL = 60000 var INTERVAL = 60000
// Number of points we add/remove from a friend after a successful/bad request // Number of points we add/remove from a friend after a successful/bad request
var PODS_SCORE = { var PODS_SCORE = {
MALUS: -10, MALUS: -10,
BONUS: 10 BONUS: 10
} }
// Number of retries we make for the make retry requests (to friends...) // Number of retries we make for the make retry requests (to friends...)
var REQUEST_RETRIES = 10 var REQUEST_RETRIES = 10
// Special constants for a test instance // Special constants for a test instance
if (isTestInstance() === true) { if (isTestInstance() === true) {
FRIEND_BASE_SCORE = 20 FRIEND_BASE_SCORE = 20
INTERVAL = 10000 INTERVAL = 10000
REQUEST_RETRIES = 2 REQUEST_RETRIES = 2
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = { module.exports = {
API_VERSION: API_VERSION, API_VERSION: API_VERSION,
FRIEND_BASE_SCORE: FRIEND_BASE_SCORE, FRIEND_BASE_SCORE: FRIEND_BASE_SCORE,
INTERVAL: INTERVAL, INTERVAL: INTERVAL,
PODS_SCORE: PODS_SCORE, PODS_SCORE: PODS_SCORE,
REQUEST_RETRIES: REQUEST_RETRIES REQUEST_RETRIES: REQUEST_RETRIES
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function isTestInstance () { function isTestInstance () {
return (process.env.NODE_ENV === 'test') return (process.env.NODE_ENV === 'test')
} }
})()

View File

@ -1,20 +1,19 @@
;(function () { 'use strict'
'use strict'
var config = require('config') var config = require('config')
var mongoose = require('mongoose') var mongoose = require('mongoose')
var logger = require('../helpers/logger') var logger = require('../helpers/logger')
var dbname = 'peertube' + config.get('database.suffix') var dbname = 'peertube' + config.get('database.suffix')
var host = config.get('database.host') var host = config.get('database.host')
var port = config.get('database.port') var port = config.get('database.port')
var database = { var database = {
connect: connect connect: connect
} }
function connect () { function connect () {
mongoose.connect('mongodb://' + host + ':' + port + '/' + dbname) mongoose.connect('mongodb://' + host + ':' + port + '/' + dbname)
mongoose.connection.on('error', function () { mongoose.connection.on('error', function () {
logger.error('Mongodb connection error.') logger.error('Mongodb connection error.')
@ -24,9 +23,8 @@
mongoose.connection.on('open', function () { mongoose.connection.on('open', function () {
logger.info('Connected to mongodb.') logger.info('Connected to mongodb.')
}) })
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = database module.exports = database
})()

View File

@ -1,49 +1,48 @@
;(function () { 'use strict'
'use strict'
var async = require('async') var async = require('async')
var config = require('config') var config = require('config')
var fs = require('fs') var fs = require('fs')
var request = require('request') var request = require('request')
var constants = require('../initializers/constants') var constants = require('../initializers/constants')
var logger = require('../helpers/logger') var logger = require('../helpers/logger')
var peertubeCrypto = require('../helpers/peertubeCrypto') var peertubeCrypto = require('../helpers/peertubeCrypto')
var Pods = require('../models/pods') var Pods = require('../models/pods')
var poolRequests = require('../lib/poolRequests') var poolRequests = require('../lib/poolRequests')
var requests = require('../helpers/requests') var requests = require('../helpers/requests')
var Videos = require('../models/videos') var Videos = require('../models/videos')
var http = config.get('webserver.https') ? 'https' : 'http' var http = config.get('webserver.https') ? 'https' : 'http'
var host = config.get('webserver.host') var host = config.get('webserver.host')
var port = config.get('webserver.port') var port = config.get('webserver.port')
var pods = { var pods = {
addVideoToFriends: addVideoToFriends, addVideoToFriends: addVideoToFriends,
hasFriends: hasFriends, hasFriends: hasFriends,
makeFriends: makeFriends, makeFriends: makeFriends,
quitFriends: quitFriends, quitFriends: quitFriends,
removeVideoToFriends: removeVideoToFriends removeVideoToFriends: removeVideoToFriends
} }
function addVideoToFriends (video) { function addVideoToFriends (video) {
// To avoid duplicates // To avoid duplicates
var id = video.name + video.magnetUri var id = video.name + video.magnetUri
// ensure namePath is null // ensure namePath is null
video.namePath = null video.namePath = null
poolRequests.addRequest(id, 'add', video) poolRequests.addRequest(id, 'add', video)
} }
function hasFriends (callback) { function hasFriends (callback) {
Pods.count(function (err, count) { Pods.count(function (err, count) {
if (err) return callback(err) if (err) return callback(err)
var has_friends = (count !== 0) var has_friends = (count !== 0)
callback(null, has_friends) callback(null, has_friends)
}) })
} }
function makeFriends (callback) { function makeFriends (callback) {
var pods_score = {} var pods_score = {}
logger.info('Make friends!') logger.info('Make friends!')
@ -159,9 +158,9 @@
) )
}) })
} }
} }
function quitFriends (callback) { function quitFriends (callback) {
// Stop pool requests // Stop pool requests
poolRequests.deactivate() poolRequests.deactivate()
// Flush pool requests // Flush pool requests
@ -198,21 +197,21 @@
}) })
}) })
}) })
} }
function removeVideoToFriends (video) { function removeVideoToFriends (video) {
// To avoid duplicates // To avoid duplicates
var id = video.name + video.magnetUri var id = video.name + video.magnetUri
poolRequests.addRequest(id, 'remove', video) poolRequests.addRequest(id, 'remove', video)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = pods module.exports = pods
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function getForeignPodsList (url, callback) { function getForeignPodsList (url, callback) {
var path = '/api/' + constants.API_VERSION + '/pods' var path = '/api/' + constants.API_VERSION + '/pods'
request.get(url + path, function (err, response, body) { request.get(url + path, function (err, response, body) {
@ -220,5 +219,4 @@
callback(null, JSON.parse(body)) callback(null, JSON.parse(body))
}) })
} }
})()

View File

@ -1,31 +1,30 @@
;(function () { 'use strict'
'use strict'
var async = require('async') var async = require('async')
var pluck = require('lodash-node/compat/collection/pluck') var pluck = require('lodash-node/compat/collection/pluck')
var constants = require('../initializers/constants') var constants = require('../initializers/constants')
var logger = require('../helpers/logger') var logger = require('../helpers/logger')
var Pods = require('../models/pods') var Pods = require('../models/pods')
var PoolRequests = require('../models/poolRequests') var PoolRequests = require('../models/poolRequests')
var requests = require('../helpers/requests') var requests = require('../helpers/requests')
var Videos = require('../models/videos') var Videos = require('../models/videos')
var timer = null var timer = null
var poolRequests = { var poolRequests = {
activate: activate, activate: activate,
addRequest: addRequest, addRequest: addRequest,
deactivate: deactivate, deactivate: deactivate,
forceSend: forceSend forceSend: forceSend
} }
function activate () { function activate () {
logger.info('Pool requests activated.') logger.info('Pool requests activated.')
timer = setInterval(makePoolRequests, constants.INTERVAL) timer = setInterval(makePoolRequests, constants.INTERVAL)
} }
function addRequest (id, type, request) { function addRequest (id, type, request) {
logger.debug('Add request to the pool requests.', { id: id, type: type, request: request }) logger.debug('Add request to the pool requests.', { id: id, type: type, request: request })
PoolRequests.findById(id, function (err, entity) { PoolRequests.findById(id, function (err, entity) {
@ -54,25 +53,25 @@
}) })
} }
}) })
} }
function deactivate () { function deactivate () {
logger.info('Pool requests deactivated.') logger.info('Pool requests deactivated.')
clearInterval(timer) clearInterval(timer)
} }
function forceSend () { function forceSend () {
logger.info('Force pool requests sending.') logger.info('Force pool requests sending.')
makePoolRequests() makePoolRequests()
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = poolRequests module.exports = poolRequests
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function makePoolRequest (type, requests_to_make, callback) { function makePoolRequest (type, requests_to_make, callback) {
if (!callback) callback = function () {} if (!callback) callback = function () {}
Pods.list(function (err, pods) { Pods.list(function (err, pods) {
@ -117,9 +116,9 @@
callback(null) callback(null)
} }
}) })
} }
function makePoolRequests () { function makePoolRequests () {
logger.info('Making pool requests to friends.') logger.info('Making pool requests to friends.')
PoolRequests.list(function (err, pool_requests) { PoolRequests.list(function (err, pool_requests) {
@ -174,9 +173,9 @@
} }
}) })
}) })
} }
function removeBadPods () { function removeBadPods () {
Pods.findBadPods(function (err, pods) { Pods.findBadPods(function (err, pods) {
if (err) { if (err) {
logger.error('Cannot find bad pods.', { error: err }) logger.error('Cannot find bad pods.', { error: err })
@ -206,9 +205,9 @@
}) })
}) })
}) })
} }
function updatePodsScore (good_pods, bad_pods) { function updatePodsScore (good_pods, bad_pods) {
logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length)
Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS, function (err) { Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS, function (err) {
@ -219,5 +218,4 @@
if (err) logger.error('Cannot increment scores of bad pods.') if (err) logger.error('Cannot increment scores of bad pods.')
removeBadPods() removeBadPods()
}) })
} }
})()

View File

@ -1,22 +1,21 @@
;(function () { 'use strict'
'use strict'
var async = require('async') var async = require('async')
var config = require('config') var config = require('config')
var path = require('path') var path = require('path')
var webtorrent = require('../lib/webtorrent') var webtorrent = require('../lib/webtorrent')
var logger = require('../helpers/logger') var logger = require('../helpers/logger')
var Videos = require('../models/videos') var Videos = require('../models/videos')
var uploadDir = path.join(__dirname, '..', config.get('storage.uploads')) var uploadDir = path.join(__dirname, '..', config.get('storage.uploads'))
var videos = { var videos = {
seed: seed, seed: seed,
seedAllExisting: seedAllExisting seedAllExisting: seedAllExisting
} }
function seed (path, callback) { function seed (path, callback) {
logger.info('Seeding %s...', path) logger.info('Seeding %s...', path)
webtorrent.seed(path, function (torrent) { webtorrent.seed(path, function (torrent) {
@ -24,9 +23,9 @@
return callback(null, torrent) return callback(null, torrent)
}) })
} }
function seedAllExisting (callback) { function seedAllExisting (callback) {
Videos.listOwned(function (err, videos_list) { Videos.listOwned(function (err, videos_list) {
if (err) { if (err) {
logger.error('Cannot get list of the videos to seed.') logger.error('Cannot get list of the videos to seed.')
@ -44,9 +43,8 @@
}) })
}, callback) }, callback)
}) })
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = videos module.exports = videos
})()

View File

@ -1,30 +1,29 @@
;(function () { 'use strict'
'use strict'
var config = require('config') var config = require('config')
var ipc = require('node-ipc') var ipc = require('node-ipc')
var pathUtils = require('path') var pathUtils = require('path')
var spawn = require('electron-spawn') var spawn = require('electron-spawn')
var logger = require('../helpers/logger') var logger = require('../helpers/logger')
var host = config.get('webserver.host') var host = config.get('webserver.host')
var port = config.get('webserver.port') var port = config.get('webserver.port')
var nodeKey = 'webtorrentnode' + port var nodeKey = 'webtorrentnode' + port
var processKey = 'webtorrentprocess' + port var processKey = 'webtorrentprocess' + port
ipc.config.silent = true ipc.config.silent = true
ipc.config.id = nodeKey ipc.config.id = nodeKey
var webtorrent = { var webtorrent = {
add: add, add: add,
app: null, // Pid of the app app: null, // Pid of the app
create: create, create: create,
remove: remove, remove: remove,
seed: seed, seed: seed,
silent: false // Useful for beautiful tests silent: false // Useful for beautiful tests
} }
function create (options, callback) { function create (options, callback) {
if (typeof options === 'function') { if (typeof options === 'function') {
callback = options callback = options
options = {} options = {}
@ -72,9 +71,9 @@
}) })
ipc.server.start() ipc.server.start()
} }
function seed (path, callback) { function seed (path, callback) {
var extension = pathUtils.extname(path) var extension = pathUtils.extname(path)
var basename = pathUtils.basename(path, extension) var basename = pathUtils.basename(path, extension)
var data = { var data = {
@ -101,9 +100,9 @@
}) })
ipc.server.broadcast(processKey + '.seed', data) ipc.server.broadcast(processKey + '.seed', data)
} }
function add (magnetUri, callback) { function add (magnetUri, callback) {
var data = { var data = {
_id: magnetUri, _id: magnetUri,
args: { args: {
@ -128,9 +127,9 @@
}) })
ipc.server.broadcast(processKey + '.add', data) ipc.server.broadcast(processKey + '.add', data)
} }
function remove (magnetUri, callback) { function remove (magnetUri, callback) {
var data = { var data = {
_id: magnetUri, _id: magnetUri,
args: { args: {
@ -153,9 +152,8 @@
}) })
ipc.server.broadcast(processKey + '.remove', data) ipc.server.broadcast(processKey + '.remove', data)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = webtorrent module.exports = webtorrent
})()

View File

@ -1,7 +1,6 @@
;(function () { 'use strict'
'use strict'
function webtorrent (args) { function webtorrent (args) {
var WebTorrent = require('webtorrent') var WebTorrent = require('webtorrent')
var ipc = require('node-ipc') var ipc = require('node-ipc')
@ -87,9 +86,8 @@
process.on('uncaughtException', function (e) { process.on('uncaughtException', function (e) {
ipc.of[nodeKey].emit(processKey + '.exception', { exception: e }) ipc.of[nodeKey].emit(processKey + '.exception', { exception: e })
}) })
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = webtorrent module.exports = webtorrent
})()

View File

@ -1,11 +1,10 @@
;(function () { 'use strict'
'use strict'
var cacheMiddleware = { var cacheMiddleware = {
cache: cache cache: cache
} }
function cache (cache) { function cache (cache) {
return function (req, res, next) { return function (req, res, next) {
// If we want explicitly a cache // If we want explicitly a cache
// Or if we don't specify if we want a cache or no and we are in production // Or if we don't specify if we want a cache or no and we are in production
@ -17,9 +16,8 @@
next() next()
} }
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = cacheMiddleware module.exports = cacheMiddleware
})()

View File

@ -1,13 +1,11 @@
;(function () { 'use strict'
'use strict'
var middlewares = { var middlewares = {
cache: require('./cache'), cache: require('./cache'),
reqValidators: require('./reqValidators'), reqValidators: require('./reqValidators'),
secure: require('./secure') secure: require('./secure')
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = middlewares module.exports = middlewares
})()

View File

@ -1,13 +1,11 @@
;(function () { 'use strict'
'use strict'
var reqValidators = { var reqValidators = {
videos: require('./videos'), videos: require('./videos'),
pods: require('./pods'), pods: require('./pods'),
remote: require('./remote') remote: require('./remote')
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = reqValidators module.exports = reqValidators
})()

View File

@ -1,16 +1,15 @@
;(function () { 'use strict'
'use strict'
var checkErrors = require('./utils').checkErrors var checkErrors = require('./utils').checkErrors
var friends = require('../../lib/friends') var friends = require('../../lib/friends')
var logger = require('../../helpers/logger') var logger = require('../../helpers/logger')
var reqValidatorsPod = { var reqValidatorsPod = {
makeFriends: makeFriends, makeFriends: makeFriends,
podsAdd: podsAdd podsAdd: podsAdd
} }
function makeFriends (req, res, next) { function makeFriends (req, res, next) {
friends.hasFriends(function (err, has_friends) { friends.hasFriends(function (err, has_friends) {
if (err) { if (err) {
logger.error('Cannot know if we have friends.', { error: err }) logger.error('Cannot know if we have friends.', { error: err })
@ -24,18 +23,17 @@
next() next()
} }
}) })
} }
function podsAdd (req, res, next) { function podsAdd (req, res, next) {
req.checkBody('data.url', 'Should have an url').notEmpty().isURL({ require_protocol: true }) req.checkBody('data.url', 'Should have an url').notEmpty().isURL({ require_protocol: true })
req.checkBody('data.publicKey', 'Should have a public key').notEmpty() req.checkBody('data.publicKey', 'Should have a public key').notEmpty()
logger.debug('Checking podsAdd parameters', { parameters: req.body }) logger.debug('Checking podsAdd parameters', { parameters: req.body })
checkErrors(req, res, next) checkErrors(req, res, next)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = reqValidatorsPod module.exports = reqValidatorsPod
})()

View File

@ -1,34 +1,33 @@
;(function () { 'use strict'
'use strict'
var checkErrors = require('./utils').checkErrors var checkErrors = require('./utils').checkErrors
var logger = require('../../helpers/logger') var logger = require('../../helpers/logger')
var reqValidatorsRemote = { var reqValidatorsRemote = {
remoteVideosAdd: remoteVideosAdd, remoteVideosAdd: remoteVideosAdd,
remoteVideosRemove: remoteVideosRemove, remoteVideosRemove: remoteVideosRemove,
secureRequest: secureRequest secureRequest: secureRequest
} }
function remoteVideosAdd (req, res, next) { function remoteVideosAdd (req, res, next) {
req.checkBody('data').isArray() req.checkBody('data').isArray()
req.checkBody('data').eachIsRemoteVideosAddValid() req.checkBody('data').eachIsRemoteVideosAddValid()
logger.debug('Checking remoteVideosAdd parameters', { parameters: req.body }) logger.debug('Checking remoteVideosAdd parameters', { parameters: req.body })
checkErrors(req, res, next) checkErrors(req, res, next)
} }
function remoteVideosRemove (req, res, next) { function remoteVideosRemove (req, res, next) {
req.checkBody('data').isArray() req.checkBody('data').isArray()
req.checkBody('data').eachIsRemoteVideosRemoveValid() req.checkBody('data').eachIsRemoteVideosRemoveValid()
logger.debug('Checking remoteVideosRemove parameters', { parameters: req.body }) logger.debug('Checking remoteVideosRemove parameters', { parameters: req.body })
checkErrors(req, res, next) checkErrors(req, res, next)
} }
function secureRequest (req, res, next) { function secureRequest (req, res, next) {
req.checkBody('signature.url', 'Should have a signature url').isURL() req.checkBody('signature.url', 'Should have a signature url').isURL()
req.checkBody('signature.signature', 'Should have a signature').notEmpty() req.checkBody('signature.signature', 'Should have a signature').notEmpty()
req.checkBody('key', 'Should have a key').notEmpty() req.checkBody('key', 'Should have a key').notEmpty()
@ -37,9 +36,8 @@
logger.debug('Checking secureRequest parameters', { parameters: { data: req.body.data, keyLength: req.body.key.length } }) logger.debug('Checking secureRequest parameters', { parameters: { data: req.body.data, keyLength: req.body.key.length } })
checkErrors(req, res, next) checkErrors(req, res, next)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = reqValidatorsRemote module.exports = reqValidatorsRemote
})()

View File

@ -1,15 +1,14 @@
;(function () { 'use strict'
'use strict'
var util = require('util') var util = require('util')
var logger = require('../../helpers/logger') var logger = require('../../helpers/logger')
var reqValidatorsUtils = { var reqValidatorsUtils = {
checkErrors: checkErrors checkErrors: checkErrors
} }
function checkErrors (req, res, next, status_code) { function checkErrors (req, res, next, status_code) {
if (status_code === undefined) status_code = 400 if (status_code === undefined) status_code = 400
var errors = req.validationErrors() var errors = req.validationErrors()
@ -19,9 +18,8 @@
} }
return next() return next()
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = reqValidatorsUtils module.exports = reqValidatorsUtils
})()

View File

@ -1,18 +1,17 @@
;(function () { 'use strict'
'use strict'
var checkErrors = require('./utils').checkErrors var checkErrors = require('./utils').checkErrors
var logger = require('../../helpers/logger') var logger = require('../../helpers/logger')
var Videos = require('../../models/videos') var Videos = require('../../models/videos')
var reqValidatorsVideos = { var reqValidatorsVideos = {
videosAdd: videosAdd, videosAdd: videosAdd,
videosGet: videosGet, videosGet: videosGet,
videosRemove: videosRemove, videosRemove: videosRemove,
videosSearch: videosSearch videosSearch: videosSearch
} }
function videosAdd (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].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.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) req.checkBody('name', 'Should have a name').isLength(1, 50)
@ -21,9 +20,9 @@
logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
checkErrors(req, res, next) checkErrors(req, res, next)
} }
function videosGet (req, res, next) { function videosGet (req, res, next) {
req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
logger.debug('Checking videosGet parameters', { parameters: req.params }) logger.debug('Checking videosGet parameters', { parameters: req.params })
@ -40,9 +39,9 @@
next() next()
}) })
}) })
} }
function videosRemove (req, res, next) { function videosRemove (req, res, next) {
req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
logger.debug('Checking videosRemove parameters', { parameters: req.params }) logger.debug('Checking videosRemove parameters', { parameters: req.params })
@ -60,17 +59,16 @@
next() next()
}) })
}) })
} }
function videosSearch (req, res, next) { function videosSearch (req, res, next) {
req.checkParams('name', 'Should have a name').notEmpty() req.checkParams('name', 'Should have a name').notEmpty()
logger.debug('Checking videosSearch parameters', { parameters: req.params }) logger.debug('Checking videosSearch parameters', { parameters: req.params })
checkErrors(req, res, next) checkErrors(req, res, next)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = reqValidatorsVideos module.exports = reqValidatorsVideos
})()

View File

@ -1,15 +1,14 @@
;(function () { 'use strict'
'use strict'
var logger = require('../helpers/logger') var logger = require('../helpers/logger')
var peertubeCrypto = require('../helpers/peertubeCrypto') var peertubeCrypto = require('../helpers/peertubeCrypto')
var Pods = require('../models/pods') var Pods = require('../models/pods')
var secureMiddleware = { var secureMiddleware = {
decryptBody: decryptBody decryptBody: decryptBody
} }
function decryptBody (req, res, next) { function decryptBody (req, res, next) {
var url = req.body.signature.url var url = req.body.signature.url
Pods.findByUrl(url, function (err, pod) { Pods.findByUrl(url, function (err, pod) {
if (err) { if (err) {
@ -43,9 +42,8 @@
return res.sendStatus(403) return res.sendStatus(403)
} }
}) })
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = secureMiddleware module.exports = secureMiddleware
})()

View File

@ -1,23 +1,22 @@
;(function () { 'use strict'
'use strict'
var mongoose = require('mongoose') var mongoose = require('mongoose')
var constants = require('../initializers/constants') var constants = require('../initializers/constants')
var logger = require('../helpers/logger') var logger = require('../helpers/logger')
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
var podsSchema = mongoose.Schema({ var podsSchema = mongoose.Schema({
url: String, url: String,
publicKey: String, publicKey: String,
score: { type: Number, max: constants.FRIEND_BASE_SCORE } score: { type: Number, max: constants.FRIEND_BASE_SCORE }
}) })
var PodsDB = mongoose.model('pods', podsSchema) var PodsDB = mongoose.model('pods', podsSchema)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
var Pods = { var Pods = {
add: add, add: add,
count: count, count: count,
findByUrl: findByUrl, findByUrl: findByUrl,
@ -27,10 +26,10 @@
remove: remove, remove: remove,
removeAll: removeAll, removeAll: removeAll,
removeAllByIds: removeAllByIds removeAllByIds: removeAllByIds
} }
// TODO: check if the pod is not already a friend // TODO: check if the pod is not already a friend
function add (data, callback) { function add (data, callback) {
if (!callback) callback = function () {} if (!callback) callback = function () {}
var params = { var params = {
url: data.url, url: data.url,
@ -39,26 +38,26 @@
} }
PodsDB.create(params, callback) PodsDB.create(params, callback)
} }
function count (callback) { function count (callback) {
return PodsDB.count(callback) return PodsDB.count(callback)
} }
function findBadPods (callback) { function findBadPods (callback) {
PodsDB.find({ score: 0 }, callback) PodsDB.find({ score: 0 }, callback)
} }
function findByUrl (url, callback) { function findByUrl (url, callback) {
PodsDB.findOne({ url: url }, callback) PodsDB.findOne({ url: url }, callback)
} }
function incrementScores (ids, value, callback) { function incrementScores (ids, value, callback) {
if (!callback) callback = function () {} if (!callback) callback = function () {}
PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
} }
function list (callback) { function list (callback) {
PodsDB.find(function (err, pods_list) { PodsDB.find(function (err, pods_list) {
if (err) { if (err) {
logger.error('Cannot get the list of the pods.') logger.error('Cannot get the list of the pods.')
@ -67,24 +66,23 @@
return callback(null, pods_list) return callback(null, pods_list)
}) })
} }
function remove (url, callback) { function remove (url, callback) {
if (!callback) callback = function () {} if (!callback) callback = function () {}
PodsDB.remove({ url: url }, callback) PodsDB.remove({ url: url }, callback)
} }
function removeAll (callback) { function removeAll (callback) {
if (!callback) callback = function () {} if (!callback) callback = function () {}
PodsDB.remove(callback) PodsDB.remove(callback)
} }
function removeAllByIds (ids, callback) { function removeAllByIds (ids, callback) {
if (!callback) callback = function () {} if (!callback) callback = function () {}
PodsDB.remove({ _id: { $in: ids } }, callback) PodsDB.remove({ _id: { $in: ids } }, callback)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = Pods module.exports = Pods
})()

View File

@ -1,46 +1,45 @@
;(function () { 'use strict'
'use strict'
var mongoose = require('mongoose') var mongoose = require('mongoose')
var logger = require('../helpers/logger') var logger = require('../helpers/logger')
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
var poolRequestsSchema = mongoose.Schema({ var poolRequestsSchema = mongoose.Schema({
type: String, type: String,
id: String, // Special id to find duplicates (video created we want to remove...) id: String, // Special id to find duplicates (video created we want to remove...)
request: mongoose.Schema.Types.Mixed request: mongoose.Schema.Types.Mixed
}) })
var PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema) var PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
var PoolRequests = { var PoolRequests = {
create: create, create: create,
findById: findById, findById: findById,
list: list, list: list,
removeRequestById: removeRequestById, removeRequestById: removeRequestById,
removeRequests: removeRequests removeRequests: removeRequests
} }
function create (id, type, request, callback) { function create (id, type, request, callback) {
PoolRequestsDB.create({ id: id, type: type, request: request }, callback) PoolRequestsDB.create({ id: id, type: type, request: request }, callback)
} }
function findById (id, callback) { function findById (id, callback) {
PoolRequestsDB.findOne({ id: id }, callback) PoolRequestsDB.findOne({ id: id }, callback)
} }
function list (callback) { function list (callback) {
PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback) PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback)
} }
function removeRequestById (id, callback) { function removeRequestById (id, callback) {
PoolRequestsDB.remove({ id: id }, callback) PoolRequestsDB.remove({ id: id }, callback)
} }
function removeRequests (ids) { function removeRequests (ids) {
PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
if (err) { if (err) {
logger.error('Cannot remove requests from the pool requests database.', { error: err }) logger.error('Cannot remove requests from the pool requests database.', { error: err })
@ -49,9 +48,8 @@
logger.info('Pool requests flushed.') logger.info('Pool requests flushed.')
}) })
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = PoolRequests module.exports = PoolRequests
})()

View File

@ -1,34 +1,33 @@
;(function () { 'use strict'
'use strict'
var async = require('async') var async = require('async')
var config = require('config') var config = require('config')
var dz = require('dezalgo') var dz = require('dezalgo')
var fs = require('fs') var fs = require('fs')
var mongoose = require('mongoose') var mongoose = require('mongoose')
var path = require('path') var path = require('path')
var logger = require('../helpers/logger') var logger = require('../helpers/logger')
var http = config.get('webserver.https') === true ? 'https' : 'http' var http = config.get('webserver.https') === true ? 'https' : 'http'
var host = config.get('webserver.host') var host = config.get('webserver.host')
var port = config.get('webserver.port') var port = config.get('webserver.port')
var uploadDir = path.join(__dirname, '..', config.get('storage.uploads')) var uploadDir = path.join(__dirname, '..', config.get('storage.uploads'))
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
var videosSchema = mongoose.Schema({ var videosSchema = mongoose.Schema({
name: String, name: String,
namePath: String, namePath: String,
description: String, description: String,
magnetUri: String, magnetUri: String,
podUrl: String podUrl: String
}) })
var VideosDB = mongoose.model('videos', videosSchema) var VideosDB = mongoose.model('videos', videosSchema)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
var Videos = { var Videos = {
add: add, add: add,
addRemotes: addRemotes, addRemotes: addRemotes,
get: get, get: get,
@ -41,9 +40,9 @@
removeAllRemotesOf: removeAllRemotesOf, removeAllRemotesOf: removeAllRemotesOf,
removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris, removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris,
search: search search: search
} }
function add (video, callback) { function add (video, callback) {
logger.info('Adding %s video to database.', video.name) logger.info('Adding %s video to database.', video.name)
var params = video var params = video
@ -57,10 +56,10 @@
callback(null) callback(null)
}) })
} }
// TODO: avoid doublons // TODO: avoid doublons
function addRemotes (videos, callback) { function addRemotes (videos, callback) {
if (!callback) callback = function () {} if (!callback) callback = function () {}
var to_add = [] var to_add = []
@ -90,9 +89,9 @@
return callback(null, videos) return callback(null, videos)
}) })
}) })
} }
function get (id, callback) { function get (id, callback) {
VideosDB.findById(id, function (err, video) { VideosDB.findById(id, function (err, video) {
if (err) { if (err) {
logger.error('Cannot get this video.') logger.error('Cannot get this video.')
@ -101,9 +100,9 @@
return callback(null, video) return callback(null, video)
}) })
} }
function getVideoState (id, callback) { function getVideoState (id, callback) {
get(id, function (err, video) { get(id, function (err, video) {
if (err) return callback(err) if (err) return callback(err)
@ -115,9 +114,9 @@
return callback(null, { exist: exist, owned: owned }) return callback(null, { exist: exist, owned: owned })
}) })
} }
function isOwned (id, callback) { function isOwned (id, callback) {
VideosDB.findById(id, function (err, video) { VideosDB.findById(id, function (err, video) {
if (err || !video) { if (err || !video) {
if (!err) err = new Error('Cannot find this video.') if (!err) err = new Error('Cannot find this video.')
@ -133,9 +132,9 @@
callback(null, true, video) callback(null, true, video)
}) })
} }
function list (callback) { function list (callback) {
VideosDB.find(function (err, videos_list) { VideosDB.find(function (err, videos_list) {
if (err) { if (err) {
logger.error('Cannot get the list of the videos.') logger.error('Cannot get the list of the videos.')
@ -144,9 +143,9 @@
return callback(null, videos_list) return callback(null, videos_list)
}) })
} }
function listOwned (callback) { function listOwned (callback) {
// If namePath is not null this is *our* video // If namePath is not null this is *our* video
VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
if (err) { if (err) {
@ -156,9 +155,9 @@
return callback(null, videos_list) return callback(null, videos_list)
}) })
} }
function removeOwned (id, callback) { function removeOwned (id, callback) {
VideosDB.findByIdAndRemove(id, function (err, video) { VideosDB.findByIdAndRemove(id, function (err, video) {
if (err) { if (err) {
logger.error('Cannot remove the torrent.') logger.error('Cannot remove the torrent.')
@ -174,19 +173,19 @@
callback(null) callback(null)
}) })
}) })
} }
function removeAllRemotes (callback) { function removeAllRemotes (callback) {
VideosDB.remove({ namePath: null }, callback) VideosDB.remove({ namePath: null }, callback)
} }
function removeAllRemotesOf (fromUrl, callback) { function removeAllRemotesOf (fromUrl, callback) {
// TODO { podUrl: { $in: urls } } // TODO { podUrl: { $in: urls } }
VideosDB.remove({ podUrl: fromUrl }, callback) VideosDB.remove({ podUrl: fromUrl }, callback)
} }
// Use the magnet Uri because the _id field is not the same on different servers // Use the magnet Uri because the _id field is not the same on different servers
function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) { function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) {
if (callback === undefined) callback = function () {} if (callback === undefined) callback = function () {}
VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) { VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
@ -218,9 +217,9 @@
}) })
}) })
}) })
} }
function search (name, callback) { function search (name, callback) {
VideosDB.find({ name: new RegExp(name) }, function (err, videos) { VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
if (err) { if (err) {
logger.error('Cannot search the videos.') logger.error('Cannot search the videos.')
@ -229,9 +228,8 @@
return callback(null, videos) return callback(null, videos)
}) })
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = Videos module.exports = Videos
})()

View File

@ -1,15 +1,14 @@
;(function () { 'use strict'
'use strict'
var async = require('async') var async = require('async')
var chai = require('chai') var chai = require('chai')
var expect = chai.expect var expect = chai.expect
var pathUtils = require('path') var pathUtils = require('path')
var request = require('supertest') var request = require('supertest')
var utils = require('./utils') var utils = require('./utils')
describe('Test parameters validator', function () { describe('Test parameters validator', function () {
var app = null var app = null
var url = '' var url = ''
@ -298,5 +297,4 @@
done() done()
} }
}) })
}) })
})()

View File

@ -1,13 +1,12 @@
;(function () { 'use strict'
'use strict'
var async = require('async') var async = require('async')
var chai = require('chai') var chai = require('chai')
var expect = chai.expect var expect = chai.expect
var utils = require('./utils') var utils = require('./utils')
describe('Test advanced friends', function () { describe('Test advanced friends', function () {
var apps = [] var apps = []
var urls = [] var urls = []
@ -248,5 +247,4 @@
done() done()
} }
}) })
}) })
})()

View File

@ -1,14 +1,13 @@
;(function () { 'use strict'
'use strict'
var async = require('async') var async = require('async')
var chai = require('chai') var chai = require('chai')
var expect = chai.expect var expect = chai.expect
var request = require('supertest') var request = require('supertest')
var utils = require('./utils') var utils = require('./utils')
describe('Test basic friends', function () { describe('Test basic friends', function () {
var apps = [] var apps = []
var urls = [] var urls = []
@ -183,5 +182,4 @@
done() done()
} }
}) })
}) })
})()

View File

@ -1,10 +1,8 @@
;(function () { 'use strict'
'use strict'
// Order of the tests we want to execute // Order of the tests we want to execute
require('./checkParams') require('./checkParams')
require('./friendsBasic') require('./friendsBasic')
require('./singlePod') require('./singlePod')
require('./multiplePods') require('./multiplePods')
require('./friendsAdvanced') require('./friendsAdvanced')
})()

View File

@ -1,16 +1,15 @@
;(function () { 'use strict'
'use strict'
var async = require('async') var async = require('async')
var chai = require('chai') var chai = require('chai')
var expect = chai.expect var expect = chai.expect
var pathUtils = require('path') var pathUtils = require('path')
var utils = require('./utils') var utils = require('./utils')
var webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent')) var webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent'))
webtorrent.silent = true webtorrent.silent = true
describe('Test multiple pods', function () { describe('Test multiple pods', function () {
var apps = [] var apps = []
var urls = [] var urls = []
var to_remove = [] var to_remove = []
@ -326,5 +325,4 @@
done() done()
} }
}) })
}) })
})()

View File

@ -1,18 +1,17 @@
;(function () { 'use strict'
'use strict'
var async = require('async') var async = require('async')
var chai = require('chai') var chai = require('chai')
var expect = chai.expect var expect = chai.expect
var fs = require('fs') var fs = require('fs')
var pathUtils = require('path') var pathUtils = require('path')
var webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent')) var webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent'))
webtorrent.silent = true webtorrent.silent = true
var utils = require('./utils') var utils = require('./utils')
describe('Test a single pod', function () { describe('Test a single pod', function () {
var app = null var app = null
var url = '' var url = ''
var video_id = -1 var video_id = -1
@ -144,5 +143,4 @@
done() done()
} }
}) })
}) })
})()

View File

@ -1,13 +1,12 @@
;(function () { 'use strict'
'use strict'
var child_process = require('child_process') var child_process = require('child_process')
var exec = child_process.exec var exec = child_process.exec
var fork = child_process.fork var fork = child_process.fork
var pathUtils = require('path') var pathUtils = require('path')
var request = require('supertest') var request = require('supertest')
var testUtils = { var testUtils = {
flushTests: flushTests, flushTests: flushTests,
getFriendsList: getFriendsList, getFriendsList: getFriendsList,
getVideosList: getVideosList, getVideosList: getVideosList,
@ -18,15 +17,15 @@
runServer: runServer, runServer: runServer,
searchVideo: searchVideo, searchVideo: searchVideo,
uploadVideo: uploadVideo uploadVideo: uploadVideo
} }
// ---------------------- Export functions -------------------- // ---------------------- Export functions --------------------
function flushTests (callback) { function flushTests (callback) {
exec(pathUtils.join(__dirname, '../../scripts/clean_test.sh'), callback) exec(pathUtils.join(__dirname, '../../scripts/clean_test.sh'), callback)
} }
function getFriendsList (url, end) { function getFriendsList (url, end) {
var path = '/api/v1/pods/' var path = '/api/v1/pods/'
request(url) request(url)
@ -35,9 +34,9 @@
.expect(200) .expect(200)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.end(end) .end(end)
} }
function getVideosList (url, end) { function getVideosList (url, end) {
var path = '/api/v1/videos' var path = '/api/v1/videos'
request(url) request(url)
@ -46,9 +45,9 @@
.expect(200) .expect(200)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.end(end) .end(end)
} }
function makeFriends (url, expected_status, callback) { function makeFriends (url, expected_status, callback) {
if (!callback) { if (!callback) {
callback = expected_status callback = expected_status
expected_status = 204 expected_status = 204
@ -67,9 +66,9 @@
// Wait for the request between pods // Wait for the request between pods
setTimeout(callback, 1000) setTimeout(callback, 1000)
}) })
} }
function quitFriends (url, callback) { function quitFriends (url, callback) {
var path = '/api/v1/pods/quitfriends' var path = '/api/v1/pods/quitfriends'
// The first pod make friend with the third // The first pod make friend with the third
@ -83,9 +82,9 @@
// Wait for the request between pods // Wait for the request between pods
setTimeout(callback, 1000) setTimeout(callback, 1000)
}) })
} }
function removeVideo (url, id, end) { function removeVideo (url, id, end) {
var path = '/api/v1/videos' var path = '/api/v1/videos'
request(url) request(url)
@ -93,9 +92,9 @@
.set('Accept', 'application/json') .set('Accept', 'application/json')
.expect(204) .expect(204)
.end(end) .end(end)
} }
function flushAndRunMultipleServers (total_servers, serversRun) { function flushAndRunMultipleServers (total_servers, serversRun) {
var apps = [] var apps = []
var urls = [] var urls = []
var i = 0 var i = 0
@ -121,9 +120,9 @@
})(j) })(j)
} }
}) })
} }
function runServer (number, callback) { function runServer (number, callback) {
var port = 9000 + number var port = 9000 + number
var server_run_string = { var server_run_string = {
'Connected to mongodb': false, 'Connected to mongodb': false,
@ -155,9 +154,9 @@
app.stdout.removeListener('data', onStdout) app.stdout.removeListener('data', onStdout)
callback(app, 'http://localhost:' + port) callback(app, 'http://localhost:' + port)
}) })
} }
function searchVideo (url, search, end) { function searchVideo (url, search, end) {
var path = '/api/v1/videos' var path = '/api/v1/videos'
request(url) request(url)
@ -166,9 +165,9 @@
.expect(200) .expect(200)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.end(end) .end(end)
} }
function uploadVideo (url, name, description, fixture, end) { function uploadVideo (url, name, description, fixture, end) {
var path = '/api/v1/videos' var path = '/api/v1/videos'
request(url) request(url)
@ -179,9 +178,8 @@
.attach('input_video', pathUtils.join(__dirname, 'fixtures', fixture)) .attach('input_video', pathUtils.join(__dirname, 'fixtures', fixture))
.expect(201) .expect(201)
.end(end) .end(end)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = testUtils module.exports = testUtils
})()