1
0
Fork 0
peertube/models/videos.js

238 lines
6.1 KiB
JavaScript
Raw Normal View History

2015-06-09 11:41:40 -04:00
;(function () {
'use strict'
2015-11-16 07:54:00 -05:00
var async = require('async')
var config = require('config')
var dz = require('dezalgo')
2015-06-09 11:41:40 -04:00
var fs = require('fs')
2016-02-04 15:10:33 -05:00
var mongoose = require('mongoose')
2016-02-07 04:39:37 -05:00
var path = require('path')
2015-06-09 11:41:40 -04:00
2016-01-30 11:05:22 -05:00
var logger = require('../helpers/logger')
2015-06-09 11:41:40 -04:00
var http = config.get('webserver.https') === true ? 'https' : 'http'
var host = config.get('webserver.host')
var port = config.get('webserver.port')
2016-02-07 04:39:37 -05:00
var uploadDir = path.join(__dirname, '..', config.get('storage.uploads'))
2016-02-04 15:10:33 -05:00
// ---------------------------------------------------------------------------
var videosSchema = mongoose.Schema({
name: String,
namePath: String,
description: String,
magnetUri: String,
podUrl: String
})
var VideosDB = mongoose.model('videos', videosSchema)
// ---------------------------------------------------------------------------
2015-06-09 11:41:40 -04:00
2016-02-04 15:10:33 -05:00
var Videos = {
2016-01-31 05:23:52 -05:00
add: add,
addRemotes: addRemotes,
get: get,
2016-02-04 15:10:33 -05:00
getVideoState: getVideoState,
isOwned: isOwned,
2016-01-31 05:23:52 -05:00
list: list,
listOwned: listOwned,
2016-02-04 15:10:33 -05:00
removeOwned: removeOwned,
2016-01-31 05:23:52 -05:00
removeAllRemotes: removeAllRemotes,
removeAllRemotesOf: removeAllRemotesOf,
2016-02-04 15:10:33 -05:00
removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris,
search: search
2015-06-09 11:41:40 -04:00
}
2016-02-04 15:10:33 -05:00
function add (video, callback) {
logger.info('Adding %s video to database.', video.name)
2016-02-04 15:10:33 -05:00
var params = video
params.podUrl = http + '://' + host + ':' + port
2015-06-09 11:41:40 -04:00
2016-02-04 15:10:33 -05:00
VideosDB.create(params, function (err, video) {
2015-06-09 11:41:40 -04:00
if (err) {
2016-02-05 13:02:05 -05:00
logger.error('Cannot insert this video into database.')
2015-06-09 11:41:40 -04:00
return callback(err)
}
2016-02-04 15:10:33 -05:00
callback(null)
2015-06-09 11:41:40 -04:00
})
}
2016-01-31 05:23:52 -05:00
// TODO: avoid doublons
function addRemotes (videos, callback) {
2016-02-04 15:10:33 -05:00
if (!callback) callback = function () {}
2016-01-31 05:23:52 -05:00
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) {
2016-02-05 13:02:05 -05:00
logger.error('Cannot insert this remote video.')
2016-01-31 05:23:52 -05:00
return callback(err)
}
return callback(null, videos)
})
})
}
function get (id, callback) {
VideosDB.findById(id, function (err, video) {
if (err) {
2016-02-05 13:02:05 -05:00
logger.error('Cannot get this video.')
2016-01-31 05:23:52 -05:00
return callback(err)
}
return callback(null, video)
})
}
2016-02-04 15:10:33 -05:00
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.')
2016-02-05 13:02:05 -05:00
logger.error('Cannot find this video.')
2016-02-04 15:10:33 -05:00
return callback(err)
}
if (video.namePath === null) {
var error_string = 'Cannot remove the video of another pod.'
logger.error(error_string)
2016-02-05 13:02:05 -05:00
return callback(new Error(error_string), false, video)
2016-02-04 15:10:33 -05:00
}
callback(null, true, video)
})
}
2016-01-31 05:23:52 -05:00
function list (callback) {
VideosDB.find(function (err, videos_list) {
if (err) {
2016-02-05 13:02:05 -05:00
logger.error('Cannot get the list of the videos.')
2016-01-31 05:23:52 -05:00
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) {
2016-02-05 13:02:05 -05:00
logger.error('Cannot get the list of owned videos.')
2016-01-31 05:23:52 -05:00
return callback(err)
}
return callback(null, videos_list)
})
}
2016-02-04 15:10:33 -05:00
function removeOwned (id, callback) {
VideosDB.findByIdAndRemove(id, function (err, video) {
if (err) {
2016-02-05 13:02:05 -05:00
logger.error('Cannot remove the torrent.')
2015-06-09 11:41:40 -04:00
return callback(err)
}
2016-02-04 15:10:33 -05:00
fs.unlink(uploadDir + video.namePath, function (err) {
if (err) {
2016-02-05 13:02:05 -05:00
logger.error('Cannot remove this video file.')
2016-02-04 15:10:33 -05:00
return callback(err)
}
2015-06-09 11:41:40 -04:00
2016-02-04 15:10:33 -05:00
callback(null)
2015-06-09 11:41:40 -04:00
})
})
}
2016-01-31 05:23:52 -05:00
function removeAllRemotes (callback) {
2016-02-04 15:10:33 -05:00
VideosDB.remove({ namePath: null }, callback)
2016-01-31 05:23:52 -05:00
}
function removeAllRemotesOf (fromUrl, callback) {
2016-02-04 15:10:33 -05:00
// TODO { podUrl: { $in: urls } }
VideosDB.remove({ podUrl: fromUrl }, callback)
2016-01-31 05:23:52 -05:00
}
2015-06-09 11:41:40 -04:00
// Use the magnet Uri because the _id field is not the same on different servers
2016-02-04 15:10:33 -05:00
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.')
2015-06-09 11:41:40 -04:00
return callback(err)
}
var to_remove = []
async.each(videos, function (video, callback_async) {
callback_async = dz(callback_async)
2015-06-09 11:41:40 -04: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 11:41:40 -04: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 11:41:40 -04:00
})
})
}
2016-01-31 05:23:52 -05:00
function search (name, callback) {
2015-06-09 11:41:40 -04:00
VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
if (err) {
2016-02-05 13:02:05 -05:00
logger.error('Cannot search the videos.')
2015-06-09 11:41:40 -04:00
return callback(err)
}
return callback(null, videos)
})
}
2016-01-31 05:23:52 -05:00
// ---------------------------------------------------------------------------
2016-02-04 15:10:33 -05:00
module.exports = Videos
2015-06-09 11:41:40 -04:00
})()