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,95 +1,93 @@
;(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)
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) { if (err) {
logger.error('Cannot read cert file.') logger.error('Cannot get the list of owned videos.')
return next(err) return next(err)
} }
Videos.listOwned(function (err, videos_list) { res.json({ cert: cert, videos: videos_list })
if (err) {
logger.error('Cannot get the list of owned videos.')
return next(err)
}
res.json({ cert: cert, videos: videos_list })
})
}) })
}) })
} })
}
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)
})
}
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)
res.sendStatus(204) res.sendStatus(204)
}) })
} })
}
function removePods (req, res, next) { function quitFriends (req, res, next) {
var url = req.body.signature.url friends.quitFriends(function (err) {
Pods.remove(url, function (err) { if (err) return next(err)
if (err) return next(err)
Videos.removeAllRemotesOf(url, function (err) { res.sendStatus(204)
if (err) logger.error('Cannot remove all remote videos of %s.', url) })
else logger.info('%s pod removed.', url) }
res.sendStatus(204)
})
})
}
function quitFriends (req, res, next) {
friends.quitFriends(function (err) {
if (err) return next(err)
res.sendStatus(204)
})
}
})()

View File

@ -1,55 +1,53 @@
;(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')
videos.removeRemotesOfByMagnetUris(url, magnetUris, function (err) { videos.removeRemotesOfByMagnetUris(url, magnetUris, function (err) {
if (err) return next(err) if (err) return next(err)
res.sendStatus(204) res.sendStatus(204)
}) })
} }
})()

View File

@ -1,146 +1,144 @@
;(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)
}, },
filename: function (req, file, cb) { filename: function (req, file, cb) {
var extension = '' var extension = ''
if (file.mimetype === 'video/webm') extension = 'webm' if (file.mimetype === 'video/webm') extension = 'webm'
else if (file.mimetype === 'video/mp4') extension = 'mp4' else if (file.mimetype === 'video/mp4') extension = 'mp4'
else if (file.mimetype === 'video/ogg') extension = 'ogv' else if (file.mimetype === 'video/ogg') extension = 'ogv'
crypto.pseudoRandomBytes(16, function (err, raw) { crypto.pseudoRandomBytes(16, function (err, raw) {
var fieldname = err ? undefined : raw.toString('hex') var fieldname = err ? undefined : raw.toString('hex')
cb(null, fieldname + '.' + extension) cb(null, fieldname + '.' + extension)
}) })
}
})
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)
// ---------------------------------------------------------------------------
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)
} }
})
var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }]) var video_data = {
name: video_infos.name,
namePath: video_file.filename,
description: video_infos.description,
magnetUri: torrent.magnetURI
}
router.get('/', cacheMiddleware.cache(false), listVideos) Videos.add(video_data, function (err) {
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
// ---------------------------------------------------------------------------
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) { if (err) {
logger.error('Cannot seed this video.') // TODO unseed the video
logger.error('Cannot insert this video in the database.')
return next(err) return next(err)
} }
var video_data = { // Now we'll add the video's meta data to our friends
name: video_infos.name, friends.addVideoToFriends(video_data)
namePath: video_file.filename,
description: video_infos.description,
magnetUri: torrent.magnetURI
}
Videos.add(video_data, function (err) { // TODO : include Location of the new video
if (err) { res.sendStatus(201)
// TODO unseed the video })
logger.error('Cannot insert this video in the database.') })
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)
}
res.json(video)
})
}
function listVideos (req, res, next) {
Videos.list(function (err, videos_list) {
if (err) return next(err)
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)
removeTorrent(video.magnetUri, function () {
Videos.removeOwned(req.params.id, function (err) {
if (err) return next(err)
var params = {
name: video.name,
magnetUri: video.magnetUri
} }
// Now we'll add the video's meta data to our friends friends.removeVideoToFriends(params)
friends.addVideoToFriends(video_data) res.sendStatus(204)
// TODO : include Location of the new video
res.sendStatus(201)
}) })
}) })
})
}
function searchVideos (req, res, next) {
Videos.search(req.params.name, function (err, videos_list) {
if (err) return next(err)
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)
} }
}
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)
}
res.json(video)
})
}
function listVideos (req, res, next) {
Videos.list(function (err, videos_list) {
if (err) return next(err)
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)
removeTorrent(video.magnetUri, function () {
Videos.removeOwned(req.params.id, function (err) {
if (err) return next(err)
var params = {
name: video.name,
magnetUri: video.magnetUri
}
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)
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)
}
}
})()

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,42 +1,40 @@
;(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',
filename: path.join(logDir, 'all-logs.log'), filename: path.join(logDir, 'all-logs.log'),
handleExceptions: true, handleExceptions: true,
json: true, json: true,
maxsize: 5242880, maxsize: 5242880,
maxFiles: 5, maxFiles: 5,
colorize: false colorize: false
}), }),
new winston.transports.Console({ new winston.transports.Console({
level: 'debug', level: 'debug',
handleExceptions: true, handleExceptions: true,
humanReadableUnhandledException: true, humanReadableUnhandledException: true,
json: false, json: false,
colorize: true colorize: true
}) })
], ],
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,149 +1,147 @@
;(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)
} }
createCerts(function (err) { createCerts(function (err) {
return callback(err)
})
})
}
function decrypt (key, data, callback) {
fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) {
if (err) return callback(err)
var my_private_key = ursa.createPrivateKey(file)
var decrypted_key = my_private_key.decrypt(key, 'hex', 'utf8')
var decrypted_data = symetricDecrypt(data, decrypted_key)
return callback(null, decrypted_data)
})
}
function encrypt (public_key, data, callback) {
var crt = ursa.createPublicKey(public_key)
symetricEncrypt(data, function (err, dataEncrypted) {
if (err) return callback(err)
var key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex')
var encrypted = {
data: dataEncrypted.crypted,
key: key
}
callback(null, encrypted)
})
}
function getCertDir () {
return certDir
}
function sign (data) {
var myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem'))
var signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')
return signature
}
// ---------------------------------------------------------------------------
module.exports = peertubeCrypto
// ---------------------------------------------------------------------------
function certsExist (callback) {
fs.exists(certDir + 'peertube.key.pem', function (exists) {
return callback(exists)
})
}
function createCerts (callback) {
certsExist(function (exist) {
if (exist === true) {
var string = 'Certs already exist.'
logger.warning(string)
return callback(new Error(string))
}
logger.info('Generating a RSA key...')
openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) {
if (err) {
logger.error('Cannot create private key on this pod.')
return callback(err) return callback(err)
})
})
}
function decrypt (key, data, callback) {
fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) {
if (err) return callback(err)
var my_private_key = ursa.createPrivateKey(file)
var decrypted_key = my_private_key.decrypt(key, 'hex', 'utf8')
var decrypted_data = symetricDecrypt(data, decrypted_key)
return callback(null, decrypted_data)
})
}
function encrypt (public_key, data, callback) {
var crt = ursa.createPublicKey(public_key)
symetricEncrypt(data, function (err, dataEncrypted) {
if (err) return callback(err)
var key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex')
var encrypted = {
data: dataEncrypted.crypted,
key: key
} }
logger.info('RSA key generated.')
callback(null, encrypted) logger.info('Manage public key...')
}) openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) {
}
function getCertDir () {
return certDir
}
function sign (data) {
var myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem'))
var signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')
return signature
}
// ---------------------------------------------------------------------------
module.exports = peertubeCrypto
// ---------------------------------------------------------------------------
function certsExist (callback) {
fs.exists(certDir + 'peertube.key.pem', function (exists) {
return callback(exists)
})
}
function createCerts (callback) {
certsExist(function (exist) {
if (exist === true) {
var string = 'Certs already exist.'
logger.warning(string)
return callback(new Error(string))
}
logger.info('Generating a RSA key...')
openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) {
if (err) { if (err) {
logger.error('Cannot create private key on this pod.') logger.error('Cannot create public key on this pod.')
return callback(err) return callback(err)
} }
logger.info('RSA key generated.')
logger.info('Manage public key...') logger.info('Public key managed.')
openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) { return callback(null)
if (err) {
logger.error('Cannot create public key on this pod.')
return callback(err)
}
logger.info('Public key managed.')
return callback(null)
})
}) })
}) })
} })
}
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)
var cipher = crypto.createCipher(algorithm, password) var cipher = crypto.createCipher(algorithm, password)
var crypted = cipher.update(text, 'utf8', 'hex') var crypted = cipher.update(text, 'utf8', 'hex')
crypted += cipher.final('hex') crypted += cipher.final('hex')
callback(null, { crypted: crypted, password: password }) callback(null, { crypted: crypted, password: password })
}) })
} }
})()

View File

