]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - src/videos.js
Make the network auto sufficient (eject bad pods with scores)
[github/Chocobozzz/PeerTube.git] / src / videos.js
CommitLineData
8c308c2b
C
1;(function () {
2 'use strict'
3
a1860380
C
4 var async = require('async')
5 var config = require('config')
8c308c2b
C
6 var fs = require('fs')
7 var webtorrent = require('./webTorrentNode')
8c308c2b
C
8
9 var logger = require('./logger')
8c308c2b 10 var pods = require('./pods')
a1860380 11 var VideosDB = require('./database').VideosDB
8c308c2b
C
12
13 var videos = {}
a1860380 14
8c308c2b
C
15 var http = config.get('webserver.https') === true ? 'https' : 'http'
16 var host = config.get('webserver.host')
17 var port = config.get('webserver.port')
18
19 // ----------- Private functions -----------
20 function seedVideo (path, callback) {
a1860380 21 logger.info('Seeding %s...', path)
8c308c2b
C
22
23 webtorrent.seed(path, function (torrent) {
a1860380 24 logger.info('%s seeded (%s).', path, torrent.magnetURI)
8c308c2b
C
25
26 return callback(null, torrent)
27 })
28 }
29
30 // ----------- Public attributes ----------
31 videos.uploadDir = __dirname + '/../' + config.get('storage.uploads')
32
33 // ----------- Public functions -----------
34 videos.list = function (callback) {
35 VideosDB.find(function (err, videos_list) {
36 if (err) {
37 logger.error('Cannot get list of the videos.', { error: err })
38 return callback(err)
39 }
40
41 return callback(null, videos_list)
42 })
43 }
44
45 videos.add = function (data, callback) {
46 var video_file = data.video
47 var video_data = data.data
48
a1860380 49 logger.info('Adding %s video.', video_file.path)
8c308c2b
C
50 seedVideo(video_file.path, function (err, torrent) {
51 if (err) {
52 logger.error('Cannot seed this video.', { error: err })
53 return callback(err)
54 }
55
56 var params = {
57 name: video_data.name,
58 namePath: video_file.name,
59 description: video_data.description,
60 magnetUri: torrent.magnetURI,
61 podUrl: http + '://' + host + ':' + port
62 }
63
64 VideosDB.create(params, function (err, video) {
65 if (err) {
66 logger.error('Cannot insert this video.', { error: err })
67 return callback(err)
68 }
69
70 // Now we'll send the video's meta data
71 params.namePath = null
72
a1860380 73 logger.info('Sending %s video to friends.', video_file.path)
8c308c2b
C
74
75 var data = {
f5a60a51 76 path: '/api/' + global.API_VERSION + '/remotevideos/add',
8c308c2b
C
77 method: 'POST',
78 data: params
79 }
80
3bcb78b3
C
81 // Do not wait the secure requests
82 pods.makeSecureRequest(data)
83 callback(null)
8c308c2b
C
84 })
85 })
86 }
87
88 videos.remove = function (id, callback) {
a1860380 89 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
8c308c2b
C
90 function removeTorrent (magnetUri, callback) {
91 try {
92 webtorrent.remove(magnetUri, callback)
93 } catch (err) {
94 logger.warn('Cannot remove the torrent from WebTorrent', { err: err })
95 return callback(null)
96 }
97 }
98
99 VideosDB.findById(id, function (err, video) {
100 if (err || !video) {
77c2df95 101 if (!err) err = new Error('Cannot find this video.')
8c308c2b
C
102 logger.error('Cannot find this video.', { error: err })
103 return callback(err)
104 }
105
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))
110 }
111
a1860380 112 logger.info('Removing %s video', video.name)
8c308c2b
C
113
114 removeTorrent(video.magnetUri, function () {
115 VideosDB.findByIdAndRemove(id, function (err) {
116 if (err) {
117 logger.error('Cannot remove the torrent.', { error: err })
118 return callback(err)
119 }
120
121 fs.unlink(videos.uploadDir + video.namePath, function (err) {
122 if (err) {
123 logger.error('Cannot remove this video file.', { error: err })
124 return callback(err)
125 }
126
127 var data = {
f5a60a51 128 path: '/api/' + global.API_VERSION + '/remotevideos/remove',
8c308c2b
C
129 method: 'POST',
130 data: {
131 magnetUri: video.magnetUri
132 }
133 }
134
135 // Yes this is a POST request because we add some informations in the body (signature, encrypt etc)
3bcb78b3
C
136 pods.makeSecureRequest(data)
137 callback(null)
8c308c2b
C
138 })
139 })
140 })
141 })
142 }
143
144 // Use the magnet Uri because the _id field is not the same on different servers
145 videos.removeRemote = function (fromUrl, magnetUri, callback) {
8c308c2b
C
146 VideosDB.findOne({ magnetUri: magnetUri }, function (err, video) {
147 if (err || !video) {
148 logger.error('Cannot find the torrent URI of this remote video.')
149 return callback(err)
150 }
151
a1860380 152 // TODO: move to reqValidators middleware ?
8c308c2b 153 if (video.podUrl !== fromUrl) {
a1860380 154 logger.error('The pod has not the rights on this video.')
8c308c2b
C
155 return callback(err)
156 }
157
158 VideosDB.findByIdAndRemove(video._id, function (err) {
159 if (err) {
160 logger.error('Cannot remove the remote video.')
161 return callback(err)
162 }
163
164 callback(null)
165 })
166 })
167 }
168
169 // { name, magnetUri, podUrl }
170 videos.addRemote = function (data, callback) {
171 logger.debug('Add remote video from pod: %s', data.podUrl)
172
173 var params = {
174 name: data.name,
175 namePath: null,
176 description: data.description,
177 magnetUri: data.magnetUri,
178 podUrl: data.podUrl
179 }
180
181 VideosDB.create(params, function (err, video) {
182 if (err) {
183 logger.error('Cannot insert this remote video.', { error: err })
184 return callback(err)
185 }
186
187 return callback(null, video)
188 })
189 }
190
191 videos.get = function (id, callback) {
192 VideosDB.findById(id, function (err, video) {
193 if (err) {
194 logger.error('Cannot get this video.', { error: err })
195 return callback(err)
196 }
197
198 return callback(null, video)
199 })
200 }
201
202 videos.search = function (name, callback) {
203 VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
204 if (err) {
205 logger.error('Cannot search the videos.', { error: err })
206 return callback(err)
207 }
208
209 return callback(null, videos)
210 })
211 }
212
a1860380 213 videos.seedAll = function (callback) {
8c308c2b
C
214 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
215 if (err) {
216 logger.error('Cannot get list of the videos to seed.', { error: err })
a1860380 217 return callback(err)
8c308c2b
C
218 }
219
a1860380 220 async.each(videos_list, function (video, each_callback) {
8c308c2b
C
221 seedVideo(videos.uploadDir + video.namePath, function (err) {
222 if (err) {
223 logger.error('Cannot seed this video.', { error: err })
224 return callback(err)
225 }
226
a1860380 227 each_callback(null)
8c308c2b 228 })
a1860380 229 }, callback)
8c308c2b
C
230 })
231 }
232
233 module.exports = videos
234})()