PeerTube/models/videos.js

273 lines
7.4 KiB
JavaScript
Raw Normal View History

2015-06-09 17:41:40 +02:00
;(function () {
'use strict'
2015-11-16 13:54:00 +01:00
var async = require('async')
var config = require('config')
var dz = require('dezalgo')
2015-06-09 17:41:40 +02:00
var fs = require('fs')
2016-01-30 17:05:22 +01:00
var webtorrent = require('../lib/webTorrentNode')
2015-06-09 17:41:40 +02:00
2016-01-30 17:05:22 +01:00
var logger = require('../helpers/logger')
2015-06-09 17:41:40 +02:00
var pods = require('./pods')
2016-01-30 17:05:22 +01:00
var VideosDB = require('../initializers/database').VideosDB
2015-06-09 17:41:40 +02:00
var videos = {}
2015-11-16 13:54:00 +01:00
2015-06-09 17:41:40 +02:00
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) {
2015-11-16 13:54:00 +01:00
logger.info('Seeding %s...', path)
2015-06-09 17:41:40 +02:00
webtorrent.seed(path, function (torrent) {
2015-11-16 13:54:00 +01:00
logger.info('%s seeded (%s).', path, torrent.magnetURI)
2015-06-09 17:41:40 +02:00
return callback(null, torrent)
})
}
// ----------- 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)
}
return callback(null, videos_list)
})
}
2015-06-09 17:41:40 +02:00
videos.add = function (data, callback) {
var video_file = data.video
var video_data = data.data
2015-11-16 13:54:00 +01:00
logger.info('Adding %s video.', video_file.path)
2015-06-09 17:41:40 +02:00
seedVideo(video_file.path, function (err, torrent) {
if (err) {
logger.error('Cannot seed this video.', { error: err })
return callback(err)
}
var params = {
name: video_data.name,
2015-12-04 21:43:26 +01:00
namePath: video_file.filename,
2015-06-09 17:41:40 +02:00
description: video_data.description,
magnetUri: torrent.magnetURI,
podUrl: http + '://' + host + ':' + port
}
VideosDB.create(params, function (err, video) {
if (err) {
logger.error('Cannot insert this video.', { error: err })
return callback(err)
}
// Now we'll add the video's meta data to our friends
2015-06-09 17:41:40 +02:00
params.namePath = null
pods.addVideoToFriends(params)
callback(null)
2015-06-09 17:41:40 +02:00
})
})
}
videos.remove = function (id, callback) {
2015-11-16 13:54:00 +01:00
// Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
2015-06-09 17:41:40 +02:00
function removeTorrent (magnetUri, callback) {
try {
webtorrent.remove(magnetUri, callback)
} catch (err) {
logger.warn('Cannot remove the torrent from WebTorrent', { err: err })
return callback(null)
}
}
VideosDB.findById(id, function (err, video) {
if (err || !video) {
2015-10-30 18:34:37 +01:00
if (!err) err = new Error('Cannot find this video.')
2015-06-09 17:41:40 +02:00
logger.error('Cannot find this video.', { error: err })
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))
}
2015-11-16 13:54:00 +01:00
logger.info('Removing %s video', video.name)
2015-06-09 17:41:40 +02:00
removeTorrent(video.magnetUri, function () {
VideosDB.findByIdAndRemove(id, function (err) {
if (err) {
logger.error('Cannot remove the torrent.', { error: err })
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)
}
var params = {
name: video.name,
magnetUri: video.magnetUri
2015-06-09 17:41:40 +02:00
}
pods.removeVideoToFriends(params)
callback(null)
2015-06-09 17:41:40 +02:00
})
})
})
})
}
// Use the magnet Uri because the _id field is not the same on different servers
videos.removeRemotes = function (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.')
2015-06-09 17:41:40 +02:00
return callback(err)
}
var to_remove = []
async.each(videos, function (video, callback_async) {
callback_async = dz(callback_async)
2015-06-09 17:41:40 +02:00
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)
2015-06-09 17:41:40 +02:00
}
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)
})
2015-06-09 17:41:40 +02:00
})
})
}
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)
})
}
2015-06-09 17:41:40 +02:00
// { name, magnetUri, podUrl }
// TODO: avoid doublons
videos.addRemotes = function (videos, callback) {
if (callback === undefined) callback = function () {}
var to_add = []
2015-06-09 17:41:40 +02:00
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
2015-06-09 17:41:40 +02:00
}
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)
})
2015-06-09 17:41:40 +02:00
})
}
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) {
VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
if (err) {
logger.error('Cannot search the videos.', { error: err })
return callback(err)
}
return callback(null, videos)
})
}
2015-11-16 13:54:00 +01:00
videos.seedAll = function (callback) {
2015-06-09 17:41:40 +02:00
VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
if (err) {
logger.error('Cannot get list of the videos to seed.', { error: err })
2015-11-16 13:54:00 +01:00
return callback(err)
2015-06-09 17:41:40 +02:00
}
2015-11-16 13:54:00 +01:00
async.each(videos_list, function (video, each_callback) {
2015-06-09 17:41:40 +02:00
seedVideo(videos.uploadDir + video.namePath, function (err) {
if (err) {
logger.error('Cannot seed this video.', { error: err })
return callback(err)
}
2015-11-16 13:54:00 +01:00
each_callback(null)
2015-06-09 17:41:40 +02:00
})
2015-11-16 13:54:00 +01:00
}, callback)
2015-06-09 17:41:40 +02:00
})
}
module.exports = videos
})()