@ -1,111 +1,109 @@
;(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) {
if (!callback) {
callback = callbackEach
callbackEach = null
} }
function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) { var url = http + '://' + host + ':' + port
if (!callback) { var signature
callback = callbackEach
callbackEach = null
}
var url = http + '://' + host + ':' + port // Add signature if it is specified in the params
var signature if (all_data.method === 'POST' && all_data.data && all_data.sign === true) {
signature = peertubeCrypto.sign(url)
}
// Add signature if it is specified in the params // Make a request for each pod
if (all_data.method === 'POST' && all_data.data && all_data.sign === true) { async.each(pods, function (pod, callback_each_async) {
signature = peertubeCrypto.sign(url) function callbackEachRetryRequest (err, response, body, url, pod) {
} if (callbackEach !== null) {
callbackEach(err, response, body, url, pod, function () {
// Make a request for each pod
async.each(pods, function (pod, callback_each_async) {
function callbackEachRetryRequest (err, response, body, url, pod) {
if (callbackEach !== null) {
callbackEach(err, response, body, url, pod, function () {
callback_each_async()
})
} else {
callback_each_async() callback_each_async()
} })
}
var params = {
url: pod.url + all_data.path,
method: all_data.method
}
// Add data with POST requst ?
if (all_data.method === 'POST' && all_data.data) {
// Encrypt data ?
if (all_data.encrypt === true) {
// TODO: ES6 with let
;(function (copy_params, copy_url, copy_pod, copy_signature) {
peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(all_data.data), function (err, encrypted) {
if (err) return callback(err)
copy_params.json = {
data: encrypted.data,
key: encrypted.key
}
makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest)
})
})(params, url, pod, signature)
} else {
params.json = { data: all_data.data }
makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
}
} else { } else {
callback_each_async()
}
}
var params = {
url: pod.url + all_data.path,
method: all_data.method
}
// Add data with POST requst ?
if (all_data.method === 'POST' && all_data.data) {
// Encrypt data ?
if (all_data.encrypt === true) {
// TODO: ES6 with let
;(function (copy_params, copy_url, copy_pod, copy_signature) {
peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(all_data.data), function (err, encrypted) {
if (err) return callback(err)
copy_params.json = {
data: encrypted.data,
key: encrypted.key
}
makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest)
})
})(params, url, pod, signature)
} else {
params.json = { data: all_data.data }
makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
} }
}, callback) } else {
} makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
// ---------------------------------------------------------------------------
module.exports = requests
// ---------------------------------------------------------------------------
function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) {
// Append the signature
if (signature) {
params.json.signature = {
url: from_url,
signature: signature
}
} }
}, callback)
}
logger.debug('Make retry requests to %s.', to_pod.url) // ---------------------------------------------------------------------------
replay( module.exports = requests
request.post(params, function (err, response, body) {
callbackEach(err, response, body, params.url, to_pod) // ---------------------------------------------------------------------------
}),
{ function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) {
retries: constants.REQUEST_RETRIES, // Append the signature
factor: 3, if (signature) {
maxTimeout: Infinity, params.json.signature = {
errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] url: from_url,
} signature: signature
).on('replay', function (replay) { }
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)
})
} }
})()
logger.debug('Make retry requests to %s.', to_pod.url)
replay(
request.post(params, function (err, response, body) {
callbackEach(err, response, body, params.url, to_pod)
}),
{
retries: constants.REQUEST_RETRIES,
factor: 3,
maxTimeout: Infinity,
errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
}
).on('replay', function (replay) {
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)
})
}

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,50 +1,48 @@
;(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',
'storage.certs', 'storage.uploads', 'storage.logs', 'storage.certs', 'storage.uploads', 'storage.logs',
'network.friends' ] 'network.friends' ]
var miss = [] var miss = []
for (var key of required) { for (var key of required) {
if (!config.has(key)) { if (!config.has(key)) {
miss.push(key) miss.push(key)
}
}
return miss
}
// Create directories for the storage if it doesn't exist
function createDirectoriesIfNotExist () {
var storages = config.get('storage')
for (var key of Object.keys(storages)) {
var dir = storages[key]
try {
mkdirp.sync(path.join(__dirname, '..', dir))
} catch (error) {
// Do not use logger
console.error('Cannot create ' + path + ':' + error)
process.exit(0)
}
} }
} }
// --------------------------------------------------------------------------- return miss
}
module.exports = checker // Create directories for the storage if it doesn't exist
})() function createDirectoriesIfNotExist () {
var storages = config.get('storage')
for (var key of Object.keys(storages)) {
var dir = storages[key]
try {
mkdirp.sync(path.join(__dirname, '..', dir))
} catch (error) {
// Do not use logger
console.error('Cannot create ' + path + ':' + error)
process.exit(0)
}
}
}
// ---------------------------------------------------------------------------
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,32 +1,30 @@
;(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.')
process.exit(0) process.exit(0)
}) })
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,224 +1,222 @@
;(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)
var has_friends = (count !== 0)
callback(null, has_friends)
})
}
function makeFriends (callback) {
var pods_score = {}
logger.info('Make friends!')
fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
if (err) {
logger.error('Cannot read public cert.')
return callback(err)
}
var urls = config.get('network.friends')
async.each(urls, computeForeignPodsList, function (err) {
if (err) return callback(err) if (err) return callback(err)
var has_friends = (count !== 0) logger.debug('Pods scores computed.', { pods_score: pods_score })
callback(null, has_friends) var pods_list = computeWinningPods(urls, pods_score)
logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list })
makeRequestsToWinningPods(cert, pods_list)
})
})
// -----------------------------------------------------------------------
function computeForeignPodsList (url, callback) {
// Let's give 1 point to the pod we ask the friends list
pods_score[url] = 1
getForeignPodsList(url, function (err, foreign_pods_list) {
if (err) return callback(err)
if (foreign_pods_list.length === 0) return callback()
async.each(foreign_pods_list, function (foreign_pod, callback_each) {
var foreign_url = foreign_pod.url
if (pods_score[foreign_url]) pods_score[foreign_url]++
else pods_score[foreign_url] = 1
callback_each()
}, function () {
callback()
})
}) })
} }
function makeFriends (callback) { function computeWinningPods (urls, pods_score) {
var pods_score = {} // Build the list of pods to add
// Only add a pod if it exists in more than a half base pods
logger.info('Make friends!') var pods_list = []
fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { var base_score = urls.length / 2
if (err) { Object.keys(pods_score).forEach(function (pod) {
logger.error('Cannot read public cert.') if (pods_score[pod] > base_score) pods_list.push({ url: pod })
return callback(err)
}
var urls = config.get('network.friends')
async.each(urls, computeForeignPodsList, function (err) {
if (err) return callback(err)
logger.debug('Pods scores computed.', { pods_score: pods_score })
var pods_list = computeWinningPods(urls, pods_score)
logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list })
makeRequestsToWinningPods(cert, pods_list)
})
}) })
// ----------------------------------------------------------------------- return pods_list
function computeForeignPodsList (url, callback) {
// Let's give 1 point to the pod we ask the friends list
pods_score[url] = 1
getForeignPodsList(url, function (err, foreign_pods_list) {
if (err) return callback(err)
if (foreign_pods_list.length === 0) return callback()
async.each(foreign_pods_list, function (foreign_pod, callback_each) {
var foreign_url = foreign_pod.url
if (pods_score[foreign_url]) pods_score[foreign_url]++
else pods_score[foreign_url] = 1
callback_each()
}, function () {
callback()
})
})
}
function computeWinningPods (urls, pods_score) {
// Build the list of pods to add
// Only add a pod if it exists in more than a half base pods
var pods_list = []
var base_score = urls.length / 2
Object.keys(pods_score).forEach(function (pod) {
if (pods_score[pod] > base_score) pods_list.push({ url: pod })
})
return pods_list
}
function makeRequestsToWinningPods (cert, pods_list) {
// Stop pool requests
poolRequests.deactivate()
// Flush pool requests
poolRequests.forceSend()
// Get the list of our videos to send to our new friends
Videos.listOwned(function (err, videos_list) {
if (err) {
logger.error('Cannot get the list of videos we own.')
return callback(err)
}
var data = {
url: http + '://' + host + ':' + port,
publicKey: cert,
videos: videos_list
}
requests.makeMultipleRetryRequest(
{ method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
pods_list,
function eachRequest (err, response, body, url, pod, callback_each_request) {
// We add the pod if it responded correctly with its public certificate
if (!err && response.statusCode === 200) {
Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
if (err) logger.error('Error with adding %s pod.', pod.url, { error: err })
Videos.addRemotes(body.videos, function (err) {
if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err })
logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos })
return callback_each_request()
})
})
} else {
logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
return callback_each_request()
}
},
function endRequests (err) {
// Now we made new friends, we can re activate the pool of requests
poolRequests.activate()
if (err) {
logger.error('There was some errors when we wanted to make friends.')
return callback(err)
}
logger.debug('makeRequestsToWinningPods finished.')
return callback(null)
}
)
})
}
} }
function quitFriends (callback) { function makeRequestsToWinningPods (cert, pods_list) {
// Stop pool requests // Stop pool requests
poolRequests.deactivate() poolRequests.deactivate()
// Flush pool requests // Flush pool requests
poolRequests.forceSend() poolRequests.forceSend()
Pods.list(function (err, pods) { // Get the list of our videos to send to our new friends
if (err) return callback(err) Videos.listOwned(function (err, videos_list) {
if (err) {
var request = { logger.error('Cannot get the list of videos we own.')
method: 'POST', return callback(err)
path: '/api/' + constants.API_VERSION + '/pods/remove',
sign: true,
encrypt: true,
data: {
url: 'me' // Fake data
}
} }
// Announce we quit them var data = {
requests.makeMultipleRetryRequest(request, pods, function () { url: http + '://' + host + ':' + port,
Pods.removeAll(function (err) { publicKey: cert,
videos: videos_list
}
requests.makeMultipleRetryRequest(
{ method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
pods_list,
function eachRequest (err, response, body, url, pod, callback_each_request) {
// We add the pod if it responded correctly with its public certificate
if (!err && response.statusCode === 200) {
Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
if (err) logger.error('Error with adding %s pod.', pod.url, { error: err })
Videos.addRemotes(body.videos, function (err) {
if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err })
logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos })
return callback_each_request()
})
})
} else {
logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
return callback_each_request()
}
},
function endRequests (err) {
// Now we made new friends, we can re activate the pool of requests
poolRequests.activate() poolRequests.activate()
if (err) {
logger.error('There was some errors when we wanted to make friends.')
return callback(err)
}
logger.debug('makeRequestsToWinningPods finished.')
return callback(null)
}
)
})
}
}
function quitFriends (callback) {
// Stop pool requests
poolRequests.deactivate()
// Flush pool requests
poolRequests.forceSend()
Pods.list(function (err, pods) {
if (err) return callback(err)
var request = {
method: 'POST',
path: '/api/' + constants.API_VERSION + '/pods/remove',
sign: true,
encrypt: true,
data: {
url: 'me' // Fake data
}
}
// Announce we quit them
requests.makeMultipleRetryRequest(request, pods, function () {
Pods.removeAll(function (err) {
poolRequests.activate()
if (err) return callback(err)
logger.info('Broke friends, so sad :(')
Videos.removeAllRemotes(function (err) {
if (err) return callback(err) if (err) return callback(err)
logger.info('Broke friends, so sad :(') logger.info('Removed all remote videos.')
callback(null)
Videos.removeAllRemotes(function (err) {
if (err) return callback(err)
logger.info('Removed all remote videos.')
callback(null)
})
}) })
}) })
}) })
} })
}
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) {
if (err) return callback(err) if (err) return callback(err)
callback(null, JSON.parse(body)) callback(null, JSON.parse(body))
}) })
} }
})()

View File

@ -1,223 +1,221 @@
;(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) {
if (err) { if (err) {
logger.error('Cannot find one pool request.', { error: err }) logger.error('Cannot find one pool request.', { error: err })
return // Abort
}
if (entity) {
if (entity.type === type) {
logger.error('Cannot insert two same requests.')
return // Abort return // Abort
} }
if (entity) { // Remove the request of the other type
if (entity.type === type) { PoolRequests.removeRequestById(id, function (err) {
logger.error('Cannot insert two same requests.') if (err) {
logger.error('Cannot remove a pool request.', { error: err })
return // Abort return // Abort
} }
// Remove the request of the other type
PoolRequests.removeRequestById(id, function (err) {
if (err) {
logger.error('Cannot remove a pool request.', { error: err })
return // Abort
}
})
} else {
PoolRequests.create(id, type, request, function (err) {
if (err) logger.error('Cannot create a pool request.', { error: err })
return // Abort
})
}
})
}
function deactivate () {
logger.info('Pool requests deactivated.')
clearInterval(timer)
}
function forceSend () {
logger.info('Force pool requests sending.')
makePoolRequests()
}
// ---------------------------------------------------------------------------
module.exports = poolRequests
// ---------------------------------------------------------------------------
function makePoolRequest (type, requests_to_make, callback) {
if (!callback) callback = function () {}
Pods.list(function (err, pods) {
if (err) return callback(err)
var params = {
encrypt: true,
sign: true,
method: 'POST',
path: null,
data: requests_to_make
}
if (type === 'add') {
params.path = '/api/' + constants.API_VERSION + '/remotevideos/add'
} else if (type === 'remove') {
params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove'
} else {
return callback(new Error('Unkown pool request type.'))
}
var bad_pods = []
var good_pods = []
requests.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished)
function callbackEachPodFinished (err, response, body, url, pod, callback_each_pod_finished) {
if (err || (response.statusCode !== 200 && response.statusCode !== 204)) {
bad_pods.push(pod._id)
logger.error('Error sending secure request to %s pod.', url, { error: err || new Error('Status code not 20x') })
} else {
good_pods.push(pod._id)
}
return callback_each_pod_finished()
}
function callbackAllPodsFinished (err) {
if (err) return callback(err)
updatePodsScore(good_pods, bad_pods)
callback(null)
}
})
}
function makePoolRequests () {
logger.info('Making pool requests to friends.')
PoolRequests.list(function (err, pool_requests) {
if (err) {
logger.error('Cannot get the list of pool requests.', { err: err })
return // Abort
}
if (pool_requests.length === 0) return
var requests_to_make = {
add: {
ids: [],
requests: []
},
remove: {
ids: [],
requests: []
}
}
async.each(pool_requests, function (pool_request, callback_each) {
if (pool_request.type === 'add') {
requests_to_make.add.requests.push(pool_request.request)
requests_to_make.add.ids.push(pool_request._id)
} else if (pool_request.type === 'remove') {
requests_to_make.remove.requests.push(pool_request.request)
requests_to_make.remove.ids.push(pool_request._id)
} else {
logger.error('Unkown pool request type.', { request_type: pool_request.type })
return // abort
}
callback_each()
}, function () {
// Send the add requests
if (requests_to_make.add.requests.length !== 0) {
makePoolRequest('add', requests_to_make.add.requests, function (err) {
if (err) logger.error('Errors when sent add pool requests.', { error: err })
PoolRequests.removeRequests(requests_to_make.add.ids)
})
}
// Send the remove requests
if (requests_to_make.remove.requests.length !== 0) {
makePoolRequest('remove', requests_to_make.remove.requests, function (err) {
if (err) logger.error('Errors when sent remove pool requests.', { error: err })
PoolRequests.removeRequests(requests_to_make.remove.ids)
})
}
}) })
}) } else {
} PoolRequests.create(id, type, request, function (err) {
if (err) logger.error('Cannot create a pool request.', { error: err })
return // Abort
})
}
})
}
function removeBadPods () { function deactivate () {
Pods.findBadPods(function (err, pods) { logger.info('Pool requests deactivated.')
if (err) { clearInterval(timer)
logger.error('Cannot find bad pods.', { error: err }) }
function forceSend () {
logger.info('Force pool requests sending.')
makePoolRequests()
}
// ---------------------------------------------------------------------------
module.exports = poolRequests
// ---------------------------------------------------------------------------
function makePoolRequest (type, requests_to_make, callback) {
if (!callback) callback = function () {}
Pods.list(function (err, pods) {
if (err) return callback(err)
var params = {
encrypt: true,
sign: true,
method: 'POST',
path: null,
data: requests_to_make
}
if (type === 'add') {
params.path = '/api/' + constants.API_VERSION + '/remotevideos/add'
} else if (type === 'remove') {
params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove'
} else {
return callback(new Error('Unkown pool request type.'))
}
var bad_pods = []
var good_pods = []
requests.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished)
function callbackEachPodFinished (err, response, body, url, pod, callback_each_pod_finished) {
if (err || (response.statusCode !== 200 && response.statusCode !== 204)) {
bad_pods.push(pod._id)
logger.error('Error sending secure request to %s pod.', url, { error: err || new Error('Status code not 20x') })
} else {
good_pods.push(pod._id)
}
return callback_each_pod_finished()
}
function callbackAllPodsFinished (err) {
if (err) return callback(err)
updatePodsScore(good_pods, bad_pods)
callback(null)
}
})
}
function makePoolRequests () {
logger.info('Making pool requests to friends.')
PoolRequests.list(function (err, pool_requests) {
if (err) {
logger.error('Cannot get the list of pool requests.', { err: err })
return // Abort
}
if (pool_requests.length === 0) return
var requests_to_make = {
add: {
ids: [],
requests: []
},
remove: {
ids: [],
requests: []
}
}
async.each(pool_requests, function (pool_request, callback_each) {
if (pool_request.type === 'add') {
requests_to_make.add.requests.push(pool_request.request)
requests_to_make.add.ids.push(pool_request._id)
} else if (pool_request.type === 'remove') {
requests_to_make.remove.requests.push(pool_request.request)
requests_to_make.remove.ids.push(pool_request._id)
} else {
logger.error('Unkown pool request type.', { request_type: pool_request.type })
return // abort return // abort
} }
if (pods.length === 0) return callback_each()
}, function () {
// Send the add requests
if (requests_to_make.add.requests.length !== 0) {
makePoolRequest('add', requests_to_make.add.requests, function (err) {
if (err) logger.error('Errors when sent add pool requests.', { error: err })
var urls = pluck(pods, 'url') PoolRequests.removeRequests(requests_to_make.add.ids)
var ids = pluck(pods, '_id')
Videos.removeAllRemotesOf(urls, function (err, r) {
if (err) {
logger.error('Cannot remove videos from a pod that we removing.', { error: err })
} else {
var videos_removed = r.result.n
logger.info('Removed %d videos.', videos_removed)
}
Pods.removeAllByIds(ids, function (err, r) {
if (err) {
logger.error('Cannot remove bad pods.', { error: err })
} else {
var pods_removed = r.result.n
logger.info('Removed %d pods.', pods_removed)
}
}) })
}
// Send the remove requests
if (requests_to_make.remove.requests.length !== 0) {
makePoolRequest('remove', requests_to_make.remove.requests, function (err) {
if (err) logger.error('Errors when sent remove pool requests.', { error: err })
PoolRequests.removeRequests(requests_to_make.remove.ids)
})
}
})
})
}
function removeBadPods () {
Pods.findBadPods(function (err, pods) {
if (err) {
logger.error('Cannot find bad pods.', { error: err })
return // abort
}
if (pods.length === 0) return
var urls = pluck(pods, 'url')
var ids = pluck(pods, '_id')
Videos.removeAllRemotesOf(urls, function (err, r) {
if (err) {
logger.error('Cannot remove videos from a pod that we removing.', { error: err })
} else {
var videos_removed = r.result.n
logger.info('Removed %d videos.', videos_removed)
}
Pods.removeAllByIds(ids, function (err, r) {
if (err) {
logger.error('Cannot remove bad pods.', { error: err })
} else {
var pods_removed = r.result.n
logger.info('Removed %d pods.', pods_removed)
}
}) })
}) })
} })
}
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) {
if (err) logger.error('Cannot increment scores of good pods.') if (err) logger.error('Cannot increment scores of good pods.')
}) })
Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) { Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) {
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,52 +1,50 @@
;(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) {
logger.info('%s seeded (%s).', path, torrent.magnetURI) logger.info('%s seeded (%s).', path, torrent.magnetURI)
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.')
return callback(err) return callback(err)
} }
async.each(videos_list, function (video, each_callback) { async.each(videos_list, function (video, each_callback) {
seed(uploadDir + video.namePath, function (err) { seed(uploadDir + video.namePath, function (err) {
if (err) { if (err) {
logger.error('Cannot seed this video.') logger.error('Cannot seed this video.')
return callback(err) return callback(err)
} }
each_callback(null) each_callback(null)
}) })
}, callback) }, callback)
}) })
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = videos module.exports = videos
})()

