3 const async
= require('async')
4 const config
= require('config')
5 const dz
= require('dezalgo')
6 const fs
= require('fs')
7 const mongoose
= require('mongoose')
8 const path
= require('path')
10 const logger
= require('../helpers/logger')
12 const http
= config
.get('webserver.https') === true ? 'https' : 'http'
13 const host
= config
.get('webserver.host')
14 const port
= config
.get('webserver.port')
15 const uploadDir
= path
.join(__dirname
, '..', '..', config
.get('storage.uploads'))
17 // ---------------------------------------------------------------------------
19 const videosSchema
= mongoose
.Schema({
27 const VideosDB
= mongoose
.model('videos', videosSchema
)
29 // ---------------------------------------------------------------------------
33 addRemotes: addRemotes
,
37 removeOwned: removeOwned
,
38 removeAllRemotes: removeAllRemotes
,
39 removeAllRemotesOf: removeAllRemotesOf
,
40 removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris
,
44 function add (video
, callback
) {
45 logger
.info('Adding %s video to database.', video
.name
)
48 params
.podUrl
= http
+ '://' + host
+ ':' + port
50 VideosDB
.create(params
, function (err
, video
) {
52 logger
.error('Cannot insert this video into database.')
60 // TODO: avoid doublons
61 function addRemotes (videos
, callback
) {
62 if (!callback
) callback = function () {}
66 async
.each(videos
, function (video
, callback_each
) {
67 callback_each
= dz(callback_each
)
68 logger
.debug('Add remote video from pod: %s', video
.podUrl
)
73 description: video
.description
,
74 magnetUri: video
.magnetUri
,
82 VideosDB
.create(to_add
, function (err
, videos
) {
84 logger
.error('Cannot insert this remote video.')
88 return callback(null, videos
)
93 function get (id
, callback
) {
94 VideosDB
.findById(id
, function (err
, video
) {
96 logger
.error('Cannot get this video.')
100 return callback(null, video
)
104 function list (callback
) {
105 VideosDB
.find(function (err
, videos_list
) {
107 logger
.error('Cannot get the list of the videos.')
111 return callback(null, videos_list
)
115 function listOwned (callback
) {
116 // If namePath is not null this is *our* video
117 VideosDB
.find({ namePath: { $ne: null } }, function (err
, videos_list
) {
119 logger
.error('Cannot get the list of owned videos.')
123 return callback(null, videos_list
)
127 function removeOwned (id
, callback
) {
128 VideosDB
.findByIdAndRemove(id
, function (err
, video
) {
130 logger
.error('Cannot remove the torrent.')
134 fs
.unlink(uploadDir
+ video
.namePath
, function (err
) {
136 logger
.error('Cannot remove this video file.')
145 function removeAllRemotes (callback
) {
146 VideosDB
.remove({ namePath: null }, callback
)
149 function removeAllRemotesOf (fromUrl
, callback
) {
150 VideosDB
.remove({ podUrl: fromUrl
}, callback
)
153 // Use the magnet Uri because the _id field is not the same on different servers
154 function removeRemotesOfByMagnetUris (fromUrl
, magnetUris
, callback
) {
155 if (callback
=== undefined) callback = function () {}
157 VideosDB
.find({ magnetUri: { $in: magnetUris
} }, function (err
, videos
) {
158 if (err
|| !videos
) {
159 logger
.error('Cannot find the torrent URI of these remote videos.')
164 async
.each(videos
, function (video
, callback_async
) {
165 callback_async
= dz(callback_async
)
167 if (video
.podUrl
!== fromUrl
) {
168 logger
.error('The pod %s has not the rights on the video of %s.', fromUrl
, video
.podUrl
)
170 to_remove
.push(video
._id
)
175 VideosDB
.remove({ _id: { $in: to_remove
} }, function (err
) {
177 logger
.error('Cannot remove the remote videos.')
181 logger
.info('Removed remote videos from %s.', fromUrl
)
188 function search (name
, callback
) {
189 VideosDB
.find({ name: new RegExp(name
) }, function (err
, videos
) {
191 logger
.error('Cannot search the videos.')
195 return callback(null, videos
)
199 // ---------------------------------------------------------------------------
201 module
.exports
= Videos