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({
28 const VideosDB
= mongoose
.model('videos', videosSchema
)
30 // ---------------------------------------------------------------------------
34 addRemotes: addRemotes
,
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
,
77 duration: video
.duration
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 list (callback
) {
107 VideosDB
.find(function (err
, videos_list
) {
109 logger
.error('Cannot get the list of the videos.')
113 return callback(null, videos_list
)
117 function listOwned (callback
) {
118 // If namePath is not null this is *our* video
119 VideosDB
.find({ namePath: { $ne: null } }, function (err
, videos_list
) {
121 logger
.error('Cannot get the list of owned videos.')
125 return callback(null, videos_list
)
129 function removeOwned (id
, callback
) {
130 VideosDB
.findByIdAndRemove(id
, function (err
, video
) {
132 logger
.error('Cannot remove the torrent.')
136 fs
.unlink(uploadDir
+ video
.namePath
, function (err
) {
138 logger
.error('Cannot remove this video file.')
147 function removeAllRemotes (callback
) {
148 VideosDB
.remove({ namePath: null }, callback
)
151 function removeAllRemotesOf (fromUrl
, callback
) {
152 VideosDB
.remove({ podUrl: fromUrl
}, callback
)
155 // Use the magnet Uri because the _id field is not the same on different servers
156 function removeRemotesOfByMagnetUris (fromUrl
, magnetUris
, callback
) {
157 if (callback
=== undefined) callback = function () {}
159 VideosDB
.find({ magnetUri: { $in: magnetUris
} }, function (err
, videos
) {
160 if (err
|| !videos
) {
161 logger
.error('Cannot find the torrent URI of these remote videos.')
166 async
.each(videos
, function (video
, callback_async
) {
167 callback_async
= dz(callback_async
)
169 if (video
.podUrl
!== fromUrl
) {
170 logger
.error('The pod %s has not the rights on the video of %s.', fromUrl
, video
.podUrl
)
172 to_remove
.push(video
._id
)
177 VideosDB
.remove({ _id: { $in: to_remove
} }, function (err
) {
179 logger
.error('Cannot remove the remote videos.')
183 logger
.info('Removed remote videos from %s.', fromUrl
)
190 function search (name
, callback
) {
191 VideosDB
.find({ name: new RegExp(name
) }, function (err
, videos
) {
193 logger
.error('Cannot search the videos.')
197 return callback(null, videos
)
201 // ---------------------------------------------------------------------------
203 module
.exports
= Videos