var config = require('config')
var dz = require('dezalgo')
var fs = require('fs')
- var webtorrent = require('../lib/webTorrentNode')
+ var mongoose = require('mongoose')
var logger = require('../helpers/logger')
- 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 uploadDir = __dirname + '/../' + config.get('storage.uploads')
+
+ // ---------------------------------------------------------------------------
+
+ var videosSchema = mongoose.Schema({
+ name: String,
+ namePath: String,
+ description: String,
+ magnetUri: 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
}
- // ----------- Public attributes ----------
- videos.uploadDir = __dirname + '/../' + config.get('storage.uploads')
+ function add (video, callback) {
+ logger.info('Adding %s video to database.', video.name)
- // ----------- 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)
- }
+ var params = video
+ params.podUrl = http + '://' + host + ':' + port
- 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) {
+ VideosDB.create(params, function (err, video) {
if (err) {
- logger.error('Cannot get list of the videos.', { error: err })
+ logger.error('Cannot insert this video into database.')
return callback(err)
}
- return callback(null, videos_list)
+ callback(null)
})
}
- videos.add = function (data, callback) {
- var video_file = data.video
- var video_data = data.data
+ // TODO: avoid doublons
+ function addRemotes (videos, callback) {
+ if (!callback) callback = function () {}
- 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 })
- return callback(err)
- }
+ 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_data.name,
- namePath: video_file.filename,
- description: video_data.description,
- magnetUri: torrent.magnetURI,
- 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) {
- logger.error('Cannot insert this video.', { error: err })
+ logger.error('Cannot insert this remote video.')
return callback(err)
}
- // Now we'll add the video's meta data to our friends
- params.namePath = null
-
- pods.addVideoToFriends(params)
- callback(null)
+ return callback(null, videos)
})
})
}
- videos.remove = function (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 {
- webtorrent.remove(magnetUri, callback)
- } catch (err) {
- logger.warn('Cannot remove the torrent from WebTorrent', { err: err })
- return callback(null)
+ 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.', { error: err })
+ 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))
+ return callback(new Error(error_string), false, video)
}
- logger.info('Removing %s video', video.name)
+ callback(null, true, video)
+ })
+ }
- removeTorrent(video.magnetUri, function () {
- VideosDB.findByIdAndRemove(id, function (err) {
- if (err) {
- logger.error('Cannot remove the torrent.', { error: err })
- return callback(err)
- }
+ function list (callback) {
+ VideosDB.find(function (err, videos_list) {
+ if (err) {
+ logger.error('Cannot get the list of the videos.')
+ return callback(err)
+ }
- fs.unlink(videos.uploadDir + video.namePath, function (err) {
- if (err) {
- logger.error('Cannot remove this video file.', { error: err })
- return callback(err)
- }
+ return callback(null, videos_list)
+ })
+ }
- var params = {
- name: video.name,
- magnetUri: video.magnetUri
- }
+ 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)
+ }
- pods.removeVideoToFriends(params)
- callback(null)
- })
- })
+ 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
- videos.removeRemotes = function (fromUrl, magnetUris, callback) {
+ function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) {
if (callback === undefined) callback = function () {}
VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
})
}
- 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 })
+ logger.error('Cannot search the videos.')
return callback(err)
}
})
}
- 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 callback(err)
- }
-
- 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)
- }
-
- each_callback(null)
- })
- }, callback)
- })
- }
+ // ---------------------------------------------------------------------------
- module.exports = videos
+ module.exports = Videos
})()