From a18603803ae37d7db8ac116113ede0bd979eddcd Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 16 Nov 2015 13:54:00 +0100 Subject: Source refractoring --- src/videos.js | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) (limited to 'src/videos.js') diff --git a/src/videos.js b/src/videos.js index f787ae49c..b95219c39 100644 --- a/src/videos.js +++ b/src/videos.js @@ -1,27 +1,27 @@ ;(function () { 'use strict' + var async = require('async') + var config = require('config') var fs = require('fs') var webtorrent = require('./webTorrentNode') - var config = require('config') - var async = require('async') var logger = require('./logger') - var VideosDB = require('./database').VideosDB var pods = require('./pods') + var VideosDB = require('./database').VideosDB var videos = {} - // Public url + var http = config.get('webserver.https') === true ? 'https' : 'http' var host = config.get('webserver.host') var port = config.get('webserver.port') // ----------- Private functions ----------- function seedVideo (path, callback) { - logger.debug('Seeding : %s', path) + logger.info('Seeding %s...', path) webtorrent.seed(path, function (torrent) { - logger.debug('Seeded : %s', torrent.magnetURI) + logger.info('%s seeded (%s).', path, torrent.magnetURI) return callback(null, torrent) }) @@ -46,7 +46,7 @@ var video_file = data.video var video_data = data.data - logger.debug('Path: %s', video_file.path) + logger.info('Adding %s video.', video_file.path) seedVideo(video_file.path, function (err, torrent) { if (err) { logger.error('Cannot seed this video.', { error: err }) @@ -70,7 +70,7 @@ // Now we'll send the video's meta data params.namePath = null - logger.debug('Sending this video Uri to friends...') + logger.info('Sending %s video to friends.', video_file.path) var data = { path: '/api/' + global.API_VERSION + '/remotevideos/add', @@ -91,7 +91,7 @@ } videos.remove = function (id, callback) { - // Maybe the torrent is not seeding, it doesn't have importance + // 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) @@ -114,7 +114,7 @@ return callback(new Error(error_string)) } - logger.debug('Removing video %s', video.magnetUri) + logger.info('Removing %s video', video.name) removeTorrent(video.magnetUri, function () { VideosDB.findByIdAndRemove(id, function (err) { @@ -154,16 +154,15 @@ // Use the magnet Uri because the _id field is not the same on different servers videos.removeRemote = function (fromUrl, magnetUri, callback) { - // TODO : check if the remote server has the rights to remove this video - VideosDB.findOne({ magnetUri: magnetUri }, function (err, video) { if (err || !video) { logger.error('Cannot find the torrent URI of this remote video.') return callback(err) } + // TODO: move to reqValidators middleware ? if (video.podUrl !== fromUrl) { - logger.error('The pod has not rights on this video.') + logger.error('The pod has not the rights on this video.') return callback(err) } @@ -222,23 +221,23 @@ }) } - videos.seedAll = function (final_callback) { + videos.seedAll = function (callback) { VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { if (err) { logger.error('Cannot get list of the videos to seed.', { error: err }) - return final_callback(err) + return callback(err) } - async.each(videos_list, function (video, callback) { + async.each(videos_list, function (video, each_callback) { seedVideo(videos.uploadDir + video.namePath, function (err) { if (err) { logger.error('Cannot seed this video.', { error: err }) return callback(err) } - callback(null) + each_callback(null) }) - }, final_callback) + }, callback) }) } -- cgit v1.2.3