diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2015-12-04 16:13:32 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2015-12-04 16:13:32 +0100 |
commit | 0b69752270f1ceea06a29872b3db23660a55d6d3 (patch) | |
tree | 42da726633f3e48f4fe592cfd2c1ca14346a159b /src/videos.js | |
parent | af82cae07dc568e3cb10acd70113df56eb8b15a9 (diff) | |
download | PeerTube-0b69752270f1ceea06a29872b3db23660a55d6d3.tar.gz PeerTube-0b69752270f1ceea06a29872b3db23660a55d6d3.tar.zst PeerTube-0b69752270f1ceea06a29872b3db23660a55d6d3.zip |
Add a pool of requests instead of making a request at each action (add
video/remove video) for performance in big networks
Diffstat (limited to 'src/videos.js')
-rw-r--r-- | src/videos.js | 100 |
1 files changed, 52 insertions, 48 deletions
diff --git a/src/videos.js b/src/videos.js index 8c44cad95..e3a5b49f1 100644 --- a/src/videos.js +++ b/src/videos.js | |||
@@ -3,6 +3,7 @@ | |||
3 | 3 | ||
4 | var async = require('async') | 4 | var async = require('async') |
5 | var config = require('config') | 5 | var config = require('config') |
6 | var dz = require('dezalgo') | ||
6 | var fs = require('fs') | 7 | var fs = require('fs') |
7 | var webtorrent = require('./webTorrentNode') | 8 | var webtorrent = require('./webTorrentNode') |
8 | 9 | ||
@@ -67,19 +68,10 @@ | |||
67 | return callback(err) | 68 | return callback(err) |
68 | } | 69 | } |
69 | 70 | ||
70 | // Now we'll send the video's meta data | 71 | // Now we'll add the video's meta data to our friends |
71 | params.namePath = null | 72 | params.namePath = null |
72 | 73 | ||
73 | logger.info('Sending %s video to friends.', video_file.path) | 74 | pods.addVideoToFriends(params) |
74 | |||
75 | var data = { | ||
76 | path: '/api/' + global.API_VERSION + '/remotevideos/add', | ||
77 | method: 'POST', | ||
78 | data: params | ||
79 | } | ||
80 | |||
81 | // Do not wait the secure requests | ||
82 | pods.makeSecureRequest(data) | ||
83 | callback(null) | 75 | callback(null) |
84 | }) | 76 | }) |
85 | }) | 77 | }) |
@@ -124,16 +116,12 @@ | |||
124 | return callback(err) | 116 | return callback(err) |
125 | } | 117 | } |
126 | 118 | ||
127 | var data = { | 119 | var params = { |
128 | path: '/api/' + global.API_VERSION + '/remotevideos/remove', | 120 | name: video.name, |
129 | method: 'POST', | 121 | magnetUri: video.magnetUri |
130 | data: { | ||
131 | magnetUri: video.magnetUri | ||
132 | } | ||
133 | } | 122 | } |
134 | 123 | ||
135 | // Yes this is a POST request because we add some informations in the body (signature, encrypt etc) | 124 | pods.removeVideoToFriends(params) |
136 | pods.makeSecureRequest(data) | ||
137 | callback(null) | 125 | callback(null) |
138 | }) | 126 | }) |
139 | }) | 127 | }) |
@@ -142,49 +130,65 @@ | |||
142 | } | 130 | } |
143 | 131 | ||
144 | // Use the magnet Uri because the _id field is not the same on different servers | 132 | // Use the magnet Uri because the _id field is not the same on different servers |
145 | videos.removeRemote = function (fromUrl, magnetUri, callback) { | 133 | videos.removeRemotes = function (fromUrl, magnetUris, callback) { |
146 | VideosDB.findOne({ magnetUri: magnetUri }, function (err, video) { | 134 | VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) { |
147 | if (err || !video) { | 135 | if (err || !videos) { |
148 | logger.error('Cannot find the torrent URI of this remote video.') | 136 | logger.error('Cannot find the torrent URI of these remote videos.') |
149 | return callback(err) | 137 | return callback(err) |
150 | } | 138 | } |
151 | 139 | ||
152 | // TODO: move to reqValidators middleware ? | 140 | var to_remove = [] |
153 | if (video.podUrl !== fromUrl) { | 141 | async.each(videos, function (video, callback_async) { |
154 | logger.error('The pod has not the rights on this video.') | 142 | callback_async = dz(callback_async) |
155 | return callback(err) | ||
156 | } | ||
157 | 143 | ||
158 | VideosDB.findByIdAndRemove(video._id, function (err) { | 144 | if (video.podUrl !== fromUrl) { |
159 | if (err) { | 145 | logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl) |
160 | logger.error('Cannot remove the remote video.') | 146 | } else { |
161 | return callback(err) | 147 | to_remove.push(video._id) |
162 | } | 148 | } |
163 | 149 | ||
164 | callback(null) | 150 | callback_async() |
151 | }, function () { | ||
152 | VideosDB.remove({ _id: { $in: to_remove } }, function (err) { | ||
153 | if (err) { | ||
154 | logger.error('Cannot remove the remote videos.') | ||
155 | return callback(err) | ||
156 | } | ||
157 | |||
158 | callback(null) | ||
159 | }) | ||
165 | }) | 160 | }) |
166 | }) | 161 | }) |
167 | } | 162 | } |
168 | 163 | ||
169 | // { name, magnetUri, podUrl } | 164 | // { name, magnetUri, podUrl } |
170 | videos.addRemote = function (data, callback) { | 165 | videos.addRemotes = function (videos, callback) { |
171 | logger.debug('Add remote video from pod: %s', data.podUrl) | 166 | var to_add = [] |
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 | 167 | ||
181 | VideosDB.create(params, function (err, video) { | 168 | async.each(videos, function (video, callback_each) { |
182 | if (err) { | 169 | callback_each = dz(callback_each) |
183 | logger.error('Cannot insert this remote video.', { error: err }) | 170 | logger.debug('Add remote video from pod: %s', video.podUrl) |
184 | return callback(err) | 171 | |
172 | var params = { | ||
173 | name: video.name, | ||
174 | namePath: null, | ||
175 | description: video.description, | ||
176 | magnetUri: video.magnetUri, | ||
177 | podUrl: video.podUrl | ||
185 | } | 178 | } |
186 | 179 | ||
187 | return callback(null, video) | 180 | to_add.push(params) |
181 | |||
182 | callback_each() | ||
183 | }, function () { | ||
184 | VideosDB.create(to_add, function (err, videos) { | ||
185 | if (err) { | ||
186 | logger.error('Cannot insert this remote video.', { error: err }) | ||
187 | return callback(err) | ||
188 | } | ||
189 | |||
190 | return callback(null, videos) | ||
191 | }) | ||
188 | }) | 192 | }) |
189 | } | 193 | } |
190 | 194 | ||