View File

@ -1,161 +1,159 @@
;(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) {
if (typeof options === 'function') {
callback = options
options = {}
} }
function create (options, callback) { // Override options
if (typeof options === 'function') { if (options.host) host = options.host
callback = options if (options.port) {
options = {} port = options.port
} nodeKey = 'webtorrentnode' + port
processKey = 'webtorrentprocess' + port
ipc.config.id = nodeKey
}
// Override options ipc.serve(function () {
if (options.host) host = options.host if (!webtorrent.silent) logger.info('IPC server ready.')
if (options.port) {
port = options.port
nodeKey = 'webtorrentnode' + port
processKey = 'webtorrentprocess' + port
ipc.config.id = nodeKey
}
ipc.serve(function () { // Run a timeout of 30s after which we exit the process
if (!webtorrent.silent) logger.info('IPC server ready.') var timeout_webtorrent_process = setTimeout(function () {
logger.error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.')
process.exit()
}, 30000)
// Run a timeout of 30s after which we exit the process ipc.server.on(processKey + '.ready', function () {
var timeout_webtorrent_process = setTimeout(function () { if (!webtorrent.silent) logger.info('Webtorrent process ready.')
logger.error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.') clearTimeout(timeout_webtorrent_process)
process.exit() callback()
}, 30000)
ipc.server.on(processKey + '.ready', function () {
if (!webtorrent.silent) logger.info('Webtorrent process ready.')
clearTimeout(timeout_webtorrent_process)
callback()
})
ipc.server.on(processKey + '.exception', function (data) {
logger.error('Received exception error from webtorrent process.', { exception: data.exception })
process.exit()
})
var webtorrent_process = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true })
webtorrent_process.stderr.on('data', function (data) {
// logger.debug('Webtorrent process stderr: ', data.toString())
})
webtorrent_process.stdout.on('data', function (data) {
// logger.debug('Webtorrent process:', data.toString())
})
webtorrent.app = webtorrent_process
}) })
ipc.server.start() ipc.server.on(processKey + '.exception', function (data) {
} logger.error('Received exception error from webtorrent process.', { exception: data.exception })
process.exit()
function seed (path, callback) {
var extension = pathUtils.extname(path)
var basename = pathUtils.basename(path, extension)
var data = {
_id: basename,
args: {
path: path
}
}
if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id)
// Finish signal
var event_key = nodeKey + '.seedDone.' + data._id
ipc.server.on(event_key, function listener (received) {
if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri)
// This is a fake object, we just use the magnetUri in this project
var torrent = {
magnetURI: received.magnetUri
}
ipc.server.off(event_key)
callback(torrent)
}) })
ipc.server.broadcast(processKey + '.seed', data) var webtorrent_process = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true })
} webtorrent_process.stderr.on('data', function (data) {
// logger.debug('Webtorrent process stderr: ', data.toString())
function add (magnetUri, callback) {
var data = {
_id: magnetUri,
args: {
magnetUri: magnetUri
}
}
if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id)
// Finish signal
var event_key = nodeKey + '.addDone.' + data._id
ipc.server.on(event_key, function (received) {
if (!webtorrent.silent) logger.debug('Process added torrent.')
// This is a fake object, we just use the magnetUri in this project
var torrent = {
files: received.files
}
ipc.server.off(event_key)
callback(torrent)
}) })
ipc.server.broadcast(processKey + '.add', data) webtorrent_process.stdout.on('data', function (data) {
} // logger.debug('Webtorrent process:', data.toString())
function remove (magnetUri, callback) {
var data = {
_id: magnetUri,
args: {
magnetUri: magnetUri
}
}
if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id)
// Finish signal
var event_key = nodeKey + '.removeDone.' + data._id
ipc.server.on(event_key, function (received) {
if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id)
var err = null
if (received.err) err = received.err
ipc.server.off(event_key)
callback(err)
}) })
ipc.server.broadcast(processKey + '.remove', data) webtorrent.app = webtorrent_process
})
ipc.server.start()
}
function seed (path, callback) {
var extension = pathUtils.extname(path)
var basename = pathUtils.basename(path, extension)
var data = {
_id: basename,
args: {
path: path
}
} }
// --------------------------------------------------------------------------- if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id)
module.exports = webtorrent // Finish signal
})() var event_key = nodeKey + '.seedDone.' + data._id
ipc.server.on(event_key, function listener (received) {
if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri)
// This is a fake object, we just use the magnetUri in this project
var torrent = {
magnetURI: received.magnetUri
}
ipc.server.off(event_key)
callback(torrent)
})
ipc.server.broadcast(processKey + '.seed', data)
}
function add (magnetUri, callback) {
var data = {
_id: magnetUri,
args: {
magnetUri: magnetUri
}
}
if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id)
// Finish signal
var event_key = nodeKey + '.addDone.' + data._id
ipc.server.on(event_key, function (received) {
if (!webtorrent.silent) logger.debug('Process added torrent.')
// This is a fake object, we just use the magnetUri in this project
var torrent = {
files: received.files
}
ipc.server.off(event_key)
callback(torrent)
})
ipc.server.broadcast(processKey + '.add', data)
}
function remove (magnetUri, callback) {
var data = {
_id: magnetUri,
args: {
magnetUri: magnetUri
}
}
if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id)
// Finish signal
var event_key = nodeKey + '.removeDone.' + data._id
ipc.server.on(event_key, function (received) {
if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id)
var err = null
if (received.err) err = received.err
ipc.server.off(event_key)
callback(err)
})
ipc.server.broadcast(processKey + '.remove', data)
}
// ---------------------------------------------------------------------------
module.exports = webtorrent

View File

@ -1,95 +1,93 @@
;(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')
if (args.length !== 3) { if (args.length !== 3) {
console.log('Wrong arguments number: ' + args.length + '/3') console.log('Wrong arguments number: ' + args.length + '/3')
process.exit(-1) process.exit(-1)
} }
var host = args[1] var host = args[1]
var port = args[2] var port = args[2]
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 = processKey ipc.config.id = processKey
if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = [] if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = []
else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket' else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket'
var wt = new WebTorrent({ dht: false }) var wt = new WebTorrent({ dht: false })
function seed (data) { function seed (data) {
var args = data.args var args = data.args
var path = args.path var path = args.path
var _id = data._id var _id = data._id
wt.seed(path, { announceList: '' }, function (torrent) { wt.seed(path, { announceList: '' }, function (torrent) {
var to_send = { var to_send = {
magnetUri: torrent.magnetURI magnetUri: torrent.magnetURI
}
ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, to_send)
})
}
function add (data) {
var args = data.args
var magnetUri = args.magnetUri
var _id = data._id
wt.add(magnetUri, function (torrent) {
var to_send = {
files: []
}
torrent.files.forEach(function (file) {
to_send.files.push({ path: file.path })
})
ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, to_send)
})
}
function remove (data) {
var args = data.args
var magnetUri = args.magnetUri
var _id = data._id
try {
wt.remove(magnetUri, callback)
} catch (err) {
console.log('Cannot remove the torrent from WebTorrent.')
return callback(null)
} }
function callback () { ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, to_send)
var to_send = {}
ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, to_send)
}
}
console.log('Configuration: ' + host + ':' + port)
console.log('Connecting to IPC...')
ipc.connectTo(nodeKey, function () {
ipc.of[nodeKey].on(processKey + '.seed', seed)
ipc.of[nodeKey].on(processKey + '.add', add)
ipc.of[nodeKey].on(processKey + '.remove', remove)
ipc.of[nodeKey].emit(processKey + '.ready')
console.log('Ready.')
})
process.on('uncaughtException', function (e) {
ipc.of[nodeKey].emit(processKey + '.exception', { exception: e })
}) })
} }
// --------------------------------------------------------------------------- function add (data) {
var args = data.args
var magnetUri = args.magnetUri
var _id = data._id
module.exports = webtorrent wt.add(magnetUri, function (torrent) {
})() var to_send = {
files: []
}
torrent.files.forEach(function (file) {
to_send.files.push({ path: file.path })
})
ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, to_send)
})
}
function remove (data) {
var args = data.args
var magnetUri = args.magnetUri
var _id = data._id
try {
wt.remove(magnetUri, callback)
} catch (err) {
console.log('Cannot remove the torrent from WebTorrent.')
return callback(null)
}
function callback () {
var to_send = {}
ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, to_send)
}
}
console.log('Configuration: ' + host + ':' + port)
console.log('Connecting to IPC...')
ipc.connectTo(nodeKey, function () {
ipc.of[nodeKey].on(processKey + '.seed', seed)
ipc.of[nodeKey].on(processKey + '.add', add)
ipc.of[nodeKey].on(processKey + '.remove', remove)
ipc.of[nodeKey].emit(processKey + '.ready')
console.log('Ready.')
})
process.on('uncaughtException', function (e) {
ipc.of[nodeKey].emit(processKey + '.exception', { exception: e })
})
}
// ---------------------------------------------------------------------------
module.exports = webtorrent

View File

@ -1,25 +1,23 @@
;(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
if (cache === true || (cache !== false && process.env.NODE_ENV === 'production')) { if (cache === true || (cache !== false && process.env.NODE_ENV === 'production')) {
res.setHeader('Cache-Control', 'public') res.setHeader('Cache-Control', 'public')
} else { } else {
res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate') res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
}
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,41 +1,39 @@
;(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 })
res.sendStatus(500) res.sendStatus(500)
} }
if (has_friends === true) { if (has_friends === true) {
// We need to quit our friends before make new ones // We need to quit our friends before make new ones
res.sendStatus(409) res.sendStatus(409)
} else { } else {
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,45 +1,43 @@
;(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()
req.checkBody('data', 'Should have data').notEmpty() req.checkBody('data', 'Should have data').notEmpty()
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,27 +1,25 @@
;(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) {
if (status_code === undefined) status_code = 400
var errors = req.validationErrors()
if (errors) {
logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors })
return res.status(status_code).send('There have been validation errors: ' + util.inspect(errors))
} }
function checkErrors (req, res, next, status_code) { return next()
if (status_code === undefined) status_code = 400 }
var errors = req.validationErrors()
if (errors) { // ---------------------------------------------------------------------------
logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors })
return res.status(status_code).send('There have been validation errors: ' + util.inspect(errors))
}
return next() module.exports = reqValidatorsUtils
}
// ---------------------------------------------------------------------------
module.exports = reqValidatorsUtils
})()

View File

