3 var async
= require('async')
4 var config
= require('config')
5 var dz
= require('dezalgo')
7 var mongoose
= require('mongoose')
8 var path
= require('path')
10 var logger
= require('../helpers/logger')
12 var http
= config
.get('webserver.https') === true ? 'https' : 'http'
13 var host
= config
.get('webserver.host')
14 var port
= config
.get('webserver.port')
15 var uploadDir
= path
.join(__dirname
, '..', '..', config
.get('storage.uploads'))
17 // ---------------------------------------------------------------------------
19 var videosSchema
= mongoose
.Schema({
26 var VideosDB
= mongoose
.model('videos', videosSchema
)
28 // ---------------------------------------------------------------------------
32 addRemotes: addRemotes
,
36 removeOwned: removeOwned
,
37 removeAllRemotes: removeAllRemotes
,
38 removeAllRemotesOf: removeAllRemotesOf
,
39 removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris
,
43 function add (video
, callback
) {
44 logger
.info('Adding %s video to database.', video
.name
)
47 params
.podUrl
= http
+ '://' + host
+ ':' + port
49 VideosDB
.create(params
, function (err
, video
) {
51 logger
.error('Cannot insert this video into database.')
59 // TODO: avoid doublons
60 function addRemotes (videos
, callback
) {
61 if (!callback
) callback = function () {}
65 async
.each(videos
, function (video
, callback_each
) {
66 callback_each
= dz(callback_each
)
67 logger
.debug('Add remote video from pod: %s', video
.podUrl
)
72 description: video
.description
,
73 magnetUri: video
.magnetUri
,
81 VideosDB
.create(to_add
, function (err
, videos
) {
83 logger
.error('Cannot insert this remote video.')
87 return callback(null, videos
)
92 function get (id
, callback
) {
93 VideosDB
.findById(id
, function (err
, video
) {
95 logger
.error('Cannot get this video.')
99 return callback(null, video
)
103 function list (callback
) {
104 VideosDB
.find(function (err
, videos_list
) {
106 logger
.error('Cannot get the list of the videos.')
110 return callback(null, videos_list
)
114 function listOwned (callback
) {
115 // If namePath is not null this is *our* video
116 VideosDB
.find({ namePath: { $ne: null } }, function (err
, videos_list
) {
118 logger
.error('Cannot get the list of owned videos.')
122 return callback(null, videos_list
)
126 function removeOwned (id
, callback
) {
127 VideosDB
.findByIdAndRemove(id
, function (err
, video
) {
129 logger
.error('Cannot remove the torrent.')
133 fs
.unlink(uploadDir
+ video
.namePath
, function (err
) {
135 logger
.error('Cannot remove this video file.')
144 function removeAllRemotes (callback
) {
145 VideosDB
.remove({ namePath: null }, callback
)
148 function removeAllRemotesOf (fromUrl
, callback
) {
149 VideosDB
.remove({ podUrl: fromUrl
}, callback
)
152 // Use the magnet Uri because the _id field is not the same on different servers
153 function removeRemotesOfByMagnetUris (fromUrl
, magnetUris
, callback
) {
154 if (callback
=== undefined) callback = function () {}
156 VideosDB
.find({ magnetUri: { $in: magnetUris
} }, function (err
, videos
) {
157 if (err
|| !videos
) {
158 logger
.error('Cannot find the torrent URI of these remote videos.')
163 async
.each(videos
, function (video
, callback_async
) {
164 callback_async
= dz(callback_async
)
166 if (video
.podUrl
!== fromUrl
) {
167 logger
.error('The pod %s has not the rights on the video of %s.', fromUrl
, video
.podUrl
)
169 to_remove
.push(video
._id
)
174 VideosDB
.remove({ _id: { $in: to_remove
} }, function (err
) {
176 logger
.error('Cannot remove the remote videos.')
180 logger
.info('Removed remote videos from %s.', fromUrl
)
187 function search (name
, callback
) {
188 VideosDB
.find({ name: new RegExp(name
) }, function (err
, videos
) {
190 logger
.error('Cannot search the videos.')
194 return callback(null, videos
)
198 // ---------------------------------------------------------------------------
200 module
.exports
= Videos