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
,
34 getVideoState: getVideoState
,
38 removeOwned: removeOwned
,
39 removeAllRemotes: removeAllRemotes
,
40 removeAllRemotesOf: removeAllRemotesOf
,
41 removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris
,
45 function add (video
, callback
) {
46 logger
.info('Adding %s video to database.', video
.name
)
49 params
.podUrl
= http
+ '://' + host
+ ':' + port
51 VideosDB
.create(params
, function (err
, video
) {
53 logger
.error('Cannot insert this video into database.')
61 // TODO: avoid doublons
62 function addRemotes (videos
, callback
) {
63 if (!callback
) callback = function () {}
67 async
.each(videos
, function (video
, callback_each
) {
68 callback_each
= dz(callback_each
)
69 logger
.debug('Add remote video from pod: %s', video
.podUrl
)
74 description: video
.description
,
75 magnetUri: video
.magnetUri
,
83 VideosDB
.create(to_add
, function (err
, videos
) {
85 logger
.error('Cannot insert this remote video.')
89 return callback(null, videos
)
94 function get (id
, callback
) {
95 VideosDB
.findById(id
, function (err
, video
) {
97 logger
.error('Cannot get this video.')
101 return callback(null, video
)
105 function getVideoState (id
, callback
) {
106 get(id
, function (err
, video
) {
107 if (err
) return callback(err
)
109 var exist
= (video
!== null)
111 if (exist
=== true) {
112 owned
= (video
.namePath
!== null)
115 return callback(null, { exist: exist
, owned: owned
})
119 function isOwned (id
, callback
) {
120 VideosDB
.findById(id
, function (err
, video
) {
122 if (!err
) err
= new Error('Cannot find this video.')
123 logger
.error('Cannot find this video.')
127 if (video
.namePath
=== null) {
128 var error_string
= 'Cannot remove the video of another pod.'
129 logger
.error(error_string
)
130 return callback(new Error(error_string
), false, video
)
133 callback(null, true, video
)
137 function list (callback
) {
138 VideosDB
.find(function (err
, videos_list
) {
140 logger
.error('Cannot get the list of the videos.')
144 return callback(null, videos_list
)
148 function listOwned (callback
) {
149 // If namePath is not null this is *our* video
150 VideosDB
.find({ namePath: { $ne: null } }, function (err
, videos_list
) {
152 logger
.error('Cannot get the list of owned videos.')
156 return callback(null, videos_list
)
160 function removeOwned (id
, callback
) {
161 VideosDB
.findByIdAndRemove(id
, function (err
, video
) {
163 logger
.error('Cannot remove the torrent.')
167 fs
.unlink(uploadDir
+ video
.namePath
, function (err
) {
169 logger
.error('Cannot remove this video file.')
178 function removeAllRemotes (callback
) {
179 VideosDB
.remove({ namePath: null }, callback
)
182 function removeAllRemotesOf (fromUrl
, callback
) {
183 // TODO { podUrl: { $in: urls } }
184 VideosDB
.remove({ podUrl: fromUrl
}, callback
)
187 // Use the magnet Uri because the _id field is not the same on different servers
188 function removeRemotesOfByMagnetUris (fromUrl
, magnetUris
, callback
) {
189 if (callback
=== undefined) callback = function () {}
191 VideosDB
.find({ magnetUri: { $in: magnetUris
} }, function (err
, videos
) {
192 if (err
|| !videos
) {
193 logger
.error('Cannot find the torrent URI of these remote videos.')
198 async
.each(videos
, function (video
, callback_async
) {
199 callback_async
= dz(callback_async
)
201 if (video
.podUrl
!== fromUrl
) {
202 logger
.error('The pod %s has not the rights on the video of %s.', fromUrl
, video
.podUrl
)
204 to_remove
.push(video
._id
)
209 VideosDB
.remove({ _id: { $in: to_remove
} }, function (err
) {
211 logger
.error('Cannot remove the remote videos.')
215 logger
.info('Removed remote videos from %s.', fromUrl
)
222 function search (name
, callback
) {
223 VideosDB
.find({ name: new RegExp(name
) }, function (err
, videos
) {
225 logger
.error('Cannot search the videos.')
229 return callback(null, videos
)
233 // ---------------------------------------------------------------------------
235 module
.exports
= Videos