@ -1,76 +1,74 @@
;(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)
req.checkBody('description', 'Should have a description').isLength(1, 250) req.checkBody('description', 'Should have a description').isLength(1, 250)
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 })
checkErrors(req, res, function () { checkErrors(req, res, function () {
Videos.getVideoState(req.params.id, function (err, state) { Videos.getVideoState(req.params.id, function (err, state) {
if (err) { if (err) {
logger.error('Error in videosGet request validator.', { error: err }) logger.error('Error in videosGet request validator.', { error: err })
res.sendStatus(500) res.sendStatus(500)
} }
if (state.exist === false) return res.status(404).send('Video not found') if (state.exist === false) return res.status(404).send('Video not found')
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 })
checkErrors(req, res, function () { checkErrors(req, res, function () {
Videos.getVideoState(req.params.id, function (err, state) { Videos.getVideoState(req.params.id, function (err, state) {
if (err) { if (err) {
logger.error('Error in videosRemove request validator.', { error: err }) logger.error('Error in videosRemove request validator.', { error: err })
res.sendStatus(500) res.sendStatus(500)
} }
if (state.exist === false) return res.status(404).send('Video not found') if (state.exist === false) return res.status(404).send('Video not found')
else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod') else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod')
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,51 +1,49 @@
;(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) {
logger.error('Cannot get signed url in decryptBody.', { error: err }) logger.error('Cannot get signed url in decryptBody.', { error: err })
return res.sendStatus(500) return res.sendStatus(500)
} }
if (pod === null) { if (pod === null) {
logger.error('Unknown pod %s.', url) logger.error('Unknown pod %s.', url)
return res.sendStatus(403) return res.sendStatus(403)
} }
logger.debug('Decrypting body from %s.', url) logger.debug('Decrypting body from %s.', url)
var signature_ok = peertubeCrypto.checkSignature(pod.publicKey, url, req.body.signature.signature) var signature_ok = peertubeCrypto.checkSignature(pod.publicKey, url, req.body.signature.signature)
if (signature_ok === true) { if (signature_ok === true) {
peertubeCrypto.decrypt(req.body.key, req.body.data, function (err, decrypted) { peertubeCrypto.decrypt(req.body.key, req.body.data, function (err, decrypted) {
if (err) { if (err) {
logger.error('Cannot decrypt data.', { error: err }) logger.error('Cannot decrypt data.', { error: err })
return res.sendStatus(500) return res.sendStatus(500)
} }
req.body.data = JSON.parse(decrypted) req.body.data = JSON.parse(decrypted)
delete req.body.key delete req.body.key
next() next()
}) })
} else { } else {
logger.error('Signature is not okay in decryptBody for %s.', req.body.signature.url) logger.error('Signature is not okay in decryptBody for %s.', req.body.signature.url)
return res.sendStatus(403) return res.sendStatus(403)
} }
}) })
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = secureMiddleware module.exports = secureMiddleware
})()

View File

@ -1,90 +1,88 @@
;(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,
findBadPods: findBadPods, findBadPods: findBadPods,
incrementScores: incrementScores, incrementScores: incrementScores,
list: list, list: list,
remove: remove, remove: remove,
removeAll: removeAll, removeAll: removeAll,
removeAllByIds: removeAllByIds removeAllByIds: removeAllByIds
}
// TODO: check if the pod is not already a friend
function add (data, callback) {
if (!callback) callback = function () {}
var params = {
url: data.url,
publicKey: data.publicKey,
score: constants.FRIEND_BASE_SCORE
} }
// TODO: check if the pod is not already a friend PodsDB.create(params, callback)
function add (data, callback) { }
if (!callback) callback = function () {}
var params = { function count (callback) {
url: data.url, return PodsDB.count(callback)
publicKey: data.publicKey, }
score: constants.FRIEND_BASE_SCORE
function findBadPods (callback) {
PodsDB.find({ score: 0 }, callback)
}
function findByUrl (url, callback) {
PodsDB.findOne({ url: url }, callback)
}
function incrementScores (ids, value, callback) {
if (!callback) callback = function () {}
PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
}
function list (callback) {
PodsDB.find(function (err, pods_list) {
if (err) {
logger.error('Cannot get the list of the pods.')
return callback(err)
} }
PodsDB.create(params, callback) return callback(null, pods_list)
} })
}
function count (callback) { function remove (url, callback) {
return PodsDB.count(callback) if (!callback) callback = function () {}
} PodsDB.remove({ url: url }, callback)
}
function findBadPods (callback) { function removeAll (callback) {
PodsDB.find({ score: 0 }, callback) if (!callback) callback = function () {}
} PodsDB.remove(callback)
}
function findByUrl (url, callback) { function removeAllByIds (ids, callback) {
PodsDB.findOne({ url: url }, callback) if (!callback) callback = function () {}
} PodsDB.remove({ _id: { $in: ids } }, callback)
}
function incrementScores (ids, value, callback) { // ---------------------------------------------------------------------------
if (!callback) callback = function () {}
PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
}
function list (callback) { module.exports = Pods
PodsDB.find(function (err, pods_list) {
if (err) {
logger.error('Cannot get the list of the pods.')
return callback(err)
}
return callback(null, pods_list)
})
}
function remove (url, callback) {
if (!callback) callback = function () {}
PodsDB.remove({ url: url }, callback)
}
function removeAll (callback) {
if (!callback) callback = function () {}
PodsDB.remove(callback)
}
function removeAllByIds (ids, callback) {
if (!callback) callback = function () {}
PodsDB.remove({ _id: { $in: ids } }, callback)
}
// ---------------------------------------------------------------------------
module.exports = Pods
})()

View File

@ -1,57 +1,55 @@
;(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 PoolRequests = {
create: create,
findById: findById,
list: list,
removeRequestById: removeRequestById,
removeRequests: removeRequests
}
function create (id, type, request, callback) {
PoolRequestsDB.create({ id: id, type: type, request: request }, callback)
}
function findById (id, callback) {
PoolRequestsDB.findOne({ id: id }, callback)
}
function list (callback) {
PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback)
}
function removeRequestById (id, callback) {
PoolRequestsDB.remove({ id: id }, callback)
}
function removeRequests (ids) {
PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
if (err) {
logger.error('Cannot remove requests from the pool requests database.', { error: err })
return // Abort
}
logger.info('Pool requests flushed.')
}) })
var PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema) }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
var PoolRequests = { module.exports = PoolRequests
create: create,
findById: findById,
list: list,
removeRequestById: removeRequestById,
removeRequests: removeRequests
}
function create (id, type, request, callback) {
PoolRequestsDB.create({ id: id, type: type, request: request }, callback)
}
function findById (id, callback) {
PoolRequestsDB.findOne({ id: id }, callback)
}
function list (callback) {
PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback)
}
function removeRequestById (id, callback) {
PoolRequestsDB.remove({ id: id }, callback)
}
function removeRequests (ids) {
PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
if (err) {
logger.error('Cannot remove requests from the pool requests database.', { error: err })
return // Abort
}
logger.info('Pool requests flushed.')
})
}
// ---------------------------------------------------------------------------
module.exports = PoolRequests
})()

View File

@ -1,237 +1,235 @@
;(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 Videos = {
add: add,
addRemotes: addRemotes,
get: get,
getVideoState: getVideoState,
isOwned: isOwned,
list: list,
listOwned: listOwned,
removeOwned: removeOwned,
removeAllRemotes: removeAllRemotes,
removeAllRemotesOf: removeAllRemotesOf,
removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris,
search: search
}
function add (video, callback) {
logger.info('Adding %s video to database.', video.name)
var params = video
params.podUrl = http + '://' + host + ':' + port
VideosDB.create(params, function (err, video) {
if (err) {
logger.error('Cannot insert this video into database.')
return callback(err)
}
callback(null)
}) })
var VideosDB = mongoose.model('videos', videosSchema) }
// --------------------------------------------------------------------------- // TODO: avoid doublons
function addRemotes (videos, callback) {
if (!callback) callback = function () {}
var Videos = { var to_add = []
add: add,
addRemotes: addRemotes,
get: get,
getVideoState: getVideoState,
isOwned: isOwned,
list: list,
listOwned: listOwned,
removeOwned: removeOwned,
removeAllRemotes: removeAllRemotes,
removeAllRemotesOf: removeAllRemotesOf,
removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris,
search: search
}
function add (video, callback) { async.each(videos, function (video, callback_each) {
logger.info('Adding %s video to database.', video.name) callback_each = dz(callback_each)
logger.debug('Add remote video from pod: %s', video.podUrl)
var params = video var params = {
params.podUrl = http + '://' + host + ':' + port name: video.name,
namePath: null,
description: video.description,
magnetUri: video.magnetUri,
podUrl: video.podUrl
}
VideosDB.create(params, function (err, video) { to_add.push(params)
callback_each()
}, function () {
VideosDB.create(to_add, function (err, videos) {
if (err) { if (err) {
logger.error('Cannot insert this video into database.') logger.error('Cannot insert this remote video.')
return callback(err)
}
callback(null)
})
}
// TODO: avoid doublons
function addRemotes (videos, callback) {
if (!callback) callback = function () {}
var to_add = []
async.each(videos, function (video, callback_each) {
callback_each = dz(callback_each)
logger.debug('Add remote video from pod: %s', video.podUrl)
var params = {
name: video.name,
namePath: null,
description: video.description,
magnetUri: video.magnetUri,
podUrl: video.podUrl
}
to_add.push(params)
callback_each()
}, function () {
VideosDB.create(to_add, function (err, videos) {
if (err) {
logger.error('Cannot insert this remote video.')
return callback(err)
}
return callback(null, videos)
})
})
}
function get (id, callback) {
VideosDB.findById(id, function (err, video) {
if (err) {
logger.error('Cannot get this video.')
return callback(err)
}
return callback(null, video)
})
}
function getVideoState (id, callback) {
get(id, function (err, video) {
if (err) return callback(err)
var exist = (video !== null)
var owned = false
if (exist === true) {
owned = (video.namePath !== null)
}
return callback(null, { exist: exist, owned: owned })
})
}
function isOwned (id, callback) {
VideosDB.findById(id, function (err, video) {
if (err || !video) {
if (!err) err = new Error('Cannot find this video.')
logger.error('Cannot find this video.')
return callback(err)
}
if (video.namePath === null) {
var error_string = 'Cannot remove the video of another pod.'
logger.error(error_string)
return callback(new Error(error_string), false, video)
}
callback(null, true, video)
})
}
function list (callback) {
VideosDB.find(function (err, videos_list) {
if (err) {
logger.error('Cannot get the list of the videos.')
return callback(err)
}
return callback(null, videos_list)
})
}
function listOwned (callback) {
// If namePath is not null this is *our* video
VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
if (err) {
logger.error('Cannot get the list of owned videos.')
return callback(err)
}
return callback(null, videos_list)
})
}
function removeOwned (id, callback) {
VideosDB.findByIdAndRemove(id, function (err, video) {
if (err) {
logger.error('Cannot remove the torrent.')
return callback(err)
}
fs.unlink(uploadDir + video.namePath, function (err) {
if (err) {
logger.error('Cannot remove this video file.')
return callback(err)
}
callback(null)
})
})
}
function removeAllRemotes (callback) {
VideosDB.remove({ namePath: null }, callback)
}
function removeAllRemotesOf (fromUrl, callback) {
// TODO { podUrl: { $in: urls } }
VideosDB.remove({ podUrl: fromUrl }, callback)
}
// Use the magnet Uri because the _id field is not the same on different servers
function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) {
if (callback === undefined) callback = function () {}
VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
if (err || !videos) {
logger.error('Cannot find the torrent URI of these remote videos.')
return callback(err)
}
var to_remove = []
async.each(videos, function (video, callback_async) {
callback_async = dz(callback_async)
if (video.podUrl !== fromUrl) {
logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl)
} else {
to_remove.push(video._id)
}
callback_async()
}, function () {
VideosDB.remove({ _id: { $in: to_remove } }, function (err) {
if (err) {
logger.error('Cannot remove the remote videos.')
return callback(err)
}
logger.info('Removed remote videos from %s.', fromUrl)
callback(null)
})
})
})
}
function search (name, callback) {
VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
if (err) {
logger.error('Cannot search the videos.')
return callback(err) return callback(err)
} }
return callback(null, videos) return callback(null, videos)
}) })
} })
}
// --------------------------------------------------------------------------- function get (id, callback) {
VideosDB.findById(id, function (err, video) {
if (err) {
logger.error('Cannot get this video.')
return callback(err)
}
module.exports = Videos return callback(null, video)
})() })
}
function getVideoState (id, callback) {
get(id, function (err, video) {
if (err) return callback(err)
var exist = (video !== null)
var owned = false
if (exist === true) {
owned = (video.namePath !== null)
}
return callback(null, { exist: exist, owned: owned })
})
}
function isOwned (id, callback) {
VideosDB.findById(id, function (err, video) {
if (err || !video) {
if (!err) err = new Error('Cannot find this video.')
logger.error('Cannot find this video.')
return callback(err)
}
if (video.namePath === null) {
var error_string = 'Cannot remove the video of another pod.'
logger.error(error_string)
return callback(new Error(error_string), false, video)
}
callback(null, true, video)
})
}
function list (callback) {
VideosDB.find(function (err, videos_list) {
if (err) {
logger.error('Cannot get the list of the videos.')
return callback(err)
}
return callback(null, videos_list)
})
}
function listOwned (callback) {
// If namePath is not null this is *our* video
VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
if (err) {
logger.error('Cannot get the list of owned videos.')
return callback(err)
}
return callback(null, videos_list)
})
}
function removeOwned (id, callback) {
VideosDB.findByIdAndRemove(id, function (err, video) {
if (err) {
logger.error('Cannot remove the torrent.')
return callback(err)
}
fs.unlink(uploadDir + video.namePath, function (err) {
if (err) {
logger.error('Cannot remove this video file.')
return callback(err)
}
callback(null)
})
})
}
function removeAllRemotes (callback) {
VideosDB.remove({ namePath: null }, callback)
}
function removeAllRemotesOf (fromUrl, callback) {
// TODO { podUrl: { $in: urls } }
VideosDB.remove({ podUrl: fromUrl }, callback)
}
// Use the magnet Uri because the _id field is not the same on different servers
function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) {
if (callback === undefined) callback = function () {}
VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
if (err || !videos) {
logger.error('Cannot find the torrent URI of these remote videos.')
return callback(err)
}
var to_remove = []
async.each(videos, function (video, callback_async) {
callback_async = dz(callback_async)
if (video.podUrl !== fromUrl) {
logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl)
} else {
to_remove.push(video._id)
}
callback_async()
}, function () {
VideosDB.remove({ _id: { $in: to_remove } }, function (err) {
if (err) {
logger.error('Cannot remove the remote videos.')
return callback(err)
}
logger.info('Removed remote videos from %s.', fromUrl)
callback(null)
})
})
})
}
function search (name, callback) {
VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
if (err) {
logger.error('Cannot search the videos.')
return callback(err)
}
return callback(null, videos)
})
}
// ---------------------------------------------------------------------------
module.exports = Videos

