]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - src/videos.js
8c44cad950822999a448fcccd444046208ba5fab
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 // Do not wait the secure requests
82 pods
.makeSecureRequest(data
)
88 videos
.remove = function (id
, callback
) {
89 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
90 function removeTorrent (magnetUri
, callback
) {
92 webtorrent
.remove(magnetUri
, callback
)
94 logger
.warn('Cannot remove the torrent from WebTorrent', { err: err
})
99 VideosDB
.findById(id
, function (err
, video
) {
101 if (!err
) err
= new Error('Cannot find this video.')
102 logger
.error('Cannot find this video.', { error: err
})
106 if (video
.namePath
=== null) {
107 var error_string
= 'Cannot remove the video of another pod.'
108 logger
.error(error_string
)
109 return callback(new Error(error_string
))
112 logger
.info('Removing %s video', video
.name
)
114 removeTorrent(video
.magnetUri
, function () {
115 VideosDB
.findByIdAndRemove(id
, function (err
) {
117 logger
.error('Cannot remove the torrent.', { error: err
})
121 fs
.unlink(videos
.uploadDir
+ video
.namePath
, function (err
) {
123 logger
.error('Cannot remove this video file.', { error: err
})
128 path: '/api/' + global
.API_VERSION
+ '/remotevideos/remove',
131 magnetUri: video
.magnetUri
135 // Yes this is a POST request because we add some informations in the body (signature, encrypt etc)
136 pods
.makeSecureRequest(data
)
144 // Use the magnet Uri because the _id field is not the same on different servers
145 videos
.removeRemote = function (fromUrl
, magnetUri
, callback
) {
146 VideosDB
.findOne({ magnetUri: magnetUri
}, function (err
, video
) {
148 logger
.error('Cannot find the torrent URI of this remote video.')
152 // TODO: move to reqValidators middleware ?
153 if (video
.podUrl
!== fromUrl
) {
154 logger
.error('The pod has not the rights on this video.')
158 VideosDB
.findByIdAndRemove(video
._id
, function (err
) {
160 logger
.error('Cannot remove the remote video.')
169 // { name, magnetUri, podUrl }
170 videos
.addRemote = function (data
, callback
) {
171 logger
.debug('Add remote video from pod: %s', data
.podUrl
)
176 description: data
.description
,
177 magnetUri: data
.magnetUri
,
181 VideosDB
.create(params
, function (err
, video
) {
183 logger
.error('Cannot insert this remote video.', { error: err
})
187 return callback(null, video
)
191 videos
.get = function (id
, callback
) {
192 VideosDB
.findById(id
, function (err
, video
) {
194 logger
.error('Cannot get this video.', { error: err
})
198 return callback(null, video
)
202 videos
.search = function (name
, callback
) {
203 VideosDB
.find({ name: new RegExp(name
) }, function (err
, videos
) {
205 logger
.error('Cannot search the videos.', { error: err
})
209 return callback(null, videos
)
213 videos
.seedAll = function (callback
) {
214 VideosDB
.find({ namePath: { $ne: null } }, function (err
, videos_list
) {
216 logger
.error('Cannot get list of the videos to seed.', { error: err
})
220 async
.each(videos_list
, function (video
, each_callback
) {
221 seedVideo(videos
.uploadDir
+ video
.namePath
, function (err
) {
223 logger
.error('Cannot seed this video.', { error: err
})
233 module
.exports
= videos