4 var async
= require('async')
5 var config
= require('config')
6 var dz
= require('dezalgo')
8 var mongoose
= require('mongoose')
9 var path
= require('path')
11 var logger
= require('../helpers/logger')
13 var http
= config
.get('webserver.https') === true ? 'https' : 'http'
14 var host
= config
.get('webserver.host')
15 var port
= config
.get('webserver.port')
16 var uploadDir
= path
.join(__dirname
, '..', config
.get('storage.uploads'))
18 // ---------------------------------------------------------------------------
20 var videosSchema
= mongoose
.Schema({
27 var VideosDB
= mongoose
.model('videos', videosSchema
)
29 // ---------------------------------------------------------------------------
33 addRemotes: addRemotes
,
35 getVideoState: getVideoState
,
39 removeOwned: removeOwned
,
40 removeAllRemotes: removeAllRemotes
,
41 removeAllRemotesOf: removeAllRemotesOf
,
42 removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris
,
46 function add (video
, callback
) {
47 logger
.info('Adding %s video to database.', video
.name
)
50 params
.podUrl
= http
+ '://' + host
+ ':' + port
52 VideosDB
.create(params
, function (err
, video
) {
54 logger
.error('Cannot insert this video into database.')
62 // TODO: avoid doublons
63 function addRemotes (videos
, callback
) {
64 if (!callback
) callback = function () {}
68 async
.each(videos
, function (video
, callback_each
) {
69 callback_each
= dz(callback_each
)
70 logger
.debug('Add remote video from pod: %s', video
.podUrl
)
75 description: video
.description
,
76 magnetUri: video
.magnetUri
,
84 VideosDB
.create(to_add
, function (err
, videos
) {
86 logger
.error('Cannot insert this remote video.')
90 return callback(null, videos
)
95 function get (id
, callback
) {
96 VideosDB
.findById(id
, function (err
, video
) {
98 logger
.error('Cannot get this video.')
102 return callback(null, video
)
106 function getVideoState (id
, callback
) {
107 get(id
, function (err
, video
) {
108 if (err
) return callback(err
)
110 var exist
= (video
!== null)
112 if (exist
=== true) {
113 owned
= (video
.namePath
!== null)
116 return callback(null, { exist: exist
, owned: owned
})
120 function isOwned (id
, callback
) {
121 VideosDB
.findById(id
, function (err
, video
) {
123 if (!err
) err
= new Error('Cannot find this video.')
124 logger
.error('Cannot find this video.')
128 if (video
.namePath
=== null) {
129 var error_string
= 'Cannot remove the video of another pod.'
130 logger
.error(error_string
)
131 return callback(new Error(error_string
), false, video
)
134 callback(null, true, video
)
138 function list (callback
) {
139 VideosDB
.find(function (err
, videos_list
) {
141 logger
.error('Cannot get the list of the videos.')
145 return callback(null, videos_list
)
149 function listOwned (callback
) {
150 // If namePath is not null this is *our* video
151 VideosDB
.find({ namePath: { $ne: null } }, function (err
, videos_list
) {
153 logger
.error('Cannot get the list of owned videos.')
157 return callback(null, videos_list
)
161 function removeOwned (id
, callback
) {
162 VideosDB
.findByIdAndRemove(id
, function (err
, video
) {
164 logger
.error('Cannot remove the torrent.')
168 fs
.unlink(uploadDir
+ video
.namePath
, function (err
) {
170 logger
.error('Cannot remove this video file.')
179 function removeAllRemotes (callback
) {
180 VideosDB
.remove({ namePath: null }, callback
)
183 function removeAllRemotesOf (fromUrl
, callback
) {
184 // TODO { podUrl: { $in: urls } }
185 VideosDB
.remove({ podUrl: fromUrl
}, callback
)
188 // Use the magnet Uri because the _id field is not the same on different servers
189 function removeRemotesOfByMagnetUris (fromUrl
, magnetUris
, callback
) {
190 if (callback
=== undefined) callback = function () {}
192 VideosDB
.find({ magnetUri: { $in: magnetUris
} }, function (err
, videos
) {
193 if (err
|| !videos
) {
194 logger
.error('Cannot find the torrent URI of these remote videos.')
199 async
.each(videos
, function (video
, callback_async
) {
200 callback_async
= dz(callback_async
)
202 if (video
.podUrl
!== fromUrl
) {
203 logger
.error('The pod %s has not the rights on the video of %s.', fromUrl
, video
.podUrl
)
205 to_remove
.push(video
._id
)
210 VideosDB
.remove({ _id: { $in: to_remove
} }, function (err
) {
212 logger
.error('Cannot remove the remote videos.')
216 logger
.info('Removed remote videos from %s.', fromUrl
)
223 function search (name
, callback
) {
224 VideosDB
.find({ name: new RegExp(name
) }, function (err
, videos
) {
226 logger
.error('Cannot search the videos.')
230 return callback(null, videos
)
234 // ---------------------------------------------------------------------------
236 module
.exports
= Videos