View File

@ -1,302 +1,300 @@
;(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 = ''
function makePostRequest (path, fields, attach, done, fail) { function makePostRequest (path, fields, attach, done, fail) {
var status_code = 400 var status_code = 400
if (fail !== undefined && fail === false) status_code = 200 if (fail !== undefined && fail === false) status_code = 200
var req = request(url) var req = request(url)
.post(path) .post(path)
.set('Accept', 'application/json') .set('Accept', 'application/json')
Object.keys(fields).forEach(function (field) { Object.keys(fields).forEach(function (field) {
var value = fields[field] var value = fields[field]
req.field(field, value) req.field(field, value)
})
req.expect(status_code, done)
}
function makePostBodyRequest (path, fields, done, fail) {
var status_code = 400
if (fail !== undefined && fail === false) status_code = 200
request(url)
.post(path)
.set('Accept', 'application/json')
.send(fields)
.expect(status_code, done)
}
// ---------------------------------------------------------------
before(function (done) {
this.timeout(20000)
async.series([
function (next) {
utils.flushTests(next)
},
function (next) {
utils.runServer(1, function (app1, url1) {
app = app1
url = url1
next()
})
}
], done)
}) })
describe('Of the pods API', function () { req.expect(status_code, done)
var path = '/api/v1/pods/' }
describe('When adding a pod', function () { function makePostBodyRequest (path, fields, done, fail) {
it('Should fail with nothing', function (done) { var status_code = 400
var data = {} if (fail !== undefined && fail === false) status_code = 200
makePostBodyRequest(path, data, done)
request(url)
.post(path)
.set('Accept', 'application/json')
.send(fields)
.expect(status_code, done)
}
// ---------------------------------------------------------------
before(function (done) {
this.timeout(20000)
async.series([
function (next) {
utils.flushTests(next)
},
function (next) {
utils.runServer(1, function (app1, url1) {
app = app1
url = url1
next()
}) })
it('Should fail without public key', function (done) {
var data = {
data: {
url: 'http://coucou.com'
}
}
makePostBodyRequest(path, data, done)
})
it('Should fail without an url', function (done) {
var data = {
data: {
publicKey: 'mysuperpublickey'
}
}
makePostBodyRequest(path, data, done)
})
it('Should fail with an incorrect url', function (done) {
var data = {
data: {
url: 'coucou.com',
publicKey: 'mysuperpublickey'
}
}
makePostBodyRequest(path, data, function () {
data.data.url = 'http://coucou'
makePostBodyRequest(path, data, function () {
data.data.url = 'coucou'
makePostBodyRequest(path, data, done)
})
})
})
it('Should succeed with the correct parameters', function (done) {
var data = {
data: {
url: 'http://coucou.com',
publicKey: 'mysuperpublickey'
}
}
makePostBodyRequest(path, data, done, false)
})
})
})
describe('Of the videos API', function () {
var path = '/api/v1/videos/'
describe('When searching a video', function () {
it('Should fail with nothing', function (done) {
request(url)
.get(pathUtils.join(path, 'search'))
.set('Accept', 'application/json')
.expect(400, done)
})
})
describe('When adding a video', function () {
it('Should fail with nothing', function (done) {
var data = {}
var attach = {}
makePostRequest(path, data, attach, done)
})
it('Should fail without name', function (done) {
var data = {
description: 'my super description'
}
var attach = {
'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
}
makePostRequest(path, data, attach, done)
})
it('Should fail with a long name', function (done) {
var data = {
name: 'My very very very very very very very very very very very very very very very very long name',
description: 'my super description'
}
var attach = {
'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
}
makePostRequest(path, data, attach, done)
})
it('Should fail without description', function (done) {
var data = {
name: 'my super name'
}
var attach = {
'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
}
makePostRequest(path, data, attach, done)
})
it('Should fail with a long description', function (done) {
var data = {
name: 'my super name',
description: 'my super description which is very very very very very very very very very very very very very very' +
'very very very very very very very very very very very very very very very very very very very very very' +
'very very very very very very very very very very very very very very very long'
}
var attach = {
'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
}
makePostRequest(path, data, attach, done)
})
it('Should fail without an input file', function (done) {
var data = {
name: 'my super name',
description: 'my super description'
}
var attach = {}
makePostRequest(path, data, attach, done)
})
it('Should fail without an incorrect input file', function (done) {
var data = {
name: 'my super name',
description: 'my super description'
}
var attach = {
'input_video': pathUtils.join(__dirname, '..', 'fixtures', 'video_short_fake.webm')
}
makePostRequest(path, data, attach, done)
})
it('Should succeed with the correct parameters', function (done) {
var data = {
name: 'my super name',
description: 'my super description'
}
var attach = {
'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
}
makePostRequest(path, data, attach, function () {
attach.input_video = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4')
makePostRequest(path, data, attach, function () {
attach.input_video = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv')
makePostRequest(path, data, attach, done, true)
}, true)
}, true)
})
})
describe('When getting a video', function () {
it('Should return the list of the videos with nothing', function (done) {
request(url)
.get(path)
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(0)
done()
})
})
it('Should fail without a mongodb id', function (done) {
request(url)
.get(path + 'coucou')
.set('Accept', 'application/json')
.expect(400, done)
})
it('Should return 404 with an incorrect video', function (done) {
request(url)
.get(path + '123456789012345678901234')
.set('Accept', 'application/json')
.expect(404, done)
})
it('Should succeed with the correct parameters')
})
describe('When removing a video', function () {
it('Should have 404 with nothing', function (done) {
request(url)
.delete(path)
.expect(404, done)
})
it('Should fail without a mongodb id', function (done) {
request(url)
.delete(path + 'hello')
.expect(400, done)
})
it('Should fail with a video which does not exist', function (done) {
request(url)
.delete(path + '123456789012345678901234')
.expect(404, done)
})
it('Should fail with a video of another pod')
it('Should succeed with the correct parameters')
})
})
describe('Of the remote videos API', function () {
describe('When making a secure request', function () {
it('Should check a secure request')
})
describe('When adding a video', function () {
it('Should check when adding a video')
})
describe('When removing a video', function () {
it('Should check when removing a video')
})
})
after(function (done) {
process.kill(-app.pid)
// Keep the logs if the test failed
if (this.ok) {
utils.flushTests(done)
} else {
done()
} }
], done)
})
describe('Of the pods API', function () {
var path = '/api/v1/pods/'
describe('When adding a pod', function () {
it('Should fail with nothing', function (done) {
var data = {}
makePostBodyRequest(path, data, done)
})
it('Should fail without public key', function (done) {
var data = {
data: {
url: 'http://coucou.com'
}
}
makePostBodyRequest(path, data, done)
})
it('Should fail without an url', function (done) {
var data = {
data: {
publicKey: 'mysuperpublickey'
}
}
makePostBodyRequest(path, data, done)
})
it('Should fail with an incorrect url', function (done) {
var data = {
data: {
url: 'coucou.com',
publicKey: 'mysuperpublickey'
}
}
makePostBodyRequest(path, data, function () {
data.data.url = 'http://coucou'
makePostBodyRequest(path, data, function () {
data.data.url = 'coucou'
makePostBodyRequest(path, data, done)
})
})
})
it('Should succeed with the correct parameters', function (done) {
var data = {
data: {
url: 'http://coucou.com',
publicKey: 'mysuperpublickey'
}
}
makePostBodyRequest(path, data, done, false)
})
}) })
}) })
})()
describe('Of the videos API', function () {
var path = '/api/v1/videos/'
describe('When searching a video', function () {
it('Should fail with nothing', function (done) {
request(url)
.get(pathUtils.join(path, 'search'))
.set('Accept', 'application/json')
.expect(400, done)
})
})
describe('When adding a video', function () {
it('Should fail with nothing', function (done) {
var data = {}
var attach = {}
makePostRequest(path, data, attach, done)
})
it('Should fail without name', function (done) {
var data = {
description: 'my super description'
}
var attach = {
'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
}
makePostRequest(path, data, attach, done)
})
it('Should fail with a long name', function (done) {
var data = {
name: 'My very very very very very very very very very very very very very very very very long name',
description: 'my super description'
}
var attach = {
'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
}
makePostRequest(path, data, attach, done)
})
it('Should fail without description', function (done) {
var data = {
name: 'my super name'
}
var attach = {
'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
}
makePostRequest(path, data, attach, done)
})
it('Should fail with a long description', function (done) {
var data = {
name: 'my super name',
description: 'my super description which is very very very very very very very very very very very very very very' +
'very very very very very very very very very very very very very very very very very very very very very' +
'very very very very very very very very very very very very very very very long'
}
var attach = {
'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
}
makePostRequest(path, data, attach, done)
})
it('Should fail without an input file', function (done) {
var data = {
name: 'my super name',
description: 'my super description'
}
var attach = {}
makePostRequest(path, data, attach, done)
})
it('Should fail without an incorrect input file', function (done) {
var data = {
name: 'my super name',
description: 'my super description'
}
var attach = {
'input_video': pathUtils.join(__dirname, '..', 'fixtures', 'video_short_fake.webm')
}
makePostRequest(path, data, attach, done)
})
it('Should succeed with the correct parameters', function (done) {
var data = {
name: 'my super name',
description: 'my super description'
}
var attach = {
'input_video': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
}
makePostRequest(path, data, attach, function () {
attach.input_video = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4')
makePostRequest(path, data, attach, function () {
attach.input_video = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv')
makePostRequest(path, data, attach, done, true)
}, true)
}, true)
})
})
describe('When getting a video', function () {
it('Should return the list of the videos with nothing', function (done) {
request(url)
.get(path)
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(0)
done()
})
})
it('Should fail without a mongodb id', function (done) {
request(url)
.get(path + 'coucou')
.set('Accept', 'application/json')
.expect(400, done)
})
it('Should return 404 with an incorrect video', function (done) {
request(url)
.get(path + '123456789012345678901234')
.set('Accept', 'application/json')
.expect(404, done)
})
it('Should succeed with the correct parameters')
})
describe('When removing a video', function () {
it('Should have 404 with nothing', function (done) {
request(url)
.delete(path)
.expect(404, done)
})
it('Should fail without a mongodb id', function (done) {
request(url)
.delete(path + 'hello')
.expect(400, done)
})
it('Should fail with a video which does not exist', function (done) {
request(url)
.delete(path + '123456789012345678901234')
.expect(404, done)
})
it('Should fail with a video of another pod')
it('Should succeed with the correct parameters')
})
})
describe('Of the remote videos API', function () {
describe('When making a secure request', function () {
it('Should check a secure request')
})
describe('When adding a video', function () {
it('Should check when adding a video')
})
describe('When removing a video', function () {
it('Should check when removing a video')
})
})
after(function (done) {
process.kill(-app.pid)
// Keep the logs if the test failed
if (this.ok) {
utils.flushTests(done)
} else {
done()
}
})
})

View File

