]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - src/videos.js
4 var async
= require('async')
5 var config
= require('config')
6 var dz
= require('dezalgo')
8 var webtorrent
= require('./webTorrentNode')
10 var logger
= require('./logger')
11 var pods
= require('./pods')
12 var VideosDB
= require('./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
.add = function (data
, callback
) {
47 var video_file
= data
.video
48 var video_data
= data
.data
50 logger
.info('Adding %s video.', video_file
.path
)
51 seedVideo(video_file
.path
, function (err
, torrent
) {
53 logger
.error('Cannot seed this video.', { error: err
})
58 name: video_data
.name
,
59 namePath: video_file
.name
,
60 description: video_data
.description
,
61 magnetUri: torrent
.magnetURI
,
62 podUrl: http
+ '://' + host
+ ':' + port
65 VideosDB
.create(params
, function (err
, video
) {
67 logger
.error('Cannot insert this video.', { error: err
})
71 // Now we'll add the video's meta data to our friends
72 params
.namePath
= null
74 pods
.addVideoToFriends(params
)
80 videos
.remove = function (id
, callback
) {
81 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
82 function removeTorrent (magnetUri
, callback
) {
84 webtorrent
.remove(magnetUri
, callback
)
86 logger
.warn('Cannot remove the torrent from WebTorrent', { err: err
})
91 VideosDB
.findById(id
, function (err
, video
) {
93 if (!err
) err
= new Error('Cannot find this video.')
94 logger
.error('Cannot find this video.', { error: err
})
98 if (video
.namePath
=== null) {
99 var error_string
= 'Cannot remove the video of another pod.'
100 logger
.error(error_string
)
101 return callback(new Error(error_string
))
104 logger
.info('Removing %s video', video
.name
)
106 removeTorrent(video
.magnetUri
, function () {
107 VideosDB
.findByIdAndRemove(id
, function (err
) {
109 logger
.error('Cannot remove the torrent.', { error: err
})
113 fs
.unlink(videos
.uploadDir
+ video
.namePath
, function (err
) {
115 logger
.error('Cannot remove this video file.', { error: err
})
121 magnetUri: video
.magnetUri
124 pods
.removeVideoToFriends(params
)
132 // Use the magnet Uri because the _id field is not the same on different servers
133 videos
.removeRemotes = function (fromUrl
, magnetUris
, callback
) {
134 VideosDB
.find({ magnetUri: { $in: magnetUris
} }, function (err
, videos
) {
135 if (err
|| !videos
) {
136 logger
.error('Cannot find the torrent URI of these remote videos.')
141 async
.each(videos
, function (video
, callback_async
) {
142 callback_async
= dz(callback_async
)
144 if (video
.podUrl
!== fromUrl
) {
145 logger
.error('The pod %s has not the rights on the video of %s.', fromUrl
, video
.podUrl
)
147 to_remove
.push(video
._id
)
152 VideosDB
.remove({ _id: { $in: to_remove
} }, function (err
) {
154 logger
.error('Cannot remove the remote videos.')
164 // { name, magnetUri, podUrl }
165 videos
.addRemotes = function (videos
, callback
) {
168 async
.each(videos
, function (video
, callback_each
) {
169 callback_each
= dz(callback_each
)
170 logger
.debug('Add remote video from pod: %s', video
.podUrl
)
175 description: video
.description
,
176 magnetUri: video
.magnetUri
,
184 VideosDB
.create(to_add
, function (err
, videos
) {
186 logger
.error('Cannot insert this remote video.', { error: err
})
190 return callback(null, videos
)
195 videos
.get = function (id
, callback
) {
196 VideosDB
.findById(id
, function (err
, video
) {
198 logger
.error('Cannot get this video.', { error: err
})
202 return callback(null, video
)
206 videos
.search = function (name
, callback
) {
207 VideosDB
.find({ name: new RegExp(name
) }, function (err
, videos
) {
209 logger
.error('Cannot search the videos.', { error: err
})
213 return callback(null, videos
)
217 videos
.seedAll = function (callback
) {
218 VideosDB
.find({ namePath: { $ne: null } }, function (err
, videos_list
) {
220 logger
.error('Cannot get list of the videos to seed.', { error: err
})
224 async
.each(videos_list
, function (video
, each_callback
) {
225 seedVideo(videos
.uploadDir
+ video
.namePath
, function (err
) {
227 logger
.error('Cannot seed this video.', { error: err
})
237 module
.exports
= videos