]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - src/videos.js
5 var webtorrent
= require('./webTorrentNode')
6 var config
= require('config')
7 var async
= require('async')
9 var logger
= require('./logger')
10 var VideosDB
= require('./database').VideosDB
11 var pods
= require('./pods')
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
.debug('Seeding : %s', path
)
23 webtorrent
.seed(path
, function (torrent
) {
24 logger
.debug('Seeded : %s', 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
.debug('Path: %s', 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
.debug('Sending this video Uri to friends...')
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 seeding, it doesn't have importance
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
.debug('Removing video %s', video
.magnetUri
)
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 // TODO : check if the remote server has the rights to remove this video
159 VideosDB
.findOne({ magnetUri: magnetUri
}, function (err
, video
) {
161 logger
.error('Cannot find the torrent URI of this remote video.')
165 if (video
.podUrl
!== fromUrl
) {
166 logger
.error('The pod has not rights on this video.')
170 VideosDB
.findByIdAndRemove(video
._id
, function (err
) {
172 logger
.error('Cannot remove the remote video.')
181 // { name, magnetUri, podUrl }
182 videos
.addRemote = function (data
, callback
) {
183 logger
.debug('Add remote video from pod: %s', data
.podUrl
)
188 description: data
.description
,
189 magnetUri: data
.magnetUri
,
193 VideosDB
.create(params
, function (err
, video
) {
195 logger
.error('Cannot insert this remote video.', { error: err
})
199 return callback(null, video
)
203 videos
.get = function (id
, callback
) {
204 VideosDB
.findById(id
, function (err
, video
) {
206 logger
.error('Cannot get this video.', { error: err
})
210 return callback(null, video
)
214 videos
.search = function (name
, callback
) {
215 VideosDB
.find({ name: new RegExp(name
) }, function (err
, videos
) {
217 logger
.error('Cannot search the videos.', { error: err
})
221 return callback(null, videos
)
225 videos
.seedAll = function (final_callback
) {
226 VideosDB
.find({ namePath: { $ne: null } }, function (err
, videos_list
) {
228 logger
.error('Cannot get list of the videos to seed.', { error: err
})
229 return final_callback(err
)
232 async
.each(videos_list
, function (video
, callback
) {
233 seedVideo(videos
.uploadDir
+ video
.namePath
, function (err
) {
235 logger
.error('Cannot seed this video.', { error: err
})
245 module
.exports
= videos