@ -1,252 +1,250 @@
;(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 = []
function makeFriends (pod_number, callback) { function makeFriends (pod_number, callback) {
return utils.makeFriends(urls[pod_number - 1], callback) return utils.makeFriends(urls[pod_number - 1], callback)
} }
function quitFriends (pod_number, callback) { function quitFriends (pod_number, callback) {
return utils.quitFriends(urls[pod_number - 1], callback) return utils.quitFriends(urls[pod_number - 1], callback)
} }
function getFriendsList (pod_number, end) { function getFriendsList (pod_number, end) {
return utils.getFriendsList(urls[pod_number - 1], end) return utils.getFriendsList(urls[pod_number - 1], end)
} }
function uploadVideo (pod_number, callback) { function uploadVideo (pod_number, callback) {
var name = 'my super video' var name = 'my super video'
var description = 'my super description' var description = 'my super description'
var fixture = 'video_short.webm' var fixture = 'video_short.webm'
return utils.uploadVideo(urls[pod_number - 1], name, description, fixture, callback) return utils.uploadVideo(urls[pod_number - 1], name, description, fixture, callback)
} }
function getVideos (pod_number, callback) { function getVideos (pod_number, callback) {
return utils.getVideosList(urls[pod_number - 1], callback) return utils.getVideosList(urls[pod_number - 1], callback)
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
before(function (done) { before(function (done) {
this.timeout(30000) this.timeout(30000)
utils.flushAndRunMultipleServers(6, function (apps_run, urls_run) { utils.flushAndRunMultipleServers(6, function (apps_run, urls_run) {
apps = apps_run apps = apps_run
urls = urls_run urls = urls_run
done() done()
})
}) })
})
it('Should make friends with two pod each in a different group', function (done) { it('Should make friends with two pod each in a different group', function (done) {
this.timeout(20000) this.timeout(20000)
async.series([ async.series([
// Pod 3 makes friend with the first one // Pod 3 makes friend with the first one
function (next) { function (next) {
makeFriends(3, next) makeFriends(3, next)
}, },
// Pod 4 makes friend with the second one // Pod 4 makes friend with the second one
function (next) { function (next) {
makeFriends(4, next) makeFriends(4, next)
}, },
// Now if the fifth wants to make friends with the third et the first // Now if the fifth wants to make friends with the third et the first
function (next) { function (next) {
makeFriends(5, next) makeFriends(5, next)
}, },
function (next) { function (next) {
setTimeout(next, 11000) setTimeout(next, 11000)
}], }],
function (err) { function (err) {
if (err) throw err
// It should have 0 friends
getFriendsList(5, function (err, res) {
if (err) throw err if (err) throw err
// It should have 0 friends expect(res.body.length).to.equal(0)
getFriendsList(5, function (err, res) {
done()
})
}
)
})
it('Should quit all friends', function (done) {
this.timeout(10000)
async.series([
function (next) {
quitFriends(1, next)
},
function (next) {
quitFriends(2, next)
}],
function (err) {
if (err) throw err
async.each([ 1, 2, 3, 4, 5, 6 ], function (i, callback) {
getFriendsList(i, function (err, res) {
if (err) throw err if (err) throw err
expect(res.body.length).to.equal(0) expect(res.body.length).to.equal(0)
done() callback()
}) })
} }, done)
)
})
it('Should quit all friends', function (done) {
this.timeout(10000)
async.series([
function (next) {
quitFriends(1, next)
},
function (next) {
quitFriends(2, next)
}],
function (err) {
if (err) throw err
async.each([ 1, 2, 3, 4, 5, 6 ], function (i, callback) {
getFriendsList(i, function (err, res) {
if (err) throw err
expect(res.body.length).to.equal(0)
callback()
})
}, done)
}
)
})
it('Should make friends with the pods 1, 2, 3', function (done) {
this.timeout(150000)
async.series([
// Pods 1, 2, 3 and 4 become friends
function (next) {
makeFriends(2, next)
},
function (next) {
makeFriends(1, next)
},
function (next) {
makeFriends(4, next)
},
// Kill pod 4
function (next) {
apps[3].kill()
next()
},
// Expulse pod 4 from pod 1 and 2
function (next) {
uploadVideo(1, next)
},
function (next) {
uploadVideo(2, next)
},
function (next) {
setTimeout(next, 11000)
},
function (next) {
uploadVideo(1, next)
},
function (next) {
uploadVideo(2, next)
},
function (next) {
setTimeout(next, 20000)
},
// Rerun server 4
function (next) {
utils.runServer(4, function (app, url) {
apps[3] = app
next()
})
},
function (next) {
getFriendsList(4, function (err, res) {
if (err) throw err
// Pod 4 didn't know pod 1 and 2 removed it
expect(res.body.length).to.equal(3)
next()
})
},
// Pod 6 ask pod 1, 2 and 3
function (next) {
makeFriends(6, next)
}],
function (err) {
if (err) throw err
getFriendsList(6, function (err, res) {
if (err) throw err
// Pod 4 should not be our friend
var result = res.body
expect(result.length).to.equal(3)
for (var pod of result) {
expect(pod.url).not.equal(urls[3])
}
done()
})
}
)
})
it('Should pod 1 quit friends', function (done) {
this.timeout(25000)
async.series([
// Upload a video on server 3 for aditionnal tests
function (next) {
uploadVideo(3, next)
},
function (next) {
setTimeout(next, 15000)
},
function (next) {
quitFriends(1, next)
},
// Remove pod 1 from pod 2
function (next) {
getVideos(1, function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(2)
next()
})
}],
function (err) {
if (err) throw err
getVideos(2, function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(3)
done()
})
}
)
})
it('Should make friends between pod 1 and 2 and exchange their videos', function (done) {
this.timeout(20000)
makeFriends(1, function () {
setTimeout(function () {
getVideos(1, function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(5)
done()
})
}, 5000)
})
})
after(function (done) {
apps.forEach(function (app) {
process.kill(-app.pid)
})
if (this.ok) {
utils.flushTests(done)
} else {
done()
} }
)
})
it('Should make friends with the pods 1, 2, 3', function (done) {
this.timeout(150000)
async.series([
// Pods 1, 2, 3 and 4 become friends
function (next) {
makeFriends(2, next)
},
function (next) {
makeFriends(1, next)
},
function (next) {
makeFriends(4, next)
},
// Kill pod 4
function (next) {
apps[3].kill()
next()
},
// Expulse pod 4 from pod 1 and 2
function (next) {
uploadVideo(1, next)
},
function (next) {
uploadVideo(2, next)
},
function (next) {
setTimeout(next, 11000)
},
function (next) {
uploadVideo(1, next)
},
function (next) {
uploadVideo(2, next)
},
function (next) {
setTimeout(next, 20000)
},
// Rerun server 4
function (next) {
utils.runServer(4, function (app, url) {
apps[3] = app
next()
})
},
function (next) {
getFriendsList(4, function (err, res) {
if (err) throw err
// Pod 4 didn't know pod 1 and 2 removed it
expect(res.body.length).to.equal(3)
next()
})
},
// Pod 6 ask pod 1, 2 and 3
function (next) {
makeFriends(6, next)
}],
function (err) {
if (err) throw err
getFriendsList(6, function (err, res) {
if (err) throw err
// Pod 4 should not be our friend
var result = res.body
expect(result.length).to.equal(3)
for (var pod of result) {
expect(pod.url).not.equal(urls[3])
}
done()
})
}
)
})
it('Should pod 1 quit friends', function (done) {
this.timeout(25000)
async.series([
// Upload a video on server 3 for aditionnal tests
function (next) {
uploadVideo(3, next)
},
function (next) {
setTimeout(next, 15000)
},
function (next) {
quitFriends(1, next)
},
// Remove pod 1 from pod 2
function (next) {
getVideos(1, function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(2)
next()
})
}],
function (err) {
if (err) throw err
getVideos(2, function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(3)
done()
})
}
)
})
it('Should make friends between pod 1 and 2 and exchange their videos', function (done) {
this.timeout(20000)
makeFriends(1, function () {
setTimeout(function () {
getVideos(1, function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(5)
done()
})
}, 5000)
}) })
}) })
})()
after(function (done) {
apps.forEach(function (app) {
process.kill(-app.pid)
})
if (this.ok) {
utils.flushTests(done)
} else {
done()
}
})
})

View File

