]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - src/videos.js
4 var async
= require('async')
5 var config
= require('config')
7 var webtorrent
= require('./webTorrentNode')
9 var logger
= require('./logger')
10 var pods
= require('./pods')
11 var VideosDB
= require('./database').VideosDB
15 var http
= config
.get('webserver.https') === true ? 'https' : 'http'
16 var host
= config
.get('webserver.host')
17 var port
= config
.get('webserver.port')
19 // ----------- Private functions -----------
20 function seedVideo (path
, callback
) {
21 logger
.info('Seeding %s...', path
)
23 webtorrent
.seed(path
, function (torrent
) {
24 logger
.info('%s seeded (%s).', path
, torrent
.magnetURI
)
26 return callback(null, torrent
)
30 // ----------- Public attributes ----------
31 videos
.uploadDir
= __dirname
+ '/../' + config
.get('storage.uploads')
33 // ----------- Public functions -----------
34 videos
.list = function (callback
) {
35 VideosDB
.find(function (err
, videos_list
) {
37 logger
.error('Cannot get list of the videos.', { error: err
})
41 return callback(null, videos_list
)
45 videos
.add = function (data
, callback
) {
46 var video_file
= data
.video
47 var video_data
= data
.data
49 logger
.info('Adding %s video.', video_file
.path
)
50 seedVideo(video_file
.path
, function (err
, torrent
) {
52 logger
.error('Cannot seed this video.', { error: err
})
57 name: video_data
.name
,
58 namePath: video_file
.name
,
59 description: video_data
.description
,
60 magnetUri: torrent
.magnetURI
,
61 podUrl: http
+ '://' + host
+ ':' + port
64 VideosDB
.create(params
, function (err
, video
) {
66 logger
.error('Cannot insert this video.', { error: err
})
70 // Now we'll send the video's meta data
71 params
.namePath
= null
73 logger
.info('Sending %s video to friends.', video_file
.path
)
76 path: '/api/' + global
.API_VERSION
+ '/remotevideos/add',
81 pods
.makeSecureRequest(data
, function (err
) {
83 logger
.error('Somes issues when sending this video to friends.', { error: err
})
93 videos
.remove = function (id
, callback
) {
94 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
95 function removeTorrent (magnetUri
, callback
) {
97 webtorrent
.remove(magnetUri
, callback
)
99 logger
.warn('Cannot remove the torrent from WebTorrent', { err: err
})
100 return callback(null)
104 VideosDB
.findById(id
, function (err
, video
) {
106 if (!err
) err
= new Error('Cannot find this video.')
107 logger
.error('Cannot find this video.', { error: err
})
111 if (video
.namePath
=== null) {
112 var error_string
= 'Cannot remove the video of another pod.'
113 logger
.error(error_string
)
114 return callback(new Error(error_string
))
117 logger
.info('Removing %s video', video
.name
)
119 removeTorrent(video
.magnetUri
, function () {
120 VideosDB
.findByIdAndRemove(id
, function (err
) {
122 logger
.error('Cannot remove the torrent.', { error: err
})
126 fs
.unlink(videos
.uploadDir
+ video
.namePath
, function (err
) {
128 logger
.error('Cannot remove this video file.', { error: err
})
133 path: '/api/' + global
.API_VERSION
+ '/remotevideos/remove',
136 magnetUri: video
.magnetUri
140 // Yes this is a POST request because we add some informations in the body (signature, encrypt etc)
141 pods
.makeSecureRequest(data
, function (err
) {
143 logger
.error('Somes issues when sending we want to remove the video to friends.', { error: err
})
155 // Use the magnet Uri because the _id field is not the same on different servers
156 videos
.removeRemote = function (fromUrl
, magnetUri
, callback
) {
157 VideosDB
.findOne({ magnetUri: magnetUri
}, function (err
, video
) {
159 logger
.error('Cannot find the torrent URI of this remote video.')
163 // TODO: move to reqValidators middleware ?
164 if (video
.podUrl
!== fromUrl
) {
165 logger
.error('The pod has not the rights on this video.')
169 VideosDB
.findByIdAndRemove(video
._id
, function (err
) {
171 logger
.error('Cannot remove the remote video.')
180 // { name, magnetUri, podUrl }
181 videos
.addRemote = function (data
, callback
) {
182 logger
.debug('Add remote video from pod: %s', data
.podUrl
)
187 description: data
.description
,
188 magnetUri: data
.magnetUri
,
192 VideosDB
.create(params
, function (err
, video
) {
194 logger
.error('Cannot insert this remote video.', { error: err
})
198 return callback(null, video
)
202 videos
.get = function (id
, callback
) {
203 VideosDB
.findById(id
, function (err
, video
) {
205 logger
.error('Cannot get this video.', { error: err
})
209 return callback(null, video
)
213 videos
.search = function (name
, callback
) {
214 VideosDB
.find({ name: new RegExp(name
) }, function (err
, videos
) {
216 logger
.error('Cannot search the videos.', { error: err
})
220 return callback(null, videos
)
224 videos
.seedAll = function (callback
) {
225 VideosDB
.find({ namePath: { $ne: null } }, function (err
, videos_list
) {
227 logger
.error('Cannot get list of the videos to seed.', { error: err
})
231 async
.each(videos_list
, function (video
, each_callback
) {
232 seedVideo(videos
.uploadDir
+ video
.namePath
, function (err
) {
234 logger
.error('Cannot seed this video.', { error: err
})
244 module
.exports
= videos