diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/pods.js | 120 | ||||
-rw-r--r-- | models/videos.js | 222 |
2 files changed, 182 insertions, 160 deletions
diff --git a/models/pods.js b/models/pods.js index c8d08b26f..ed2f0d8ee 100644 --- a/models/pods.js +++ b/models/pods.js | |||
@@ -12,39 +12,23 @@ | |||
12 | var poolRequests = require('../lib/poolRequests') | 12 | var poolRequests = require('../lib/poolRequests') |
13 | var utils = require('../helpers/utils') | 13 | var utils = require('../helpers/utils') |
14 | 14 | ||
15 | var pods = {} | ||
16 | |||
17 | var http = config.get('webserver.https') ? 'https' : 'http' | 15 | var http = config.get('webserver.https') ? 'https' : 'http' |
18 | var host = config.get('webserver.host') | 16 | var host = config.get('webserver.host') |
19 | var port = config.get('webserver.port') | 17 | var port = config.get('webserver.port') |
20 | 18 | ||
21 | // ----------- Private functions ----------- | 19 | var pods = { |
22 | 20 | add: add, | |
23 | function getForeignPodsList (url, callback) { | 21 | addVideoToFriends: addVideoToFriends, |
24 | var path = '/api/' + constants.API_VERSION + '/pods' | 22 | list: list, |
25 | 23 | hasFriends: hasFriends, | |
26 | request.get(url + path, function (err, response, body) { | 24 | makeFriends: makeFriends, |
27 | if (err) throw err | 25 | quitFriends: quitFriends, |
28 | callback(JSON.parse(body)) | 26 | remove: remove, |
29 | }) | 27 | removeVideoToFriends |
30 | } | 28 | } |
31 | 29 | ||
32 | // ----------- Public functions ----------- | ||
33 | |||
34 | pods.list = function (callback) { | ||
35 | PodsDB.find(function (err, pods_list) { | ||
36 | if (err) { | ||
37 | logger.error('Cannot get the list of the pods.', { error: err }) | ||
38 | return callback(err) | ||
39 | } | ||
40 | |||
41 | return callback(null, pods_list) | ||
42 | }) | ||
43 | } | ||
44 | |||
45 | // { url } | ||
46 | // TODO: check if the pod is not already a friend | 30 | // TODO: check if the pod is not already a friend |
47 | pods.add = function (data, callback) { | 31 | function add (data, callback) { |
48 | var videos = require('./videos') | 32 | var videos = require('./videos') |
49 | logger.info('Adding pod: %s', data.url) | 33 | logger.info('Adding pod: %s', data.url) |
50 | 34 | ||
@@ -62,7 +46,7 @@ | |||
62 | 46 | ||
63 | videos.addRemotes(data.videos) | 47 | videos.addRemotes(data.videos) |
64 | 48 | ||
65 | fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) { | 49 | fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { |
66 | if (err) { | 50 | if (err) { |
67 | logger.error('Cannot read cert file.', { error: err }) | 51 | logger.error('Cannot read cert file.', { error: err }) |
68 | return callback(err) | 52 | return callback(err) |
@@ -80,40 +64,38 @@ | |||
80 | }) | 64 | }) |
81 | } | 65 | } |
82 | 66 | ||
83 | pods.remove = function (url, callback) { | 67 | function addVideoToFriends (video) { |
84 | var videos = require('./videos') | 68 | // To avoid duplicates |
85 | logger.info('Removing %s pod.', url) | 69 | var id = video.name + video.magnetUri |
86 | 70 | poolRequests.addToPoolRequests(id, 'add', video) | |
87 | videos.removeAllRemotesOf(url, function (err) { | 71 | } |
88 | if (err) logger.error('Cannot remove all remote videos of %s.', url) | ||
89 | 72 | ||
90 | PodsDB.remove({ url: url }, function (err) { | 73 | function list (callback) { |
91 | if (err) return callback(err) | 74 | PodsDB.find(function (err, pods_list) { |
75 | if (err) { | ||
76 | logger.error('Cannot get the list of the pods.', { error: err }) | ||
77 | return callback(err) | ||
78 | } | ||
92 | 79 | ||
93 | logger.info('%s pod removed.', url) | 80 | return callback(null, pods_list) |
94 | callback(null) | ||
95 | }) | ||
96 | }) | 81 | }) |
97 | } | 82 | } |
98 | 83 | ||
99 | pods.addVideoToFriends = function (video) { | 84 | function hasFriends (callback) { |
100 | // To avoid duplicates | 85 | PodsDB.count(function (err, count) { |
101 | var id = video.name + video.magnetUri | 86 | if (err) return callback(err) |
102 | poolRequests.addToPoolRequests(id, 'add', video) | ||
103 | } | ||
104 | 87 | ||
105 | pods.removeVideoToFriends = function (video) { | 88 | var has_friends = (count !== 0) |
106 | // To avoid duplicates | 89 | callback(null, has_friends) |
107 | var id = video.name + video.magnetUri | 90 | }) |
108 | poolRequests.addToPoolRequests(id, 'remove', video) | ||
109 | } | 91 | } |
110 | 92 | ||
111 | pods.makeFriends = function (callback) { | 93 | function makeFriends (callback) { |
112 | var videos = require('./videos') | 94 | var videos = require('./videos') |
113 | var pods_score = {} | 95 | var pods_score = {} |
114 | 96 | ||
115 | logger.info('Make friends!') | 97 | logger.info('Make friends!') |
116 | fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) { | 98 | fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { |
117 | if (err) { | 99 | if (err) { |
118 | logger.error('Cannot read public cert.', { error: err }) | 100 | logger.error('Cannot read public cert.', { error: err }) |
119 | return callback(err) | 101 | return callback(err) |
@@ -188,7 +170,7 @@ | |||
188 | function eachRequest (err, response, body, url, pod, callback_each_request) { | 170 | function eachRequest (err, response, body, url, pod, callback_each_request) { |
189 | // We add the pod if it responded correctly with its public certificate | 171 | // We add the pod if it responded correctly with its public certificate |
190 | if (!err && response.statusCode === 200) { | 172 | if (!err && response.statusCode === 200) { |
191 | pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) { | 173 | add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) { |
192 | if (err) logger.error('Error with adding %s pod.', pod.url, { error: err }) | 174 | if (err) logger.error('Error with adding %s pod.', pod.url, { error: err }) |
193 | 175 | ||
194 | videos.addRemotes(body.videos, function (err) { | 176 | videos.addRemotes(body.videos, function (err) { |
@@ -221,7 +203,7 @@ | |||
221 | } | 203 | } |
222 | } | 204 | } |
223 | 205 | ||
224 | pods.quitFriends = function (callback) { | 206 | function quitFriends (callback) { |
225 | // Stop pool requests | 207 | // Stop pool requests |
226 | poolRequests.deactivate() | 208 | poolRequests.deactivate() |
227 | // Flush pool requests | 209 | // Flush pool requests |
@@ -261,14 +243,40 @@ | |||
261 | }) | 243 | }) |
262 | } | 244 | } |
263 | 245 | ||
264 | pods.hasFriends = function (callback) { | 246 | function remove (url, callback) { |
265 | PodsDB.count(function (err, count) { | 247 | var videos = require('./videos') |
266 | if (err) return callback(err) | 248 | logger.info('Removing %s pod.', url) |
267 | 249 | ||
268 | var has_friends = (count !== 0) | 250 | videos.removeAllRemotesOf(url, function (err) { |
269 | callback(null, has_friends) | 251 | if (err) logger.error('Cannot remove all remote videos of %s.', url) |
252 | |||
253 | PodsDB.remove({ url: url }, function (err) { | ||
254 | if (err) return callback(err) | ||
255 | |||
256 | logger.info('%s pod removed.', url) | ||
257 | callback(null) | ||
258 | }) | ||
270 | }) | 259 | }) |
271 | } | 260 | } |
272 | 261 | ||
262 | function removeVideoToFriends (video) { | ||
263 | // To avoid duplicates | ||
264 | var id = video.name + video.magnetUri | ||
265 | poolRequests.addToPoolRequests(id, 'remove', video) | ||
266 | } | ||
267 | |||
268 | // --------------------------------------------------------------------------- | ||
269 | |||
273 | module.exports = pods | 270 | module.exports = pods |
271 | |||
272 | // --------------------------------------------------------------------------- | ||
273 | |||
274 | function getForeignPodsList (url, callback) { | ||
275 | var path = '/api/' + constants.API_VERSION + '/pods' | ||
276 | |||
277 | request.get(url + path, function (err, response, body) { | ||
278 | if (err) throw err | ||
279 | callback(JSON.parse(body)) | ||
280 | }) | ||
281 | } | ||
274 | })() | 282 | })() |
diff --git a/models/videos.js b/models/videos.js index 626c55819..5711c5657 100644 --- a/models/videos.js +++ b/models/videos.js | |||
@@ -11,51 +11,29 @@ | |||
11 | var pods = require('./pods') | 11 | var pods = require('./pods') |
12 | var VideosDB = require('../initializers/database').VideosDB | 12 | var VideosDB = require('../initializers/database').VideosDB |
13 | 13 | ||
14 | var videos = {} | ||
15 | |||
16 | var http = config.get('webserver.https') === true ? 'https' : 'http' | 14 | var http = config.get('webserver.https') === true ? 'https' : 'http' |
17 | var host = config.get('webserver.host') | 15 | var host = config.get('webserver.host') |
18 | var port = config.get('webserver.port') | 16 | var port = config.get('webserver.port') |
19 | 17 | ||
20 | // ----------- Private functions ----------- | 18 | var videos = { |
21 | function seedVideo (path, callback) { | 19 | add: add, |
22 | logger.info('Seeding %s...', path) | 20 | addRemotes: addRemotes, |
23 | 21 | get: get, | |
24 | webtorrent.seed(path, function (torrent) { | 22 | list: list, |
25 | logger.info('%s seeded (%s).', path, torrent.magnetURI) | 23 | listOwned: listOwned, |
26 | 24 | remove: remove, | |
27 | return callback(null, torrent) | 25 | removeAllRemotes: removeAllRemotes, |
28 | }) | 26 | removeAllRemotesOf: removeAllRemotesOf, |
27 | removeRemotes: removeRemotes, | ||
28 | search: search, | ||
29 | seedAll: seedAll, | ||
30 | uploadDir: uploadDir | ||
29 | } | 31 | } |
30 | 32 | ||
31 | // ----------- Public attributes ---------- | 33 | // ----------- Public attributes ---------- |
32 | videos.uploadDir = __dirname + '/../' + config.get('storage.uploads') | 34 | var uploadDir = __dirname + '/../' + config.get('storage.uploads') |
33 | |||
34 | // ----------- Public functions ----------- | ||
35 | videos.list = function (callback) { | ||
36 | VideosDB.find(function (err, videos_list) { | ||
37 | if (err) { | ||
38 | logger.error('Cannot get list of the videos.', { error: err }) | ||
39 | return callback(err) | ||
40 | } | ||
41 | |||
42 | return callback(null, videos_list) | ||
43 | }) | ||
44 | } | ||
45 | |||
46 | videos.listOwned = function (callback) { | ||
47 | // If namePath is not null this is *our* video | ||
48 | VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { | ||
49 | if (err) { | ||
50 | logger.error('Cannot get list of the videos.', { error: err }) | ||
51 | return callback(err) | ||
52 | } | ||
53 | 35 | ||
54 | return callback(null, videos_list) | 36 | function add (data, callback) { |
55 | }) | ||
56 | } | ||
57 | |||
58 | videos.add = function (data, callback) { | ||
59 | var video_file = data.video | 37 | var video_file = data.video |
60 | var video_data = data.data | 38 | var video_data = data.data |
61 | 39 | ||
@@ -89,7 +67,74 @@ | |||
89 | }) | 67 | }) |
90 | } | 68 | } |
91 | 69 | ||
92 | videos.remove = function (id, callback) { | 70 | // TODO: avoid doublons |
71 | function addRemotes (videos, callback) { | ||
72 | if (callback === undefined) callback = function () {} | ||
73 | |||
74 | var to_add = [] | ||
75 | |||
76 | async.each(videos, function (video, callback_each) { | ||
77 | callback_each = dz(callback_each) | ||
78 | logger.debug('Add remote video from pod: %s', video.podUrl) | ||
79 | |||
80 | var params = { | ||
81 | name: video.name, | ||
82 | namePath: null, | ||
83 | description: video.description, | ||
84 | magnetUri: video.magnetUri, | ||
85 | podUrl: video.podUrl | ||
86 | } | ||
87 | |||
88 | to_add.push(params) | ||
89 | |||
90 | callback_each() | ||
91 | }, function () { | ||
92 | VideosDB.create(to_add, function (err, videos) { | ||
93 | if (err) { | ||
94 | logger.error('Cannot insert this remote video.', { error: err }) | ||
95 | return callback(err) | ||
96 | } | ||
97 | |||
98 | return callback(null, videos) | ||
99 | }) | ||
100 | }) | ||
101 | } | ||
102 | |||
103 | function get (id, callback) { | ||
104 | VideosDB.findById(id, function (err, video) { | ||
105 | if (err) { | ||
106 | logger.error('Cannot get this video.', { error: err }) | ||
107 | return callback(err) | ||
108 | } | ||
109 | |||
110 | return callback(null, video) | ||
111 | }) | ||
112 | } | ||
113 | |||
114 | function list (callback) { | ||
115 | VideosDB.find(function (err, videos_list) { | ||
116 | if (err) { | ||
117 | logger.error('Cannot get list of the videos.', { error: err }) | ||
118 | return callback(err) | ||
119 | } | ||
120 | |||
121 | return callback(null, videos_list) | ||
122 | }) | ||
123 | } | ||
124 | |||
125 | function listOwned (callback) { | ||
126 | // If namePath is not null this is *our* video | ||
127 | VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { | ||
128 | if (err) { | ||
129 | logger.error('Cannot get list of the videos.', { error: err }) | ||
130 | return callback(err) | ||
131 | } | ||
132 | |||
133 | return callback(null, videos_list) | ||
134 | }) | ||
135 | } | ||
136 | |||
137 | function remove (id, callback) { | ||
93 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process | 138 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process |
94 | function removeTorrent (magnetUri, callback) { | 139 | function removeTorrent (magnetUri, callback) { |
95 | try { | 140 | try { |
@@ -122,7 +167,7 @@ | |||
122 | return callback(err) | 167 | return callback(err) |
123 | } | 168 | } |
124 | 169 | ||
125 | fs.unlink(videos.uploadDir + video.namePath, function (err) { | 170 | fs.unlink(uploadDir + video.namePath, function (err) { |
126 | if (err) { | 171 | if (err) { |
127 | logger.error('Cannot remove this video file.', { error: err }) | 172 | logger.error('Cannot remove this video file.', { error: err }) |
128 | return callback(err) | 173 | return callback(err) |
@@ -141,8 +186,24 @@ | |||
141 | }) | 186 | }) |
142 | } | 187 | } |
143 | 188 | ||
189 | function removeAllRemotes (callback) { | ||
190 | VideosDB.remove({ namePath: null }, function (err) { | ||
191 | if (err) return callback(err) | ||
192 | |||
193 | callback(null) | ||
194 | }) | ||
195 | } | ||
196 | |||
197 | function removeAllRemotesOf (fromUrl, callback) { | ||
198 | VideosDB.remove({ podUrl: fromUrl }, function (err) { | ||
199 | if (err) return callback(err) | ||
200 | |||
201 | callback(null) | ||
202 | }) | ||
203 | } | ||
204 | |||
144 | // Use the magnet Uri because the _id field is not the same on different servers | 205 | // Use the magnet Uri because the _id field is not the same on different servers |
145 | videos.removeRemotes = function (fromUrl, magnetUris, callback) { | 206 | function removeRemotes (fromUrl, magnetUris, callback) { |
146 | if (callback === undefined) callback = function () {} | 207 | if (callback === undefined) callback = function () {} |
147 | 208 | ||
148 | VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) { | 209 | VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) { |
@@ -176,68 +237,7 @@ | |||
176 | }) | 237 | }) |
177 | } | 238 | } |
178 | 239 | ||
179 | videos.removeAllRemotes = function (callback) { | 240 | function search (name, callback) { |
180 | VideosDB.remove({ namePath: null }, function (err) { | ||
181 | if (err) return callback(err) | ||
182 | |||
183 | callback(null) | ||
184 | }) | ||
185 | } | ||
186 | |||
187 | videos.removeAllRemotesOf = function (fromUrl, callback) { | ||
188 | VideosDB.remove({ podUrl: fromUrl }, function (err) { | ||
189 | if (err) return callback(err) | ||
190 | |||
191 | callback(null) | ||
192 | }) | ||
193 | } | ||
194 | |||
195 | // { name, magnetUri, podUrl } | ||
196 | // TODO: avoid doublons | ||
197 | videos.addRemotes = function (videos, callback) { | ||
198 | if (callback === undefined) callback = function () {} | ||
199 | |||
200 | var to_add = [] | ||
201 | |||
202 | async.each(videos, function (video, callback_each) { | ||
203 | callback_each = dz(callback_each) | ||
204 | logger.debug('Add remote video from pod: %s', video.podUrl) | ||
205 | |||
206 | var params = { | ||
207 | name: video.name, | ||
208 | namePath: null, | ||
209 | description: video.description, | ||
210 | magnetUri: video.magnetUri, | ||
211 | podUrl: video.podUrl | ||
212 | } | ||
213 | |||
214 | to_add.push(params) | ||
215 | |||
216 | callback_each() | ||
217 | }, function () { | ||
218 | VideosDB.create(to_add, function (err, videos) { | ||
219 | if (err) { | ||
220 | logger.error('Cannot insert this remote video.', { error: err }) | ||
221 | return callback(err) | ||
222 | } | ||
223 | |||
224 | return callback(null, videos) | ||
225 | }) | ||
226 | }) | ||
227 | } | ||
228 | |||
229 | videos.get = function (id, callback) { | ||
230 | VideosDB.findById(id, function (err, video) { | ||
231 | if (err) { | ||
232 | logger.error('Cannot get this video.', { error: err }) | ||
233 | return callback(err) | ||
234 | } | ||
235 | |||
236 | return callback(null, video) | ||
237 | }) | ||
238 | } | ||
239 | |||
240 | videos.search = function (name, callback) { | ||
241 | VideosDB.find({ name: new RegExp(name) }, function (err, videos) { | 241 | VideosDB.find({ name: new RegExp(name) }, function (err, videos) { |
242 | if (err) { | 242 | if (err) { |
243 | logger.error('Cannot search the videos.', { error: err }) | 243 | logger.error('Cannot search the videos.', { error: err }) |
@@ -248,7 +248,7 @@ | |||
248 | }) | 248 | }) |
249 | } | 249 | } |
250 | 250 | ||
251 | videos.seedAll = function (callback) { | 251 | function seedAll (callback) { |
252 | VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { | 252 | VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { |
253 | if (err) { | 253 | if (err) { |
254 | logger.error('Cannot get list of the videos to seed.', { error: err }) | 254 | logger.error('Cannot get list of the videos to seed.', { error: err }) |
@@ -256,7 +256,7 @@ | |||
256 | } | 256 | } |
257 | 257 | ||
258 | async.each(videos_list, function (video, each_callback) { | 258 | async.each(videos_list, function (video, each_callback) { |
259 | seedVideo(videos.uploadDir + video.namePath, function (err) { | 259 | seedVideo(uploadDir + video.namePath, function (err) { |
260 | if (err) { | 260 | if (err) { |
261 | logger.error('Cannot seed this video.', { error: err }) | 261 | logger.error('Cannot seed this video.', { error: err }) |
262 | return callback(err) | 262 | return callback(err) |
@@ -268,5 +268,19 @@ | |||
268 | }) | 268 | }) |
269 | } | 269 | } |
270 | 270 | ||
271 | // --------------------------------------------------------------------------- | ||
272 | |||
271 | module.exports = videos | 273 | module.exports = videos |
274 | |||
275 | // --------------------------------------------------------------------------- | ||
276 | |||
277 | function seedVideo (path, callback) { | ||
278 | logger.info('Seeding %s...', path) | ||
279 | |||
280 | webtorrent.seed(path, function (torrent) { | ||
281 | logger.info('%s seeded (%s).', path, torrent.magnetURI) | ||
282 | |||
283 | return callback(null, torrent) | ||
284 | }) | ||
285 | } | ||
272 | })() | 286 | })() |