@ -1,187 +1,185 @@
;(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 = []
function testMadeFriends (urls, url_to_test, callback) { function testMadeFriends (urls, url_to_test, callback) {
var friends = [] var friends = []
for (var i = 0; i < urls.length; i++) { for (var i = 0; i < urls.length; i++) {
if (urls[i] === url_to_test) continue if (urls[i] === url_to_test) continue
friends.push(urls[i]) friends.push(urls[i])
} }
utils.getFriendsList(url_to_test, function (err, res) { utils.getFriendsList(url_to_test, function (err, res) {
if (err) throw err
var result = res.body
var result_urls = [ result[0].url, result[1].url ]
expect(result).to.be.an('array')
expect(result.length).to.equal(2)
expect(result_urls[0]).to.not.equal(result_urls[1])
var error_string = 'Friends url do not correspond for ' + url_to_test
expect(friends).to.contain(result_urls[0], error_string)
expect(friends).to.contain(result_urls[1], error_string)
callback()
})
}
// ---------------------------------------------------------------
before(function (done) {
this.timeout(20000)
utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) {
apps = apps_run
urls = urls_run
done()
})
})
it('Should not have friends', function (done) {
async.each(urls, function (url, callback) {
utils.getFriendsList(url, function (err, res) {
if (err) throw err if (err) throw err
var result = res.body var result = res.body
var result_urls = [ result[0].url, result[1].url ]
expect(result).to.be.an('array') expect(result).to.be.an('array')
expect(result.length).to.equal(2) expect(result.length).to.equal(0)
expect(result_urls[0]).to.not.equal(result_urls[1])
var error_string = 'Friends url do not correspond for ' + url_to_test
expect(friends).to.contain(result_urls[0], error_string)
expect(friends).to.contain(result_urls[1], error_string)
callback() callback()
}) })
} }, done)
})
// --------------------------------------------------------------- it('Should make friends', function (done) {
this.timeout(10000)
before(function (done) { var path = '/api/v1/pods/makefriends'
this.timeout(20000)
utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) {
apps = apps_run
urls = urls_run
done()
})
})
it('Should not have friends', function (done) { async.series([
// The second pod make friend with the third
function (next) {
request(urls[1])
.get(path)
.set('Accept', 'application/json')
.expect(204)
.end(next)
},
// Wait for the request between pods
function (next) {
setTimeout(next, 1000)
},
// The second pod should have the third as a friend
function (next) {
utils.getFriendsList(urls[1], function (err, res) {
if (err) throw err
var result = res.body
expect(result).to.be.an('array')
expect(result.length).to.equal(1)
expect(result[0].url).to.be.equal(urls[2])
next()
})
},
// Same here, the third pod should have the second pod as a friend
function (next) {
utils.getFriendsList(urls[2], function (err, res) {
if (err) throw err
var result = res.body
expect(result).to.be.an('array')
expect(result.length).to.equal(1)
expect(result[0].url).to.be.equal(urls[1])
next()
})
},
// Finally the first pod make friend with the second pod
function (next) {
request(urls[0])
.get(path)
.set('Accept', 'application/json')
.expect(204)
.end(next)
},
// Wait for the request between pods
function (next) {
setTimeout(next, 1000)
}
],
// Now each pod should be friend with the other ones
function (err) {
if (err) throw err
async.each(urls, function (url, callback) { async.each(urls, function (url, callback) {
utils.getFriendsList(url, function (err, res) { testMadeFriends(urls, url, callback)
}, done)
})
})
it('Should not be allowed to make friend again', function (done) {
utils.makeFriends(urls[1], 409, done)
})
it('Should quit friends of pod 2', function (done) {
async.series([
// Pod 1 quit friends
function (next) {
utils.quitFriends(urls[1], next)
},
// Pod 1 should not have friends anymore
function (next) {
utils.getFriendsList(urls[1], function (err, res) {
if (err) throw err if (err) throw err
var result = res.body var result = res.body
expect(result).to.be.an('array') expect(result).to.be.an('array')
expect(result.length).to.equal(0) expect(result.length).to.equal(0)
callback()
next()
}) })
},
// Other pods shouldn't have pod 1 too
function (next) {
async.each([ urls[0], urls[2] ], function (url, callback) {
utils.getFriendsList(url, function (err, res) {
if (err) throw err
var result = res.body
expect(result).to.be.an('array')
expect(result.length).to.equal(1)
expect(result[0].url).not.to.be.equal(urls[1])
callback()
})
}, next)
}
], done)
})
it('Should allow pod 2 to make friend again', function (done) {
utils.makeFriends(urls[1], function () {
async.each(urls, function (url, callback) {
testMadeFriends(urls, url, callback)
}, done) }, done)
}) })
it('Should make friends', function (done) {
this.timeout(10000)
var path = '/api/v1/pods/makefriends'
async.series([
// The second pod make friend with the third
function (next) {
request(urls[1])
.get(path)
.set('Accept', 'application/json')
.expect(204)
.end(next)
},
// Wait for the request between pods
function (next) {
setTimeout(next, 1000)
},
// The second pod should have the third as a friend
function (next) {
utils.getFriendsList(urls[1], function (err, res) {
if (err) throw err
var result = res.body
expect(result).to.be.an('array')
expect(result.length).to.equal(1)
expect(result[0].url).to.be.equal(urls[2])
next()
})
},
// Same here, the third pod should have the second pod as a friend
function (next) {
utils.getFriendsList(urls[2], function (err, res) {
if (err) throw err
var result = res.body
expect(result).to.be.an('array')
expect(result.length).to.equal(1)
expect(result[0].url).to.be.equal(urls[1])
next()
})
},
// Finally the first pod make friend with the second pod
function (next) {
request(urls[0])
.get(path)
.set('Accept', 'application/json')
.expect(204)
.end(next)
},
// Wait for the request between pods
function (next) {
setTimeout(next, 1000)
}
],
// Now each pod should be friend with the other ones
function (err) {
if (err) throw err
async.each(urls, function (url, callback) {
testMadeFriends(urls, url, callback)
}, done)
})
})
it('Should not be allowed to make friend again', function (done) {
utils.makeFriends(urls[1], 409, done)
})
it('Should quit friends of pod 2', function (done) {
async.series([
// Pod 1 quit friends
function (next) {
utils.quitFriends(urls[1], next)
},
// Pod 1 should not have friends anymore
function (next) {
utils.getFriendsList(urls[1], function (err, res) {
if (err) throw err
var result = res.body
expect(result).to.be.an('array')
expect(result.length).to.equal(0)
next()
})
},
// Other pods shouldn't have pod 1 too
function (next) {
async.each([ urls[0], urls[2] ], function (url, callback) {
utils.getFriendsList(url, function (err, res) {
if (err) throw err
var result = res.body
expect(result).to.be.an('array')
expect(result.length).to.equal(1)
expect(result[0].url).not.to.be.equal(urls[1])
callback()
})
}, next)
}
], done)
})
it('Should allow pod 2 to make friend again', function (done) {
utils.makeFriends(urls[1], function () {
async.each(urls, function (url, callback) {
testMadeFriends(urls, url, callback)
}, done)
})
})
after(function (done) {
apps.forEach(function (app) {
process.kill(-app.pid)
})
if (this.ok) {
utils.flushTests(done)
} else {
done()
}
})
}) })
})()
after(function (done) {
apps.forEach(function (app) {
process.kill(-app.pid)
})
if (this.ok) {
utils.flushTests(done)
} else {
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,330 +1,328 @@
;(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 = []
before(function (done) { before(function (done) {
this.timeout(30000)
async.series([
// Run servers
function (next) {
utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) {
apps = apps_run
urls = urls_run
next()
})
},
// The second pod make friend with the third
function (next) {
utils.makeFriends(urls[1], next)
},
// Wait for the request between pods
function (next) {
setTimeout(next, 10000)
},
// Pod 1 make friends too
function (next) {
utils.makeFriends(urls[0], next)
},
function (next) {
webtorrent.create({ host: 'client', port: '1' }, next)
}
], done)
})
it('Should not have videos for all pods', function (done) {
async.each(urls, function (url, callback) {
utils.getVideosList(url, function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(0)
callback()
})
}, done)
})
describe('Should upload the video and propagate on each pod', function () {
it('Should upload the video on pod 1 and propagate on each pod', function (done) {
this.timeout(15000)
async.series([
function (next) {
utils.uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', next)
},
function (next) {
setTimeout(next, 11000)
}],
// All pods should have this video
function (err) {
if (err) throw err
async.each(urls, function (url, callback) {
var base_magnet = null
utils.getVideosList(url, function (err, res) {
if (err) throw err
var videos = res.body
expect(videos).to.be.an('array')
expect(videos.length).to.equal(1)
var video = videos[0]
expect(video.name).to.equal('my super name for pod 1')
expect(video.description).to.equal('my super description for pod 1')
expect(video.podUrl).to.equal('http://localhost:9001')
expect(video.magnetUri).to.exist
// All pods should have the same magnet Uri
if (base_magnet === null) {
base_magnet = video.magnetUri
} else {
expect(video.magnetUri).to.equal.magnetUri
}
callback()
})
}, done)
}
)
})
it('Should upload the video on pod 2 and propagate on each pod', function (done) {
this.timeout(15000)
async.series([
function (next) {
utils.uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', next)
},
function (next) {
setTimeout(next, 11000)
}],
// All pods should have this video
function (err) {
if (err) throw err
async.each(urls, function (url, callback) {
var base_magnet = null
utils.getVideosList(url, function (err, res) {
if (err) throw err
var videos = res.body
expect(videos).to.be.an('array')
expect(videos.length).to.equal(2)
var video = videos[1]
expect(video.name).to.equal('my super name for pod 2')
expect(video.description).to.equal('my super description for pod 2')
expect(video.podUrl).to.equal('http://localhost:9002')
expect(video.magnetUri).to.exist
// All pods should have the same magnet Uri
if (base_magnet === null) {
base_magnet = video.magnetUri
} else {
expect(video.magnetUri).to.equal.magnetUri
}
callback()
})
}, done)
}
)
})
it('Should upload two videos on pod 3 and propagate on each pod', function (done) {
this.timeout(30000) this.timeout(30000)
async.series([ async.series([
// Run servers
function (next) { function (next) {
utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) { utils.uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', next)
apps = apps_run
urls = urls_run
next()
})
},
// The second pod make friend with the third
function (next) {
utils.makeFriends(urls[1], next)
},
// Wait for the request between pods
function (next) {
setTimeout(next, 10000)
},
// Pod 1 make friends too
function (next) {
utils.makeFriends(urls[0], next)
}, },
function (next) { function (next) {
webtorrent.create({ host: 'client', port: '1' }, next) utils.uploadVideo(urls[2], 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', next)
},
function (next) {
setTimeout(next, 22000)
}],
function (err) {
if (err) throw err
var base_magnet = null
// All pods should have this video
async.each(urls, function (url, callback) {
utils.getVideosList(url, function (err, res) {
if (err) throw err
var videos = res.body
expect(videos).to.be.an('array')
expect(videos.length).to.equal(4)
var video = videos[2]
expect(video.name).to.equal('my super name for pod 3')
expect(video.description).to.equal('my super description for pod 3')
expect(video.podUrl).to.equal('http://localhost:9003')
expect(video.magnetUri).to.exist
video = videos[3]
expect(video.name).to.equal('my super name for pod 3-2')
expect(video.description).to.equal('my super description for pod 3-2')
expect(video.podUrl).to.equal('http://localhost:9003')
expect(video.magnetUri).to.exist
// All pods should have the same magnet Uri
if (base_magnet === null) {
base_magnet = video.magnetUri
} else {
expect(video.magnetUri).to.equal.magnetUri
}
callback()
})
}, done)
} }
], done) )
})
})
describe('Should seed the uploaded video', function () {
it('Should add the file 1 by asking pod 3', function (done) {
// Yes, this could be long
this.timeout(200000)
utils.getVideosList(urls[2], function (err, res) {
if (err) throw err
var video = res.body[0]
to_remove.push(res.body[2]._id)
to_remove.push(res.body[3]._id)
webtorrent.add(video.magnetUri, function (torrent) {
expect(torrent.files).to.exist
expect(torrent.files.length).to.equal(1)
expect(torrent.files[0].path).to.exist.and.to.not.equal('')
done()
})
})
}) })
it('Should not have videos for all pods', function (done) { it('Should add the file 2 by asking pod 1', function (done) {
// Yes, this could be long
this.timeout(200000)
utils.getVideosList(urls[0], function (err, res) {
if (err) throw err
var video = res.body[1]
webtorrent.add(video.magnetUri, function (torrent) {
expect(torrent.files).to.exist
expect(torrent.files.length).to.equal(1)
expect(torrent.files[0].path).to.exist.and.to.not.equal('')
done()
})
})
})
it('Should add the file 3 by asking pod 2', function (done) {
// Yes, this could be long
this.timeout(200000)
utils.getVideosList(urls[1], function (err, res) {
if (err) throw err
var video = res.body[2]
webtorrent.add(video.magnetUri, function (torrent) {
expect(torrent.files).to.exist
expect(torrent.files.length).to.equal(1)
expect(torrent.files[0].path).to.exist.and.to.not.equal('')
done()
})
})
})
it('Should add the file 3-2 by asking pod 1', function (done) {
// Yes, this could be long
this.timeout(200000)
utils.getVideosList(urls[0], function (err, res) {
if (err) throw err
var video = res.body[3]
webtorrent.add(video.magnetUri, function (torrent) {
expect(torrent.files).to.exist
expect(torrent.files.length).to.equal(1)
expect(torrent.files[0].path).to.exist.and.to.not.equal('')
done()
})
})
})
it('Should remove the file 3 and 3-2 by asking pod 3', function (done) {
this.timeout(15000)
async.series([
function (next) {
utils.removeVideo(urls[2], to_remove[0], next)
},
function (next) {
utils.removeVideo(urls[2], to_remove[1], next)
}],
function (err) {
if (err) throw err
setTimeout(done, 11000)
}
)
})
it('Should have videos 1 and 3 on each pod', function (done) {
async.each(urls, function (url, callback) { async.each(urls, function (url, callback) {
utils.getVideosList(url, function (err, res) { utils.getVideosList(url, function (err, res) {
if (err) throw err if (err) throw err
expect(res.body).to.be.an('array') var videos = res.body
expect(res.body.length).to.equal(0) expect(videos).to.be.an('array')
expect(videos.length).to.equal(2)
expect(videos[0]._id).not.to.equal(videos[1]._id)
expect(videos[0]._id).not.to.equal(to_remove[0])
expect(videos[1]._id).not.to.equal(to_remove[0])
expect(videos[0]._id).not.to.equal(to_remove[1])
expect(videos[1]._id).not.to.equal(to_remove[1])
callback() callback()
}) })
}, done) }, done)
}) })
describe('Should upload the video and propagate on each pod', function () {
it('Should upload the video on pod 1 and propagate on each pod', function (done) {
this.timeout(15000)
async.series([
function (next) {
utils.uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', next)
},
function (next) {
setTimeout(next, 11000)
}],
// All pods should have this video
function (err) {
if (err) throw err
async.each(urls, function (url, callback) {
var base_magnet = null
utils.getVideosList(url, function (err, res) {
if (err) throw err
var videos = res.body
expect(videos).to.be.an('array')
expect(videos.length).to.equal(1)
var video = videos[0]
expect(video.name).to.equal('my super name for pod 1')
expect(video.description).to.equal('my super description for pod 1')
expect(video.podUrl).to.equal('http://localhost:9001')
expect(video.magnetUri).to.exist
// All pods should have the same magnet Uri
if (base_magnet === null) {
base_magnet = video.magnetUri
} else {
expect(video.magnetUri).to.equal.magnetUri
}
callback()
})
}, done)
}
)
})
it('Should upload the video on pod 2 and propagate on each pod', function (done) {
this.timeout(15000)
async.series([
function (next) {
utils.uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', next)
},
function (next) {
setTimeout(next, 11000)
}],
// All pods should have this video
function (err) {
if (err) throw err
async.each(urls, function (url, callback) {
var base_magnet = null
utils.getVideosList(url, function (err, res) {
if (err) throw err
var videos = res.body
expect(videos).to.be.an('array')
expect(videos.length).to.equal(2)
var video = videos[1]
expect(video.name).to.equal('my super name for pod 2')
expect(video.description).to.equal('my super description for pod 2')
expect(video.podUrl).to.equal('http://localhost:9002')
expect(video.magnetUri).to.exist
// All pods should have the same magnet Uri
if (base_magnet === null) {
base_magnet = video.magnetUri
} else {
expect(video.magnetUri).to.equal.magnetUri
}
callback()
})
}, done)
}
)
})
it('Should upload two videos on pod 3 and propagate on each pod', function (done) {
this.timeout(30000)
async.series([
function (next) {
utils.uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', next)
},
function (next) {
utils.uploadVideo(urls[2], 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', next)
},
function (next) {
setTimeout(next, 22000)
}],
function (err) {
if (err) throw err
var base_magnet = null
// All pods should have this video
async.each(urls, function (url, callback) {
utils.getVideosList(url, function (err, res) {
if (err) throw err
var videos = res.body
expect(videos).to.be.an('array')
expect(videos.length).to.equal(4)
var video = videos[2]
expect(video.name).to.equal('my super name for pod 3')
expect(video.description).to.equal('my super description for pod 3')
expect(video.podUrl).to.equal('http://localhost:9003')
expect(video.magnetUri).to.exist
video = videos[3]
expect(video.name).to.equal('my super name for pod 3-2')
expect(video.description).to.equal('my super description for pod 3-2')
expect(video.podUrl).to.equal('http://localhost:9003')
expect(video.magnetUri).to.exist
// All pods should have the same magnet Uri
if (base_magnet === null) {
base_magnet = video.magnetUri
} else {
expect(video.magnetUri).to.equal.magnetUri
}
callback()
})
}, done)
}
)
})
})
describe('Should seed the uploaded video', function () {
it('Should add the file 1 by asking pod 3', function (done) {
// Yes, this could be long
this.timeout(200000)
utils.getVideosList(urls[2], function (err, res) {
if (err) throw err
var video = res.body[0]
to_remove.push(res.body[2]._id)
to_remove.push(res.body[3]._id)
webtorrent.add(video.magnetUri, function (torrent) {
expect(torrent.files).to.exist
expect(torrent.files.length).to.equal(1)
expect(torrent.files[0].path).to.exist.and.to.not.equal('')
done()
})
})
})
it('Should add the file 2 by asking pod 1', function (done) {
// Yes, this could be long
this.timeout(200000)
utils.getVideosList(urls[0], function (err, res) {
if (err) throw err
var video = res.body[1]
webtorrent.add(video.magnetUri, function (torrent) {
expect(torrent.files).to.exist
expect(torrent.files.length).to.equal(1)
expect(torrent.files[0].path).to.exist.and.to.not.equal('')
done()
})
})
})
it('Should add the file 3 by asking pod 2', function (done) {
// Yes, this could be long
this.timeout(200000)
utils.getVideosList(urls[1], function (err, res) {
if (err) throw err
var video = res.body[2]
webtorrent.add(video.magnetUri, function (torrent) {
expect(torrent.files).to.exist
expect(torrent.files.length).to.equal(1)
expect(torrent.files[0].path).to.exist.and.to.not.equal('')
done()
})
})
})
it('Should add the file 3-2 by asking pod 1', function (done) {
// Yes, this could be long
this.timeout(200000)
utils.getVideosList(urls[0], function (err, res) {
if (err) throw err
var video = res.body[3]
webtorrent.add(video.magnetUri, function (torrent) {
expect(torrent.files).to.exist
expect(torrent.files.length).to.equal(1)
expect(torrent.files[0].path).to.exist.and.to.not.equal('')
done()
})
})
})
it('Should remove the file 3 and 3-2 by asking pod 3', function (done) {
this.timeout(15000)
async.series([
function (next) {
utils.removeVideo(urls[2], to_remove[0], next)
},
function (next) {
utils.removeVideo(urls[2], to_remove[1], next)
}],
function (err) {
if (err) throw err
setTimeout(done, 11000)
}
)
})
it('Should have videos 1 and 3 on each pod', function (done) {
async.each(urls, function (url, callback) {
utils.getVideosList(url, function (err, res) {
if (err) throw err
var videos = res.body
expect(videos).to.be.an('array')
expect(videos.length).to.equal(2)
expect(videos[0]._id).not.to.equal(videos[1]._id)
expect(videos[0]._id).not.to.equal(to_remove[0])
expect(videos[1]._id).not.to.equal(to_remove[0])
expect(videos[0]._id).not.to.equal(to_remove[1])
expect(videos[1]._id).not.to.equal(to_remove[1])
callback()
})
}, done)
})
})
after(function (done) {
apps.forEach(function (app) {
process.kill(-app.pid)
})
process.kill(-webtorrent.app.pid)
// Keep the logs if the test failed
if (this.ok) {
utils.flushTests(done)
} else {
done()
}
})
}) })
})()
after(function (done) {
apps.forEach(function (app) {
process.kill(-app.pid)
})
process.kill(-webtorrent.app.pid)
// Keep the logs if the test failed
if (this.ok) {
utils.flushTests(done)
} else {
done()
}
})
})

