From c45f7f84001c2731909db04dd82e1c1f290386eb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 31 Jan 2016 11:23:52 +0100 Subject: Infile code reorganization --- models/videos.js | 222 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 118 insertions(+), 104 deletions(-) (limited to 'models/videos.js') diff --git a/models/videos.js b/models/videos.js index 626c55819..5711c5657 100644 --- a/models/videos.js +++ b/models/videos.js @@ -11,51 +11,29 @@ var pods = require('./pods') var VideosDB = require('../initializers/database').VideosDB - var videos = {} - 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.info('Seeding %s...', path) - - webtorrent.seed(path, function (torrent) { - logger.info('%s seeded (%s).', path, torrent.magnetURI) - - return callback(null, torrent) - }) + var videos = { + add: add, + addRemotes: addRemotes, + get: get, + list: list, + listOwned: listOwned, + remove: remove, + removeAllRemotes: removeAllRemotes, + removeAllRemotesOf: removeAllRemotesOf, + removeRemotes: removeRemotes, + search: search, + seedAll: seedAll, + uploadDir: uploadDir } // ----------- Public attributes ---------- - videos.uploadDir = __dirname + '/../' + config.get('storage.uploads') - - // ----------- Public functions ----------- - videos.list = function (callback) { - VideosDB.find(function (err, videos_list) { - if (err) { - logger.error('Cannot get list of the videos.', { error: err }) - return callback(err) - } - - return callback(null, videos_list) - }) - } - - videos.listOwned = function (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 list of the videos.', { error: err }) - return callback(err) - } + var uploadDir = __dirname + '/../' + config.get('storage.uploads') - return callback(null, videos_list) - }) - } - - videos.add = function (data, callback) { + function add (data, callback) { var video_file = data.video var video_data = data.data @@ -89,7 +67,74 @@ }) } - videos.remove = function (id, callback) { + // TODO: avoid doublons + function addRemotes (videos, callback) { + if (callback === undefined) 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.', { error: err }) + 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.', { error: err }) + return callback(err) + } + + return callback(null, video) + }) + } + + function list (callback) { + VideosDB.find(function (err, videos_list) { + if (err) { + logger.error('Cannot get list of the videos.', { error: err }) + 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 list of the videos.', { error: err }) + return callback(err) + } + + return callback(null, videos_list) + }) + } + + function remove (id, callback) { // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process function removeTorrent (magnetUri, callback) { try { @@ -122,7 +167,7 @@ return callback(err) } - fs.unlink(videos.uploadDir + video.namePath, function (err) { + fs.unlink(uploadDir + video.namePath, function (err) { if (err) { logger.error('Cannot remove this video file.', { error: err }) return callback(err) @@ -141,8 +186,24 @@ }) } + function removeAllRemotes (callback) { + VideosDB.remove({ namePath: null }, function (err) { + if (err) return callback(err) + + callback(null) + }) + } + + function removeAllRemotesOf (fromUrl, callback) { + VideosDB.remove({ podUrl: fromUrl }, function (err) { + if (err) return callback(err) + + callback(null) + }) + } + // Use the magnet Uri because the _id field is not the same on different servers - videos.removeRemotes = function (fromUrl, magnetUris, callback) { + function removeRemotes (fromUrl, magnetUris, callback) { if (callback === undefined) callback = function () {} VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) { @@ -176,68 +237,7 @@ }) } - videos.removeAllRemotes = function (callback) { - VideosDB.remove({ namePath: null }, function (err) { - if (err) return callback(err) - - callback(null) - }) - } - - videos.removeAllRemotesOf = function (fromUrl, callback) { - VideosDB.remove({ podUrl: fromUrl }, function (err) { - if (err) return callback(err) - - callback(null) - }) - } - - // { name, magnetUri, podUrl } - // TODO: avoid doublons - videos.addRemotes = function (videos, callback) { - if (callback === undefined) 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.', { error: err }) - return callback(err) - } - - return callback(null, videos) - }) - }) - } - - videos.get = function (id, callback) { - VideosDB.findById(id, function (err, video) { - if (err) { - logger.error('Cannot get this video.', { error: err }) - return callback(err) - } - - return callback(null, video) - }) - } - - videos.search = function (name, callback) { + function search (name, callback) { VideosDB.find({ name: new RegExp(name) }, function (err, videos) { if (err) { logger.error('Cannot search the videos.', { error: err }) @@ -248,7 +248,7 @@ }) } - videos.seedAll = function (callback) { + function seedAll (callback) { VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { if (err) { logger.error('Cannot get list of the videos to seed.', { error: err }) @@ -256,7 +256,7 @@ } async.each(videos_list, function (video, each_callback) { - seedVideo(videos.uploadDir + video.namePath, function (err) { + seedVideo(uploadDir + video.namePath, function (err) { if (err) { logger.error('Cannot seed this video.', { error: err }) return callback(err) @@ -268,5 +268,19 @@ }) } + // --------------------------------------------------------------------------- + module.exports = videos + + // --------------------------------------------------------------------------- + + function seedVideo (path, callback) { + logger.info('Seeding %s...', path) + + webtorrent.seed(path, function (torrent) { + logger.info('%s seeded (%s).', path, torrent.magnetURI) + + return callback(null, torrent) + }) + } })() -- cgit v1.2.3