]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - models/videos.js
4 var async
= require('async')
5 var config
= require('config')
6 var dz
= require('dezalgo')
8 var webtorrent
= require('../lib/webTorrentNode')
10 var logger
= require('../helpers/logger')
11 var pods
= require('./pods')
12 var VideosDB
= require('../initializers/database').VideosDB
16 var http
= config
.get('webserver.https') === true ? 'https' : 'http'
17 var host
= config
.get('webserver.host')
18 var port
= config
.get('webserver.port')
20 // ----------- Private functions -----------
21 function seedVideo (path
, callback
) {
22 logger
.info('Seeding %s...', path
)
24 webtorrent
.seed(path
, function (torrent
) {
25 logger
.info('%s seeded (%s).', path
, torrent
.magnetURI
)
27 return callback(null, torrent
)
31 // ----------- Public attributes ----------
32 videos
.uploadDir
= __dirname
+ '/../' + config
.get('storage.uploads')
34 // ----------- Public functions -----------
35 videos
.list = function (callback
) {
36 VideosDB
.find(function (err
, videos_list
) {
38 logger
.error('Cannot get list of the videos.', { error: err
})
42 return callback(null, videos_list
)
46 videos
.listOwned = function (callback
) {
47 // If namePath is not null this is *our* video
48 VideosDB
.find({ namePath: { $ne: null } }, function (err
, videos_list
) {
50 logger
.error('Cannot get list of the videos.', { error: err
})
54 return callback(null, videos_list
)
58 videos
.add = function (data
, callback
) {
59 var video_file
= data
.video
60 var video_data
= data
.data
62 logger
.info('Adding %s video.', video_file
.path
)
63 seedVideo(video_file
.path
, function (err
, torrent
) {
65 logger
.error('Cannot seed this video.', { error: err
})
70 name: video_data
.name
,
71 namePath: video_file
.filename
,
72 description: video_data
.description
,
73 magnetUri: torrent
.magnetURI
,
74 podUrl: http
+ '://' + host
+ ':' + port
77 VideosDB
.create(params
, function (err
, video
) {
79 logger
.error('Cannot insert this video.', { error: err
})
83 // Now we'll add the video's meta data to our friends
84 params
.namePath
= null
86 pods
.addVideoToFriends(params
)
92 videos
.remove = function (id
, callback
) {
93 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
94 function removeTorrent (magnetUri
, callback
) {
96 webtorrent
.remove(magnetUri
, callback
)
98 logger
.warn('Cannot remove the torrent from WebTorrent', { err: err
})
103 VideosDB
.findById(id
, function (err
, video
) {
105 if (!err
) err
= new Error('Cannot find this video.')
106 logger
.error('Cannot find this video.', { error: err
})
110 if (video
.namePath
=== null) {
111 var error_string
= 'Cannot remove the video of another pod.'
112 logger
.error(error_string
)
113 return callback(new Error(error_string
))
116 logger
.info('Removing %s video', video
.name
)
118 removeTorrent(video
.magnetUri
, function () {
119 VideosDB
.findByIdAndRemove(id
, function (err
) {
121 logger
.error('Cannot remove the torrent.', { error: err
})
125 fs
.unlink(videos
.uploadDir
+ video
.namePath
, function (err
) {
127 logger
.error('Cannot remove this video file.', { error: err
})
133 magnetUri: video
.magnetUri
136 pods
.removeVideoToFriends(params
)
144 // Use the magnet Uri because the _id field is not the same on different servers
145 videos
.removeRemotes = function (fromUrl
, magnetUris
, callback
) {
146 if (callback
=== undefined) callback = function () {}
148 VideosDB
.find({ magnetUri: { $in: magnetUris
} }, function (err
, videos
) {
149 if (err
|| !videos
) {
150 logger
.error('Cannot find the torrent URI of these remote videos.')
155 async
.each(videos
, function (video
, callback_async
) {
156 callback_async
= dz(callback_async
)
158 if (video
.podUrl
!== fromUrl
) {
159 logger
.error('The pod %s has not the rights on the video of %s.', fromUrl
, video
.podUrl
)
161 to_remove
.push(video
._id
)
166 VideosDB
.remove({ _id: { $in: to_remove
} }, function (err
) {
168 logger
.error('Cannot remove the remote videos.')
172 logger
.info('Removed remote videos from %s.', fromUrl
)
179 videos
.removeAllRemotes = function (callback
) {
180 VideosDB
.remove({ namePath: null }, function (err
) {
181 if (err
) return callback(err
)
187 videos
.removeAllRemotesOf = function (fromUrl
, callback
) {
188 VideosDB
.remove({ podUrl: fromUrl
}, function (err
) {
189 if (err
) return callback(err
)
195 // { name, magnetUri, podUrl }
196 // TODO: avoid doublons
197 videos
.addRemotes = function (videos
, callback
) {
198 if (callback
=== undefined) callback = function () {}
202 async
.each(videos
, function (video
, callback_each
) {
203 callback_each
= dz(callback_each
)
204 logger
.debug('Add remote video from pod: %s', video
.podUrl
)
209 description: video
.description
,
210 magnetUri: video
.magnetUri
,
218 VideosDB
.create(to_add
, function (err
, videos
) {
220 logger
.error('Cannot insert this remote video.', { error: err
})
224 return callback(null, videos
)
229 videos
.get = function (id
, callback
) {
230 VideosDB
.findById(id
, function (err
, video
) {
232 logger
.error('Cannot get this video.', { error: err
})
236 return callback(null, video
)
240 videos
.search = function (name
, callback
) {
241 VideosDB
.find({ name: new RegExp(name
) }, function (err
, videos
) {
243 logger
.error('Cannot search the videos.', { error: err
})
247 return callback(null, videos
)
251 videos
.seedAll = function (callback
) {
252 VideosDB
.find({ namePath: { $ne: null } }, function (err
, videos_list
) {
254 logger
.error('Cannot get list of the videos to seed.', { error: err
})
258 async
.each(videos_list
, function (video
, each_callback
) {
259 seedVideo(videos
.uploadDir
+ video
.namePath
, function (err
) {
261 logger
.error('Cannot seed this video.', { error: err
})
271 module
.exports
= videos