2016-02-07 05:23:23 -05:00
|
|
|
'use strict'
|
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const async = require('async')
|
|
|
|
const config = require('config')
|
|
|
|
const dz = require('dezalgo')
|
|
|
|
const fs = require('fs')
|
|
|
|
const mongoose = require('mongoose')
|
|
|
|
const path = require('path')
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const logger = require('../helpers/logger')
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const http = config.get('webserver.https') === true ? 'https' : 'http'
|
|
|
|
const host = config.get('webserver.host')
|
|
|
|
const port = config.get('webserver.port')
|
|
|
|
const uploadDir = path.join(__dirname, '..', '..', config.get('storage.uploads'))
|
2016-02-07 05:23:23 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const videosSchema = mongoose.Schema({
|
2016-02-07 05:23:23 -05:00
|
|
|
name: String,
|
|
|
|
namePath: String,
|
|
|
|
description: String,
|
|
|
|
magnetUri: String,
|
|
|
|
podUrl: String
|
|
|
|
})
|
2016-03-16 17:29:27 -04:00
|
|
|
const VideosDB = mongoose.model('videos', videosSchema)
|
2016-02-07 05:23:23 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const Videos = {
|
2016-02-07 05:23:23 -05:00
|
|
|
add: add,
|
|
|
|
addRemotes: addRemotes,
|
|
|
|
get: get,
|
|
|
|
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)
|
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const params = video
|
2016-02-07 05:23:23 -05:00
|
|
|
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)
|
2016-02-04 15:10:33 -05:00
|
|
|
})
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2015-06-09 11:41:40 -04:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// TODO: avoid doublons
|
|
|
|
function addRemotes (videos, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const to_add = []
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
async.each(videos, function (video, callback_each) {
|
|
|
|
callback_each = dz(callback_each)
|
|
|
|
logger.debug('Add remote video from pod: %s', video.podUrl)
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const params = {
|
2016-02-07 05:23:23 -05:00
|
|
|
name: video.name,
|
|
|
|
namePath: null,
|
|
|
|
description: video.description,
|
|
|
|
magnetUri: video.magnetUri,
|
|
|
|
podUrl: video.podUrl
|
|
|
|
}
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
to_add.push(params)
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
callback_each()
|
|
|
|
}, function () {
|
|
|
|
VideosDB.create(to_add, function (err, videos) {
|
2016-01-31 05:23:52 -05:00
|
|
|
if (err) {
|
2016-02-07 05:23:23 -05:00
|
|
|
logger.error('Cannot insert this remote video.')
|
2016-01-31 05:23:52 -05:00
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
return callback(null, videos)
|
2016-01-31 05:23:52 -05:00
|
|
|
})
|
2016-02-07 05:23:23 -05:00
|
|
|
})
|
|
|
|
}
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function get (id, callback) {
|
|
|
|
VideosDB.findById(id, function (err, video) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot get this video.')
|
|
|
|
return callback(err)
|
|
|
|
}
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
return callback(null, video)
|
|
|
|
})
|
|
|
|
}
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function list (callback) {
|
|
|
|
VideosDB.find(function (err, videos_list) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot get the list of the videos.')
|
|
|
|
return callback(err)
|
|
|
|
}
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
return callback(null, videos_list)
|
|
|
|
})
|
|
|
|
}
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
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)
|
|
|
|
}
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
fs.unlink(uploadDir + video.namePath, function (err) {
|
2016-01-31 05:23:52 -05:00
|
|
|
if (err) {
|
2016-02-07 05:23:23 -05:00
|
|
|
logger.error('Cannot remove this video file.')
|
2016-01-31 05:23:52 -05:00
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
callback(null)
|
2016-01-31 05:23:52 -05:00
|
|
|
})
|
2016-02-07 05:23:23 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeAllRemotes (callback) {
|
|
|
|
VideosDB.remove({ namePath: null }, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeAllRemotesOf (fromUrl, callback) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const to_remove = []
|
2016-02-07 05:23:23 -05:00
|
|
|
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)
|
2015-06-09 11:41:40 -04:00
|
|
|
}
|
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
callback_async()
|
|
|
|
}, function () {
|
|
|
|
VideosDB.remove({ _id: { $in: to_remove } }, function (err) {
|
2016-02-04 15:10:33 -05:00
|
|
|
if (err) {
|
2016-02-07 05:23:23 -05:00
|
|
|
logger.error('Cannot remove the remote videos.')
|
2016-02-04 15:10:33 -05:00
|
|
|
return callback(err)
|
|
|
|
}
|
2015-06-09 11:41:40 -04:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
logger.info('Removed remote videos from %s.', fromUrl)
|
2016-02-04 15:10:33 -05:00
|
|
|
callback(null)
|
2015-06-09 11:41:40 -04:00
|
|
|
})
|
|
|
|
})
|
2016-02-07 05:23:23 -05:00
|
|
|
})
|
|
|
|
}
|
2015-06-09 11:41:40 -04:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function search (name, callback) {
|
|
|
|
VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot search the videos.')
|
|
|
|
return callback(err)
|
|
|
|
}
|
2015-06-09 11:41:40 -04:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
return callback(null, videos)
|
|
|
|
})
|
|
|
|
}
|
2015-06-09 11:41:40 -04:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
module.exports = Videos
|