View File

@ -1,148 +1,146 @@
;(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
before(function (done) { before(function (done) {
this.timeout(20000) this.timeout(20000)
async.series([ async.series([
function (next) { function (next) {
utils.flushTests(next) utils.flushTests(next)
}, },
function (next) { function (next) {
utils.runServer(1, function (app1, url1) { utils.runServer(1, function (app1, url1) {
app = app1 app = app1
url = url1 url = url1
next() next()
})
},
function (next) {
webtorrent.create({ host: 'client', port: '1' }, next)
}
], done)
})
it('Should not have videos', function (done) {
utils.getVideosList(url, function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(0)
done()
})
})
it('Should upload the video', function (done) {
this.timeout(5000)
utils.uploadVideo(url, 'my super name', 'my super description', 'video_short.webm', done)
})
it('Should seed the uploaded video', function (done) {
// Yes, this could be long
this.timeout(60000)
utils.getVideosList(url, function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(1)
var video = res.body[0]
expect(video.name).to.equal('my super name')
expect(video.description).to.equal('my super description')
expect(video.podUrl).to.equal('http://localhost:9001')
expect(video.magnetUri).to.exist
video_id = video._id
webtorrent.add(video.magnetUri, function (torrent) {
expect(torrent.files).to.exist
expect(torrent.files.length).to.equal(1)
expect(torrent.files[0].path).to.exist.and.to.not.equal('')
done()
}) })
}) },
}) function (next) {
webtorrent.create({ host: 'client', port: '1' }, next)
it('Should search the video', function (done) {
utils.searchVideo(url, 'my', function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(1)
var video = res.body[0]
expect(video.name).to.equal('my super name')
expect(video.description).to.equal('my super description')
expect(video.podUrl).to.equal('http://localhost:9001')
expect(video.magnetUri).to.exist
done()
})
})
it('Should not find a search', function (done) {
utils.searchVideo(url, 'hello', function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(0)
done()
})
})
it('Should remove the video', function (done) {
utils.removeVideo(url, video_id, function (err) {
if (err) throw err
fs.readdir(pathUtils.join(__dirname, '../../test1/uploads/'), function (err, files) {
if (err) throw err
expect(files.length).to.equal(0)
done()
})
})
})
it('Should not have videos', function (done) {
utils.getVideosList(url, function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(0)
done()
})
})
after(function (done) {
process.kill(-app.pid)
process.kill(-webtorrent.app.pid)
// Keep the logs if the test failed
if (this.ok) {
utils.flushTests(done)
} else {
done()
} }
], done)
})
it('Should not have videos', function (done) {
utils.getVideosList(url, function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(0)
done()
}) })
}) })
})()
it('Should upload the video', function (done) {
this.timeout(5000)
utils.uploadVideo(url, 'my super name', 'my super description', 'video_short.webm', done)
})
it('Should seed the uploaded video', function (done) {
// Yes, this could be long
this.timeout(60000)
utils.getVideosList(url, function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(1)
var video = res.body[0]
expect(video.name).to.equal('my super name')
expect(video.description).to.equal('my super description')
expect(video.podUrl).to.equal('http://localhost:9001')
expect(video.magnetUri).to.exist
video_id = video._id
webtorrent.add(video.magnetUri, function (torrent) {
expect(torrent.files).to.exist
expect(torrent.files.length).to.equal(1)
expect(torrent.files[0].path).to.exist.and.to.not.equal('')
done()
})
})
})
it('Should search the video', function (done) {
utils.searchVideo(url, 'my', function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(1)
var video = res.body[0]
expect(video.name).to.equal('my super name')
expect(video.description).to.equal('my super description')
expect(video.podUrl).to.equal('http://localhost:9001')
expect(video.magnetUri).to.exist
done()
})
})
it('Should not find a search', function (done) {
utils.searchVideo(url, 'hello', function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(0)
done()
})
})
it('Should remove the video', function (done) {
utils.removeVideo(url, video_id, function (err) {
if (err) throw err
fs.readdir(pathUtils.join(__dirname, '../../test1/uploads/'), function (err, files) {
if (err) throw err
expect(files.length).to.equal(0)
done()
})
})
})
it('Should not have videos', function (done) {
utils.getVideosList(url, function (err, res) {
if (err) throw err
expect(res.body).to.be.an('array')
expect(res.body.length).to.equal(0)
done()
})
})
after(function (done) {
process.kill(-app.pid)
process.kill(-webtorrent.app.pid)
// Keep the logs if the test failed
if (this.ok) {
utils.flushTests(done)
} else {
done()
}
})
})

View File

@ -1,187 +1,185 @@
;(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,
makeFriends: makeFriends, makeFriends: makeFriends,
quitFriends: quitFriends, quitFriends: quitFriends,
removeVideo: removeVideo, removeVideo: removeVideo,
flushAndRunMultipleServers: flushAndRunMultipleServers, flushAndRunMultipleServers: flushAndRunMultipleServers,
runServer: runServer, runServer: runServer,
searchVideo: searchVideo, searchVideo: searchVideo,
uploadVideo: uploadVideo uploadVideo: uploadVideo
}
// ---------------------- Export functions --------------------
function flushTests (callback) {
exec(pathUtils.join(__dirname, '../../scripts/clean_test.sh'), callback)
}
function getFriendsList (url, end) {
var path = '/api/v1/pods/'
request(url)
.get(path)
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(end)
}
function getVideosList (url, end) {
var path = '/api/v1/videos'
request(url)
.get(path)
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(end)
}
function makeFriends (url, expected_status, callback) {
if (!callback) {
callback = expected_status
expected_status = 204
} }
// ---------------------- Export functions -------------------- var path = '/api/v1/pods/makefriends'
function flushTests (callback) { // The first pod make friend with the third
exec(pathUtils.join(__dirname, '../../scripts/clean_test.sh'), callback) request(url)
} .get(path)
.set('Accept', 'application/json')
.expect(expected_status)
.end(function (err, res) {
if (err) throw err
function getFriendsList (url, end) { // Wait for the request between pods
var path = '/api/v1/pods/' setTimeout(callback, 1000)
request(url)
.get(path)
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(end)
}
function getVideosList (url, end) {
var path = '/api/v1/videos'
request(url)
.get(path)
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(end)
}
function makeFriends (url, expected_status, callback) {
if (!callback) {
callback = expected_status
expected_status = 204
}
var path = '/api/v1/pods/makefriends'
// The first pod make friend with the third
request(url)
.get(path)
.set('Accept', 'application/json')
.expect(expected_status)
.end(function (err, res) {
if (err) throw err
// Wait for the request between pods
setTimeout(callback, 1000)
})
}
function quitFriends (url, callback) {
var path = '/api/v1/pods/quitfriends'
// The first pod make friend with the third
request(url)
.get(path)
.set('Accept', 'application/json')
.expect(204)
.end(function (err, res) {
if (err) throw err
// Wait for the request between pods
setTimeout(callback, 1000)
})
}
function removeVideo (url, id, end) {
var path = '/api/v1/videos'
request(url)
.delete(path + '/' + id)
.set('Accept', 'application/json')
.expect(204)
.end(end)
}
function flushAndRunMultipleServers (total_servers, serversRun) {
var apps = []
var urls = []
var i = 0
function anotherServerDone (number, app, url) {
apps[number - 1] = app
urls[number - 1] = url
i++
if (i === total_servers) {
serversRun(apps, urls)
}
}
flushTests(function () {
for (var j = 1; j <= total_servers; j++) {
(function (k) { // TODO: ES6 with let
// For the virtual buffer
setTimeout(function () {
runServer(k, function (app, url) {
anotherServerDone(k, app, url)
})
}, 1000 * k)
})(j)
}
}) })
} }
function runServer (number, callback) { function quitFriends (url, callback) {
var port = 9000 + number var path = '/api/v1/pods/quitfriends'
var server_run_string = {
'Connected to mongodb': false,
'Server listening on port': false
}
// Share the environment // The first pod make friend with the third
var env = Object.create(process.env) request(url)
env.NODE_ENV = 'test' .get(path)
env.NODE_APP_INSTANCE = number .set('Accept', 'application/json')
var options = { .expect(204)
silent: true, .end(function (err, res) {
env: env, if (err) throw err
detached: true
}
var app = fork(pathUtils.join(__dirname, '../../server.js'), [], options) // Wait for the request between pods
app.stdout.on('data', function onStdout (data) { setTimeout(callback, 1000)
var dont_continue = false
// Check if all required sentences are here
for (var key of Object.keys(server_run_string)) {
if (data.toString().indexOf(key) !== -1) server_run_string[key] = true
if (server_run_string[key] === false) dont_continue = true
}
// If no, there is maybe one thing not already initialized (mongodb...)
if (dont_continue === true) return
app.stdout.removeListener('data', onStdout)
callback(app, 'http://localhost:' + port)
}) })
}
function removeVideo (url, id, end) {
var path = '/api/v1/videos'
request(url)
.delete(path + '/' + id)
.set('Accept', 'application/json')
.expect(204)
.end(end)
}
function flushAndRunMultipleServers (total_servers, serversRun) {
var apps = []
var urls = []
var i = 0
function anotherServerDone (number, app, url) {
apps[number - 1] = app
urls[number - 1] = url
i++
if (i === total_servers) {
serversRun(apps, urls)
}
} }
function searchVideo (url, search, end) { flushTests(function () {
var path = '/api/v1/videos' for (var j = 1; j <= total_servers; j++) {
(function (k) { // TODO: ES6 with let
// For the virtual buffer
setTimeout(function () {
runServer(k, function (app, url) {
anotherServerDone(k, app, url)
})
}, 1000 * k)
})(j)
}
})
}
request(url) function runServer (number, callback) {
.get(path + '/search/' + search) var port = 9000 + number
.set('Accept', 'application/json') var server_run_string = {
.expect(200) 'Connected to mongodb': false,
.expect('Content-Type', /json/) 'Server listening on port': false
.end(end)
} }
function uploadVideo (url, name, description, fixture, end) { // Share the environment
var path = '/api/v1/videos' var env = Object.create(process.env)
env.NODE_ENV = 'test'
request(url) env.NODE_APP_INSTANCE = number
.post(path) var options = {
.set('Accept', 'application/json') silent: true,
.field('name', name) env: env,
.field('description', description) detached: true
.attach('input_video', pathUtils.join(__dirname, 'fixtures', fixture))
.expect(201)
.end(end)
} }
// --------------------------------------------------------------------------- var app = fork(pathUtils.join(__dirname, '../../server.js'), [], options)
app.stdout.on('data', function onStdout (data) {
var dont_continue = false
// Check if all required sentences are here
for (var key of Object.keys(server_run_string)) {
if (data.toString().indexOf(key) !== -1) server_run_string[key] = true
if (server_run_string[key] === false) dont_continue = true
}
module.exports = testUtils // If no, there is maybe one thing not already initialized (mongodb...)
})() if (dont_continue === true) return
app.stdout.removeListener('data', onStdout)
callback(app, 'http://localhost:' + port)
})
}
function searchVideo (url, search, end) {
var path = '/api/v1/videos'
request(url)
.get(path + '/search/' + search)
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(end)
}
function uploadVideo (url, name, description, fixture, end) {
var path = '/api/v1/videos'
request(url)
.post(path)
.set('Accept', 'application/json')
.field('name', name)
.field('description', description)
.attach('input_video', pathUtils.join(__dirname, 'fixtures', fixture))
.expect(201)
.end(end)
}
// ---------------------------------------------------------------------------
module.exports = testUtils