aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/friends.js72
-rw-r--r--server/lib/requestsScheduler.js66
-rw-r--r--server/lib/videos.js54
-rw-r--r--server/lib/webtorrent.js34
-rw-r--r--server/lib/webtorrentProcess.js14
5 files changed, 119 insertions, 121 deletions
diff --git a/server/lib/friends.js b/server/lib/friends.js
index 3b8a52060..f4f2ada87 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -39,8 +39,8 @@ function hasFriends (callback) {
39 Pods.count(function (err, count) { 39 Pods.count(function (err, count) {
40 if (err) return callback(err) 40 if (err) return callback(err)
41 41
42 const has_friends = (count !== 0) 42 const hasFriends = (count !== 0)
43 callback(null, has_friends) 43 callback(null, hasFriends)
44 }) 44 })
45} 45}
46 46
@@ -49,7 +49,7 @@ function getMyCertificate (callback) {
49} 49}
50 50
51function makeFriends (callback) { 51function makeFriends (callback) {
52 const pods_score = {} 52 const podsScore = {}
53 53
54 logger.info('Make friends!') 54 logger.info('Make friends!')
55 getMyCertificate(function (err, cert) { 55 getMyCertificate(function (err, cert) {
@@ -60,16 +60,16 @@ function makeFriends (callback) {
60 60
61 const urls = config.get('network.friends') 61 const urls = config.get('network.friends')
62 62
63 async.each(urls, function (url, callback_each) { 63 async.each(urls, function (url, callbackEach) {
64 computeForeignPodsList(url, pods_score, callback_each) 64 computeForeignPodsList(url, podsScore, callbackEach)
65 }, function (err) { 65 }, function (err) {
66 if (err) return callback(err) 66 if (err) return callback(err)
67 67
68 logger.debug('Pods scores computed.', { pods_score: pods_score }) 68 logger.debug('Pods scores computed.', { podsScore: podsScore })
69 const pods_list = computeWinningPods(urls, pods_score) 69 const podsList = computeWinningPods(urls, podsScore)
70 logger.debug('Pods that we keep.', { pods_to_keep: pods_list }) 70 logger.debug('Pods that we keep.', { podsToKeep: podsList })
71 71
72 makeRequestsToWinningPods(cert, pods_list, callback) 72 makeRequestsToWinningPods(cert, podsList, callback)
73 }) 73 })
74 }) 74 })
75} 75}
@@ -102,10 +102,10 @@ function quitFriends (callback) {
102 102
103 logger.info('Broke friends, so sad :(') 103 logger.info('Broke friends, so sad :(')
104 104
105 Videos.listFromRemotes(function (err, videos_list) { 105 Videos.listFromRemotes(function (err, videosList) {
106 if (err) return callback(err) 106 if (err) return callback(err)
107 107
108 videos.removeRemoteVideos(videos_list, function (err) { 108 videos.removeRemoteVideos(videosList, function (err) {
109 if (err) { 109 if (err) {
110 logger.error('Cannot remove remote videos.', { error: err }) 110 logger.error('Cannot remove remote videos.', { error: err })
111 return callback(err) 111 return callback(err)
@@ -132,35 +132,35 @@ module.exports = pods
132 132
133// --------------------------------------------------------------------------- 133// ---------------------------------------------------------------------------
134 134
135function computeForeignPodsList (url, pods_score, callback) { 135function computeForeignPodsList (url, podsScore, callback) {
136 // Let's give 1 point to the pod we ask the friends list 136 // Let's give 1 point to the pod we ask the friends list
137 pods_score[url] = 1 137 podsScore[url] = 1
138 138
139 getForeignPodsList(url, function (err, foreign_pods_list) { 139 getForeignPodsList(url, function (err, foreignPodsList) {
140 if (err) return callback(err) 140 if (err) return callback(err)
141 if (foreign_pods_list.length === 0) return callback() 141 if (foreignPodsList.length === 0) return callback()
142 142
143 foreign_pods_list.forEach(function (foreign_pod) { 143 foreignPodsList.forEach(function (foreignPod) {
144 const foreign_url = foreign_pod.url 144 const foreignUrl = foreignPod.url
145 145
146 if (pods_score[foreign_url]) pods_score[foreign_url]++ 146 if (podsScore[foreignUrl]) podsScore[foreignUrl]++
147 else pods_score[foreign_url] = 1 147 else podsScore[foreignUrl] = 1
148 }) 148 })
149 149
150 callback() 150 callback()
151 }) 151 })
152} 152}
153 153
154function computeWinningPods (urls, pods_score) { 154function computeWinningPods (urls, podsScore) {
155 // Build the list of pods to add 155 // Build the list of pods to add
156 // Only add a pod if it exists in more than a half base pods 156 // Only add a pod if it exists in more than a half base pods
157 const pods_list = [] 157 const podsList = []
158 const base_score = urls.length / 2 158 const baseScore = urls.length / 2
159 Object.keys(pods_score).forEach(function (pod) { 159 Object.keys(baseScore).forEach(function (pod) {
160 if (pods_score[pod] > base_score) pods_list.push({ url: pod }) 160 if (podsScore[pod] > baseScore) podsList.push({ url: pod })
161 }) 161 })
162 162
163 return pods_list 163 return podsList
164} 164}
165 165
166function getForeignPodsList (url, callback) { 166function getForeignPodsList (url, callback) {
@@ -173,14 +173,14 @@ function getForeignPodsList (url, callback) {
173 }) 173 })
174} 174}
175 175
176function makeRequestsToWinningPods (cert, pods_list, callback) { 176function makeRequestsToWinningPods (cert, podsList, callback) {
177 // Stop pool requests 177 // Stop pool requests
178 requestsScheduler.deactivate() 178 requestsScheduler.deactivate()
179 // Flush pool requests 179 // Flush pool requests
180 requestsScheduler.forceSend() 180 requestsScheduler.forceSend()
181 181
182 // Get the list of our videos to send to our new friends 182 // Get the list of our videos to send to our new friends
183 Videos.listOwned(function (err, videos_list) { 183 Videos.listOwned(function (err, videosList) {
184 if (err) { 184 if (err) {
185 logger.error('Cannot get the list of videos we own.') 185 logger.error('Cannot get the list of videos we own.')
186 return callback(err) 186 return callback(err)
@@ -189,38 +189,36 @@ function makeRequestsToWinningPods (cert, pods_list, callback) {
189 const data = { 189 const data = {
190 url: http + '://' + host + ':' + port, 190 url: http + '://' + host + ':' + port,
191 publicKey: cert, 191 publicKey: cert,
192 videos: videos_list 192 videos: videosList
193 } 193 }
194 194
195 requests.makeMultipleRetryRequest( 195 requests.makeMultipleRetryRequest(
196 { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data }, 196 { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
197 197
198 pods_list, 198 podsList,
199 199
200 function eachRequest (err, response, body, url, pod, callback_each_request) { 200 function eachRequest (err, response, body, url, pod, callbackEachRequest) {
201 // We add the pod if it responded correctly with its public certificate 201 // We add the pod if it responded correctly with its public certificate
202 if (!err && response.statusCode === 200) { 202 if (!err && response.statusCode === 200) {
203 Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) { 203 Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
204 if (err) { 204 if (err) {
205 logger.error('Error with adding %s pod.', pod.url, { error: err }) 205 logger.error('Error with adding %s pod.', pod.url, { error: err })
206 return callback_each_request() 206 return callbackEachRequest()
207 } 207 }
208 console.log('hihi') 208
209 videos.createRemoteVideos(body.videos, function (err) { 209 videos.createRemoteVideos(body.videos, function (err) {
210 if (err) { 210 if (err) {
211 logger.error('Error with adding videos of pod.', pod.url, { error: err }) 211 logger.error('Error with adding videos of pod.', pod.url, { error: err })
212 return callback_each_request() 212 return callbackEachRequest()
213 } 213 }
214 214
215 console.log('kik')
216
217 logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos }) 215 logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos })
218 return callback_each_request() 216 return callbackEachRequest()
219 }) 217 })
220 }) 218 })
221 } else { 219 } else {
222 logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') }) 220 logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
223 return callback_each_request() 221 return callbackEachRequest()
224 } 222 }
225 }, 223 },
226 224
diff --git a/server/lib/requestsScheduler.js b/server/lib/requestsScheduler.js
index 4953f6a91..f10de6276 100644
--- a/server/lib/requestsScheduler.js
+++ b/server/lib/requestsScheduler.js
@@ -72,7 +72,7 @@ module.exports = requestsScheduler
72 72
73// --------------------------------------------------------------------------- 73// ---------------------------------------------------------------------------
74 74
75function makeRequest (type, requests_to_make, callback) { 75function makeRequest (type, requestsToMake, callback) {
76 if (!callback) callback = function () {} 76 if (!callback) callback = function () {}
77 77
78 Pods.list(function (err, pods) { 78 Pods.list(function (err, pods) {
@@ -83,7 +83,7 @@ function makeRequest (type, requests_to_make, callback) {
83 sign: true, 83 sign: true,
84 method: 'POST', 84 method: 'POST',
85 path: null, 85 path: null,
86 data: requests_to_make 86 data: requestsToMake
87 } 87 }
88 88
89 if (type === 'add') { 89 if (type === 'add') {
@@ -94,26 +94,26 @@ function makeRequest (type, requests_to_make, callback) {
94 return callback(new Error('Unkown pool request type.')) 94 return callback(new Error('Unkown pool request type.'))
95 } 95 }
96 96
97 const bad_pods = [] 97 const badPods = []
98 const good_pods = [] 98 const goodPods = []
99 99
100 requests.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished) 100 requests.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished)
101 101
102 function callbackEachPodFinished (err, response, body, url, pod, callback_each_pod_finished) { 102 function callbackEachPodFinished (err, response, body, url, pod, callbackEachPodFinished) {
103 if (err || (response.statusCode !== 200 && response.statusCode !== 201 && response.statusCode !== 204)) { 103 if (err || (response.statusCode !== 200 && response.statusCode !== 201 && response.statusCode !== 204)) {
104 bad_pods.push(pod._id) 104 badPods.push(pod._id)
105 logger.error('Error sending secure request to %s pod.', url, { error: err || new Error('Status code not 20x') }) 105 logger.error('Error sending secure request to %s pod.', url, { error: err || new Error('Status code not 20x') })
106 } else { 106 } else {
107 good_pods.push(pod._id) 107 goodPods.push(pod._id)
108 } 108 }
109 109
110 return callback_each_pod_finished() 110 return callbackEachPodFinished()
111 } 111 }
112 112
113 function callbackAllPodsFinished (err) { 113 function callbackAllPodsFinished (err) {
114 if (err) return callback(err) 114 if (err) return callback(err)
115 115
116 updatePodsScore(good_pods, bad_pods) 116 updatePodsScore(goodPods, badPods)
117 callback(null) 117 callback(null)
118 } 118 }
119 }) 119 })
@@ -130,7 +130,7 @@ function makeRequests () {
130 130
131 if (requests.length === 0) return 131 if (requests.length === 0) return
132 132
133 const requests_to_make = { 133 const requestsToMake = {
134 add: { 134 add: {
135 ids: [], 135 ids: [],
136 requests: [] 136 requests: []
@@ -141,35 +141,35 @@ function makeRequests () {
141 } 141 }
142 } 142 }
143 143
144 async.each(requests, function (pool_request, callback_each) { 144 async.each(requests, function (poolRequest, callbackEach) {
145 if (pool_request.type === 'add') { 145 if (poolRequest.type === 'add') {
146 requests_to_make.add.requests.push(pool_request.request) 146 requestsToMake.add.requests.push(poolRequest.request)
147 requests_to_make.add.ids.push(pool_request._id) 147 requestsToMake.add.ids.push(poolRequest._id)
148 } else if (pool_request.type === 'remove') { 148 } else if (poolRequest.type === 'remove') {
149 requests_to_make.remove.requests.push(pool_request.request) 149 requestsToMake.remove.requests.push(poolRequest.request)
150 requests_to_make.remove.ids.push(pool_request._id) 150 requestsToMake.remove.ids.push(poolRequest._id)
151 } else { 151 } else {
152 logger.error('Unkown request type.', { request_type: pool_request.type }) 152 logger.error('Unkown request type.', { request_type: poolRequest.type })
153 return // abort 153 return // abort
154 } 154 }
155 155
156 callback_each() 156 callbackEach()
157 }, function () { 157 }, function () {
158 // Send the add requests 158 // Send the add requests
159 if (requests_to_make.add.requests.length !== 0) { 159 if (requestsToMake.add.requests.length !== 0) {
160 makeRequest('add', requests_to_make.add.requests, function (err) { 160 makeRequest('add', requestsToMake.add.requests, function (err) {
161 if (err) logger.error('Errors when sent add requests.', { error: err }) 161 if (err) logger.error('Errors when sent add requests.', { error: err })
162 162
163 Requests.removeRequests(requests_to_make.add.ids) 163 Requests.removeRequests(requestsToMake.add.ids)
164 }) 164 })
165 } 165 }
166 166
167 // Send the remove requests 167 // Send the remove requests
168 if (requests_to_make.remove.requests.length !== 0) { 168 if (requestsToMake.remove.requests.length !== 0) {
169 makeRequest('remove', requests_to_make.remove.requests, function (err) { 169 makeRequest('remove', requestsToMake.remove.requests, function (err) {
170 if (err) logger.error('Errors when sent remove pool requests.', { error: err }) 170 if (err) logger.error('Errors when sent remove pool requests.', { error: err })
171 171
172 Requests.removeRequests(requests_to_make.remove.ids) 172 Requests.removeRequests(requestsToMake.remove.ids)
173 }) 173 })
174 } 174 }
175 }) 175 })
@@ -188,11 +188,11 @@ function removeBadPods () {
188 const urls = map(pods, 'url') 188 const urls = map(pods, 'url')
189 const ids = map(pods, '_id') 189 const ids = map(pods, '_id')
190 190
191 Videos.listFromUrls(urls, function (err, videos_list) { 191 Videos.listFromUrls(urls, function (err, videosList) {
192 if (err) { 192 if (err) {
193 logger.error('Cannot list videos urls.', { error: err, urls: urls }) 193 logger.error('Cannot list videos urls.', { error: err, urls: urls })
194 } else { 194 } else {
195 videos.removeRemoteVideos(videos_list, function (err) { 195 videos.removeRemoteVideos(videosList, function (err) {
196 if (err) logger.error('Cannot remove remote videos.', { error: err }) 196 if (err) logger.error('Cannot remove remote videos.', { error: err })
197 }) 197 })
198 } 198 }
@@ -201,22 +201,22 @@ function removeBadPods () {
201 if (err) { 201 if (err) {
202 logger.error('Cannot remove bad pods.', { error: err }) 202 logger.error('Cannot remove bad pods.', { error: err })
203 } else { 203 } else {
204 const pods_removed = r.result.n 204 const podsRemoved = r.result.n
205 logger.info('Removed %d pods.', pods_removed) 205 logger.info('Removed %d pods.', podsRemoved)
206 } 206 }
207 }) 207 })
208 }) 208 })
209 }) 209 })
210} 210}
211 211
212function updatePodsScore (good_pods, bad_pods) { 212function updatePodsScore (goodPods, badPods) {
213 logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) 213 logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
214 214
215 Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS, function (err) { 215 Pods.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) {
216 if (err) logger.error('Cannot increment scores of good pods.') 216 if (err) logger.error('Cannot increment scores of good pods.')
217 }) 217 })
218 218
219 Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) { 219 Pods.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) {
220 if (err) logger.error('Cannot increment scores of bad pods.') 220 if (err) logger.error('Cannot increment scores of bad pods.')
221 removeBadPods() 221 removeBadPods()
222 }) 222 })
diff --git a/server/lib/videos.js b/server/lib/videos.js
index b3497743a..7da4b11d2 100644
--- a/server/lib/videos.js
+++ b/server/lib/videos.js
@@ -29,15 +29,15 @@ const videos = {
29 29
30function createRemoteVideos (videos, callback) { 30function createRemoteVideos (videos, callback) {
31 // Create the remote videos from the new pod 31 // Create the remote videos from the new pod
32 createRemoteVideoObjects(videos, function (err, remote_videos) { 32 createRemoteVideoObjects(videos, function (err, remoteVideos) {
33 if (err) return callback(err) 33 if (err) return callback(err)
34 34
35 Videos.addRemotes(remote_videos, callback) 35 Videos.addRemotes(remoteVideos, callback)
36 }) 36 })
37} 37}
38 38
39function getVideoDuration (video_path, callback) { 39function getVideoDuration (videoPath, callback) {
40 ffmpeg.ffprobe(video_path, function (err, metadata) { 40 ffmpeg.ffprobe(videoPath, function (err, metadata) {
41 if (err) return callback(err) 41 if (err) return callback(err)
42 42
43 return callback(null, Math.floor(metadata.format.duration)) 43 return callback(null, Math.floor(metadata.format.duration))
@@ -54,9 +54,9 @@ function getVideoState (video) {
54 return { exist: exist, owned: owned } 54 return { exist: exist, owned: owned }
55} 55}
56 56
57function getVideoThumbnail (video_path, callback) { 57function getVideoThumbnail (videoPath, callback) {
58 const filename = pathUtils.basename(video_path) + '.jpg' 58 const filename = pathUtils.basename(videoPath) + '.jpg'
59 ffmpeg(video_path) 59 ffmpeg(videoPath)
60 .on('error', callback) 60 .on('error', callback)
61 .on('end', function () { 61 .on('end', function () {
62 callback(null, filename) 62 callback(null, filename)
@@ -71,7 +71,7 @@ function getVideoThumbnail (video_path, callback) {
71 71
72// Remove video datas from disk (video file, thumbnail...) 72// Remove video datas from disk (video file, thumbnail...)
73function removeVideosDataFromDisk (videos, callback) { 73function removeVideosDataFromDisk (videos, callback) {
74 async.each(videos, function (video, callback_each) { 74 async.each(videos, function (video, callbackEach) {
75 fs.unlink(thumbnailsDir + video.thumbnail, function (err) { 75 fs.unlink(thumbnailsDir + video.thumbnail, function (err) {
76 if (err) logger.error('Cannot remove the video thumbnail') 76 if (err) logger.error('Cannot remove the video thumbnail')
77 77
@@ -79,13 +79,13 @@ function removeVideosDataFromDisk (videos, callback) {
79 fs.unlink(uploadDir + video.namePath, function (err) { 79 fs.unlink(uploadDir + video.namePath, function (err) {
80 if (err) { 80 if (err) {
81 logger.error('Cannot remove this video file.') 81 logger.error('Cannot remove this video file.')
82 return callback_each(err) 82 return callbackEach(err)
83 } 83 }
84 84
85 callback_each(null) 85 callbackEach(null)
86 }) 86 })
87 } else { 87 } else {
88 callback_each(null) 88 callbackEach(null)
89 } 89 }
90 }) 90 })
91 }, callback) 91 }, callback)
@@ -110,20 +110,20 @@ function seed (path, callback) {
110} 110}
111 111
112function seedAllExisting (callback) { 112function seedAllExisting (callback) {
113 Videos.listOwned(function (err, videos_list) { 113 Videos.listOwned(function (err, videosList) {
114 if (err) { 114 if (err) {
115 logger.error('Cannot get list of the videos to seed.') 115 logger.error('Cannot get list of the videos to seed.')
116 return callback(err) 116 return callback(err)
117 } 117 }
118 118
119 async.each(videos_list, function (video, each_callback) { 119 async.each(videosList, function (video, callbackEach) {
120 seed(uploadDir + video.namePath, function (err) { 120 seed(uploadDir + video.namePath, function (err) {
121 if (err) { 121 if (err) {
122 logger.error('Cannot seed this video.') 122 logger.error('Cannot seed this video.')
123 return callback(err) 123 return callback(err)
124 } 124 }
125 125
126 each_callback(null) 126 callbackEach(null)
127 }) 127 })
128 }, callback) 128 }, callback)
129 }) 129 })
@@ -136,16 +136,16 @@ module.exports = videos
136// --------------------------------------------------------------------------- 136// ---------------------------------------------------------------------------
137 137
138function createRemoteVideoObjects (videos, callback) { 138function createRemoteVideoObjects (videos, callback) {
139 const remote_videos = [] 139 const remoteVideos = []
140 140
141 async.each(videos, function (video, callback_each) { 141 async.each(videos, function (video, callbackEach) {
142 // Creating the thumbnail for this remote video 142 // Creating the thumbnail for this remote video
143 utils.generateRandomString(16, function (err, random_string) { 143 utils.generateRandomString(16, function (err, randomString) {
144 if (err) return callback_each(err) 144 if (err) return callbackEach(err)
145 145
146 const thumbnail_name = random_string + '.jpg' 146 const thumbnailName = randomString + '.jpg'
147 createThumbnailFromBase64(thumbnail_name, video.thumbnail_base64, function (err) { 147 createThumbnailFromBase64(thumbnailName, video.thumbnailBase64, function (err) {
148 if (err) return callback_each(err) 148 if (err) return callbackEach(err)
149 149
150 const params = { 150 const params = {
151 name: video.name, 151 name: video.name,
@@ -153,21 +153,21 @@ function createRemoteVideoObjects (videos, callback) {
153 magnetUri: video.magnetUri, 153 magnetUri: video.magnetUri,
154 podUrl: video.podUrl, 154 podUrl: video.podUrl,
155 duration: video.duration, 155 duration: video.duration,
156 thumbnail: thumbnail_name 156 thumbnail: thumbnailName
157 } 157 }
158 remote_videos.push(params) 158 remoteVideos.push(params)
159 159
160 callback_each(null) 160 callbackEach(null)
161 }) 161 })
162 }) 162 })
163 }, 163 },
164 function (err) { 164 function (err) {
165 if (err) return callback(err) 165 if (err) return callback(err)
166 166
167 callback(null, remote_videos) 167 callback(null, remoteVideos)
168 }) 168 })
169} 169}
170 170
171function createThumbnailFromBase64 (thumbnail_name, data, callback) { 171function createThumbnailFromBase64 (thumbnailName, data, callback) {
172 fs.writeFile(thumbnailsDir + thumbnail_name, data, { encoding: 'base64' }, callback) 172 fs.writeFile(thumbnailsDir + thumbnailName, data, { encoding: 'base64' }, callback)
173} 173}
diff --git a/server/lib/webtorrent.js b/server/lib/webtorrent.js
index 656f8c7a8..fe2ee357f 100644
--- a/server/lib/webtorrent.js
+++ b/server/lib/webtorrent.js
@@ -7,7 +7,7 @@ const spawn = require('electron-spawn')
7 7
8const logger = require('../helpers/logger') 8const logger = require('../helpers/logger')
9 9
10const electron_debug = config.get('electron.debug') 10const electronDebug = config.get('electron.debug')
11let host = config.get('webserver.host') 11let host = config.get('webserver.host')
12let port = config.get('webserver.port') 12let port = config.get('webserver.port')
13let nodeKey = 'webtorrentnode' + port 13let nodeKey = 'webtorrentnode' + port
@@ -43,13 +43,13 @@ function create (options, callback) {
43 if (!webtorrent.silent) logger.info('IPC server ready.') 43 if (!webtorrent.silent) logger.info('IPC server ready.')
44 44
45 // Run a timeout of 30s after which we exit the process 45 // Run a timeout of 30s after which we exit the process
46 const timeout_webtorrent_process = setTimeout(function () { 46 const timeoutWebtorrentProcess = setTimeout(function () {
47 throw new Error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.') 47 throw new Error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.')
48 }, 30000) 48 }, 30000)
49 49
50 ipc.server.on(processKey + '.ready', function () { 50 ipc.server.on(processKey + '.ready', function () {
51 if (!webtorrent.silent) logger.info('Webtorrent process ready.') 51 if (!webtorrent.silent) logger.info('Webtorrent process ready.')
52 clearTimeout(timeout_webtorrent_process) 52 clearTimeout(timeoutWebtorrentProcess)
53 callback() 53 callback()
54 }) 54 })
55 55
@@ -57,19 +57,19 @@ function create (options, callback) {
57 throw new Error('Received exception error from webtorrent process : ' + data.exception) 57 throw new Error('Received exception error from webtorrent process : ' + data.exception)
58 }) 58 })
59 59
60 const webtorrent_process = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true }) 60 const webtorrentProcess = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true })
61 61
62 if (electron_debug === true) { 62 if (electronDebug === true) {
63 webtorrent_process.stderr.on('data', function (data) { 63 webtorrentProcess.stderr.on('data', function (data) {
64 logger.debug('Webtorrent process stderr: ', data.toString()) 64 logger.debug('Webtorrent process stderr: ', data.toString())
65 }) 65 })
66 66
67 webtorrent_process.stdout.on('data', function (data) { 67 webtorrentProcess.stdout.on('data', function (data) {
68 logger.debug('Webtorrent process:', data.toString()) 68 logger.debug('Webtorrent process:', data.toString())
69 }) 69 })
70 } 70 }
71 71
72 webtorrent.app = webtorrent_process 72 webtorrent.app = webtorrentProcess
73 }) 73 })
74 74
75 ipc.server.start() 75 ipc.server.start()
@@ -88,8 +88,8 @@ function seed (path, callback) {
88 if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id) 88 if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id)
89 89
90 // Finish signal 90 // Finish signal
91 const event_key = nodeKey + '.seedDone.' + data._id 91 const eventKey = nodeKey + '.seedDone.' + data._id
92 ipc.server.on(event_key, function listener (received) { 92 ipc.server.on(eventKey, function listener (received) {
93 if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri) 93 if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri)
94 94
95 // This is a fake object, we just use the magnetUri in this project 95 // This is a fake object, we just use the magnetUri in this project
@@ -97,7 +97,7 @@ function seed (path, callback) {
97 magnetURI: received.magnetUri 97 magnetURI: received.magnetUri
98 } 98 }
99 99
100 ipc.server.off(event_key) 100 ipc.server.off(eventKey)
101 callback(torrent) 101 callback(torrent)
102 }) 102 })
103 103
@@ -115,8 +115,8 @@ function add (magnetUri, callback) {
115 if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id) 115 if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id)
116 116
117 // Finish signal 117 // Finish signal
118 const event_key = nodeKey + '.addDone.' + data._id 118 const eventKey = nodeKey + '.addDone.' + data._id
119 ipc.server.on(event_key, function (received) { 119 ipc.server.on(eventKey, function (received) {
120 if (!webtorrent.silent) logger.debug('Process added torrent.') 120 if (!webtorrent.silent) logger.debug('Process added torrent.')
121 121
122 // This is a fake object, we just use the magnetUri in this project 122 // This is a fake object, we just use the magnetUri in this project
@@ -124,7 +124,7 @@ function add (magnetUri, callback) {
124 files: received.files 124 files: received.files
125 } 125 }
126 126
127 ipc.server.off(event_key) 127 ipc.server.off(eventKey)
128 callback(torrent) 128 callback(torrent)
129 }) 129 })
130 130
@@ -142,14 +142,14 @@ function remove (magnetUri, callback) {
142 if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id) 142 if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id)
143 143
144 // Finish signal 144 // Finish signal
145 const event_key = nodeKey + '.removeDone.' + data._id 145 const eventKey = nodeKey + '.removeDone.' + data._id
146 ipc.server.on(event_key, function (received) { 146 ipc.server.on(eventKey, function (received) {
147 if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id) 147 if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id)
148 148
149 let err = null 149 let err = null
150 if (received.err) err = received.err 150 if (received.err) err = received.err
151 151
152 ipc.server.off(event_key) 152 ipc.server.off(eventKey)
153 callback(err) 153 callback(err)
154 }) 154 })
155 155
diff --git a/server/lib/webtorrentProcess.js b/server/lib/webtorrentProcess.js
index 7889e7128..be7ac5bb4 100644
--- a/server/lib/webtorrentProcess.js
+++ b/server/lib/webtorrentProcess.js
@@ -26,11 +26,11 @@ function webtorrent (args) {
26 const _id = data._id 26 const _id = data._id
27 27
28 wt.seed(path, { announceList: '' }, function (torrent) { 28 wt.seed(path, { announceList: '' }, function (torrent) {
29 const to_send = { 29 const toSend = {
30 magnetUri: torrent.magnetURI 30 magnetUri: torrent.magnetURI
31 } 31 }
32 32
33 ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, to_send) 33 ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, toSend)
34 }) 34 })
35 } 35 }
36 36
@@ -40,15 +40,15 @@ function webtorrent (args) {
40 const _id = data._id 40 const _id = data._id
41 41
42 wt.add(magnetUri, function (torrent) { 42 wt.add(magnetUri, function (torrent) {
43 const to_send = { 43 const toSend = {
44 files: [] 44 files: []
45 } 45 }
46 46
47 torrent.files.forEach(function (file) { 47 torrent.files.forEach(function (file) {
48 to_send.files.push({ path: file.path }) 48 toSend.files.push({ path: file.path })
49 }) 49 })
50 50
51 ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, to_send) 51 ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, toSend)
52 }) 52 })
53 } 53 }
54 54
@@ -65,8 +65,8 @@ function webtorrent (args) {
65 } 65 }
66 66
67 function callback () { 67 function callback () {
68 const to_send = {} 68 const toSend = {}
69 ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, to_send) 69 ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, toSend)
70 } 70 }
71 } 71 }
72 72