aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/friends.js62
-rw-r--r--server/lib/poolRequests.js36
-rw-r--r--server/lib/videos.js21
-rw-r--r--server/lib/webtorrent.js46
-rw-r--r--server/lib/webtorrentProcess.js38
5 files changed, 101 insertions, 102 deletions
diff --git a/server/lib/friends.js b/server/lib/friends.js
index 006a64404..c3c231604 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -1,23 +1,23 @@
1'use strict' 1'use strict'
2 2
3var async = require('async') 3const async = require('async')
4var config = require('config') 4const config = require('config')
5var fs = require('fs') 5const fs = require('fs')
6var request = require('request') 6const request = require('request')
7 7
8var constants = require('../initializers/constants') 8const constants = require('../initializers/constants')
9var logger = require('../helpers/logger') 9const logger = require('../helpers/logger')
10var peertubeCrypto = require('../helpers/peertubeCrypto') 10const peertubeCrypto = require('../helpers/peertubeCrypto')
11var Pods = require('../models/pods') 11const Pods = require('../models/pods')
12var poolRequests = require('../lib/poolRequests') 12const poolRequests = require('../lib/poolRequests')
13var requests = require('../helpers/requests') 13const requests = require('../helpers/requests')
14var Videos = require('../models/videos') 14const Videos = require('../models/videos')
15 15
16var http = config.get('webserver.https') ? 'https' : 'http' 16const http = config.get('webserver.https') ? 'https' : 'http'
17var host = config.get('webserver.host') 17const host = config.get('webserver.host')
18var port = config.get('webserver.port') 18const port = config.get('webserver.port')
19 19
20var pods = { 20const pods = {
21 addVideoToFriends: addVideoToFriends, 21 addVideoToFriends: addVideoToFriends,
22 hasFriends: hasFriends, 22 hasFriends: hasFriends,
23 makeFriends: makeFriends, 23 makeFriends: makeFriends,
@@ -27,7 +27,7 @@ var pods = {
27 27
28function addVideoToFriends (video) { 28function addVideoToFriends (video) {
29 // To avoid duplicates 29 // To avoid duplicates
30 var id = video.name + video.magnetUri 30 const id = video.name + video.magnetUri
31 // ensure namePath is null 31 // ensure namePath is null
32 video.namePath = null 32 video.namePath = null
33 poolRequests.addRequest(id, 'add', video) 33 poolRequests.addRequest(id, 'add', video)
@@ -37,13 +37,13 @@ function hasFriends (callback) {
37 Pods.count(function (err, count) { 37 Pods.count(function (err, count) {
38 if (err) return callback(err) 38 if (err) return callback(err)
39 39
40 var has_friends = (count !== 0) 40 const has_friends = (count !== 0)
41 callback(null, has_friends) 41 callback(null, has_friends)
42 }) 42 })
43} 43}
44 44
45function makeFriends (callback) { 45function makeFriends (callback) {
46 var pods_score = {} 46 const pods_score = {}
47 47
48 logger.info('Make friends!') 48 logger.info('Make friends!')
49 fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { 49 fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
@@ -52,7 +52,7 @@ function makeFriends (callback) {
52 return callback(err) 52 return callback(err)
53 } 53 }
54 54
55 var urls = config.get('network.friends') 55 const urls = config.get('network.friends')
56 56
57 async.each(urls, function (url, callback) { 57 async.each(urls, function (url, callback) {
58 computeForeignPodsList(url, pods_score, callback) 58 computeForeignPodsList(url, pods_score, callback)
@@ -60,8 +60,8 @@ function makeFriends (callback) {
60 if (err) return callback(err) 60 if (err) return callback(err)
61 61
62 logger.debug('Pods scores computed.', { pods_score: pods_score }) 62 logger.debug('Pods scores computed.', { pods_score: pods_score })
63 var pods_list = computeWinningPods(urls, pods_score) 63 const pods_list = computeWinningPods(urls, pods_score)
64 logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list }) 64 logger.debug('Pods that we keep.', { pods_to_keep: pods_list })
65 65
66 makeRequestsToWinningPods(cert, pods_list, callback) 66 makeRequestsToWinningPods(cert, pods_list, callback)
67 }) 67 })
@@ -77,7 +77,7 @@ function quitFriends (callback) {
77 Pods.list(function (err, pods) { 77 Pods.list(function (err, pods) {
78 if (err) return callback(err) 78 if (err) return callback(err)
79 79
80 var request = { 80 const request = {
81 method: 'POST', 81 method: 'POST',
82 path: '/api/' + constants.API_VERSION + '/pods/remove', 82 path: '/api/' + constants.API_VERSION + '/pods/remove',
83 sign: true, 83 sign: true,
@@ -109,7 +109,7 @@ function quitFriends (callback) {
109 109
110function removeVideoToFriends (video) { 110function removeVideoToFriends (video) {
111 // To avoid duplicates 111 // To avoid duplicates
112 var id = video.name + video.magnetUri 112 const id = video.name + video.magnetUri
113 poolRequests.addRequest(id, 'remove', video) 113 poolRequests.addRequest(id, 'remove', video)
114} 114}
115 115
@@ -128,7 +128,7 @@ function computeForeignPodsList (url, pods_score, callback) {
128 if (foreign_pods_list.length === 0) return callback() 128 if (foreign_pods_list.length === 0) return callback()
129 129
130 async.each(foreign_pods_list, function (foreign_pod, callback_each) { 130 async.each(foreign_pods_list, function (foreign_pod, callback_each) {
131 var foreign_url = foreign_pod.url 131 const foreign_url = foreign_pod.url
132 132
133 if (pods_score[foreign_url]) pods_score[foreign_url]++ 133 if (pods_score[foreign_url]) pods_score[foreign_url]++
134 else pods_score[foreign_url] = 1 134 else pods_score[foreign_url] = 1
@@ -143,8 +143,8 @@ function computeForeignPodsList (url, pods_score, callback) {
143function computeWinningPods (urls, pods_score) { 143function computeWinningPods (urls, pods_score) {
144 // Build the list of pods to add 144 // Build the list of pods to add
145 // Only add a pod if it exists in more than a half base pods 145 // Only add a pod if it exists in more than a half base pods
146 var pods_list = [] 146 const pods_list = []
147 var base_score = urls.length / 2 147 const base_score = urls.length / 2
148 Object.keys(pods_score).forEach(function (pod) { 148 Object.keys(pods_score).forEach(function (pod) {
149 if (pods_score[pod] > base_score) pods_list.push({ url: pod }) 149 if (pods_score[pod] > base_score) pods_list.push({ url: pod })
150 }) 150 })
@@ -153,7 +153,7 @@ function computeWinningPods (urls, pods_score) {
153} 153}
154 154
155function getForeignPodsList (url, callback) { 155function getForeignPodsList (url, callback) {
156 var path = '/api/' + constants.API_VERSION + '/pods' 156 const path = '/api/' + constants.API_VERSION + '/pods'
157 157
158 request.get(url + path, function (err, response, body) { 158 request.get(url + path, function (err, response, body) {
159 if (err) return callback(err) 159 if (err) return callback(err)
@@ -175,7 +175,7 @@ function makeRequestsToWinningPods (cert, pods_list, callback) {
175 return callback(err) 175 return callback(err)
176 } 176 }
177 177
178 var data = { 178 const data = {
179 url: http + '://' + host + ':' + port, 179 url: http + '://' + host + ':' + port,
180 publicKey: cert, 180 publicKey: cert,
181 videos: videos_list 181 videos: videos_list
diff --git a/server/lib/poolRequests.js b/server/lib/poolRequests.js
index f786c3c7a..2eb501311 100644
--- a/server/lib/poolRequests.js
+++ b/server/lib/poolRequests.js
@@ -1,18 +1,18 @@
1'use strict' 1'use strict'
2 2
3var async = require('async') 3const async = require('async')
4var pluck = require('lodash-node/compat/collection/pluck') 4const pluck = require('lodash-node/compat/collection/pluck')
5 5
6var constants = require('../initializers/constants') 6const constants = require('../initializers/constants')
7var logger = require('../helpers/logger') 7const logger = require('../helpers/logger')
8var Pods = require('../models/pods') 8const Pods = require('../models/pods')
9var PoolRequests = require('../models/poolRequests') 9const PoolRequests = require('../models/poolRequests')
10var requests = require('../helpers/requests') 10const requests = require('../helpers/requests')
11var Videos = require('../models/videos') 11const Videos = require('../models/videos')
12 12
13var timer = null 13let timer = null
14 14
15var poolRequests = { 15const poolRequests = {
16 activate: activate, 16 activate: activate,
17 addRequest: addRequest, 17 addRequest: addRequest,
18 deactivate: deactivate, 18 deactivate: deactivate,
@@ -77,7 +77,7 @@ function makePoolRequest (type, requests_to_make, callback) {
77 Pods.list(function (err, pods) { 77 Pods.list(function (err, pods) {
78 if (err) return callback(err) 78 if (err) return callback(err)
79 79
80 var params = { 80 const params = {
81 encrypt: true, 81 encrypt: true,
82 sign: true, 82 sign: true,
83 method: 'POST', 83 method: 'POST',
@@ -93,8 +93,8 @@ function makePoolRequest (type, requests_to_make, callback) {
93 return callback(new Error('Unkown pool request type.')) 93 return callback(new Error('Unkown pool request type.'))
94 } 94 }
95 95
96 var bad_pods = [] 96 const bad_pods = []
97 var good_pods = [] 97 const good_pods = []
98 98
99 requests.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished) 99 requests.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished)
100 100
@@ -129,7 +129,7 @@ function makePoolRequests () {
129 129
130 if (pool_requests.length === 0) return 130 if (pool_requests.length === 0) return
131 131
132 var requests_to_make = { 132 const requests_to_make = {
133 add: { 133 add: {
134 ids: [], 134 ids: [],
135 requests: [] 135 requests: []
@@ -184,14 +184,14 @@ function removeBadPods () {
184 184
185 if (pods.length === 0) return 185 if (pods.length === 0) return
186 186
187 var urls = pluck(pods, 'url') 187 const urls = pluck(pods, 'url')
188 var ids = pluck(pods, '_id') 188 const ids = pluck(pods, '_id')
189 189
190 Videos.removeAllRemotesOf(urls, function (err, r) { 190 Videos.removeAllRemotesOf(urls, function (err, r) {
191 if (err) { 191 if (err) {
192 logger.error('Cannot remove videos from a pod that we removing.', { error: err }) 192 logger.error('Cannot remove videos from a pod that we removing.', { error: err })
193 } else { 193 } else {
194 var videos_removed = r.result.n 194 const videos_removed = r.result.n
195 logger.info('Removed %d videos.', videos_removed) 195 logger.info('Removed %d videos.', videos_removed)
196 } 196 }
197 197
@@ -199,7 +199,7 @@ function removeBadPods () {
199 if (err) { 199 if (err) {
200 logger.error('Cannot remove bad pods.', { error: err }) 200 logger.error('Cannot remove bad pods.', { error: err })
201 } else { 201 } else {
202 var pods_removed = r.result.n 202 const pods_removed = r.result.n
203 logger.info('Removed %d pods.', pods_removed) 203 logger.info('Removed %d pods.', pods_removed)
204 } 204 }
205 }) 205 })
diff --git a/server/lib/videos.js b/server/lib/videos.js
index a5fe7b0c4..eb3a0125a 100644
--- a/server/lib/videos.js
+++ b/server/lib/videos.js
@@ -1,25 +1,24 @@
1'use strict' 1'use strict'
2 2
3var async = require('async') 3const async = require('async')
4var config = require('config') 4const config = require('config')
5// TODO 5const pathUtils = require('path')
6var path = require('path') 6const webtorrent = require('../lib/webtorrent')
7var webtorrent = require('../lib/webtorrent')
8 7
9var logger = require('../helpers/logger') 8const logger = require('../helpers/logger')
10var Videos = require('../models/videos') 9const Videos = require('../models/videos')
11 10
12var uploadDir = path.join(__dirname, '..', '..', config.get('storage.uploads')) 11const uploadDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads'))
13 12
14var videos = { 13const videos = {
15 getVideoState: getVideoState, 14 getVideoState: getVideoState,
16 seed: seed, 15 seed: seed,
17 seedAllExisting: seedAllExisting 16 seedAllExisting: seedAllExisting
18} 17}
19 18
20function getVideoState (video, callback) { 19function getVideoState (video, callback) {
21 var exist = (video !== null) 20 const exist = (video !== null)
22 var owned = false 21 let owned = false
23 if (exist === true) { 22 if (exist === true) {
24 owned = (video.namePath !== null) 23 owned = (video.namePath !== null)
25 } 24 }
diff --git a/server/lib/webtorrent.js b/server/lib/webtorrent.js
index cb641fead..455b086a3 100644
--- a/server/lib/webtorrent.js
+++ b/server/lib/webtorrent.js
@@ -1,20 +1,20 @@
1'use strict' 1'use strict'
2 2
3var config = require('config') 3const config = require('config')
4var ipc = require('node-ipc') 4const ipc = require('node-ipc')
5var pathUtils = require('path') 5const pathUtils = require('path')
6var spawn = require('electron-spawn') 6const spawn = require('electron-spawn')
7 7
8var logger = require('../helpers/logger') 8const logger = require('../helpers/logger')
9 9
10var host = config.get('webserver.host') 10let host = config.get('webserver.host')
11var port = config.get('webserver.port') 11let port = config.get('webserver.port')
12var nodeKey = 'webtorrentnode' + port 12let nodeKey = 'webtorrentnode' + port
13var processKey = 'webtorrentprocess' + port 13let processKey = 'webtorrentprocess' + port
14ipc.config.silent = true 14ipc.config.silent = true
15ipc.config.id = nodeKey 15ipc.config.id = nodeKey
16 16
17var webtorrent = { 17const webtorrent = {
18 add: add, 18 add: add,
19 app: null, // Pid of the app 19 app: null, // Pid of the app
20 create: create, 20 create: create,
@@ -42,7 +42,7 @@ function create (options, callback) {
42 if (!webtorrent.silent) logger.info('IPC server ready.') 42 if (!webtorrent.silent) logger.info('IPC server ready.')
43 43
44 // Run a timeout of 30s after which we exit the process 44 // Run a timeout of 30s after which we exit the process
45 var timeout_webtorrent_process = setTimeout(function () { 45 const timeout_webtorrent_process = setTimeout(function () {
46 throw new Error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.') 46 throw new Error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.')
47 }, 30000) 47 }, 30000)
48 48
@@ -56,7 +56,7 @@ function create (options, callback) {
56 throw new Error('Received exception error from webtorrent process.' + data.exception) 56 throw new Error('Received exception error from webtorrent process.' + data.exception)
57 }) 57 })
58 58
59 var webtorrent_process = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true }) 59 const webtorrent_process = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true })
60 webtorrent_process.stderr.on('data', function (data) { 60 webtorrent_process.stderr.on('data', function (data) {
61 // logger.debug('Webtorrent process stderr: ', data.toString()) 61 // logger.debug('Webtorrent process stderr: ', data.toString())
62 }) 62 })
@@ -72,9 +72,9 @@ function create (options, callback) {
72} 72}
73 73
74function seed (path, callback) { 74function seed (path, callback) {
75 var extension = pathUtils.extname(path) 75 const extension = pathUtils.extname(path)
76 var basename = pathUtils.basename(path, extension) 76 const basename = pathUtils.basename(path, extension)
77 var data = { 77 const data = {
78 _id: basename, 78 _id: basename,
79 args: { 79 args: {
80 path: path 80 path: path
@@ -84,12 +84,12 @@ function seed (path, callback) {
84 if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id) 84 if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id)
85 85
86 // Finish signal 86 // Finish signal
87 var event_key = nodeKey + '.seedDone.' + data._id 87 const event_key = nodeKey + '.seedDone.' + data._id
88 ipc.server.on(event_key, function listener (received) { 88 ipc.server.on(event_key, function listener (received) {
89 if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri) 89 if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri)
90 90
91 // This is a fake object, we just use the magnetUri in this project 91 // This is a fake object, we just use the magnetUri in this project
92 var torrent = { 92 const torrent = {
93 magnetURI: received.magnetUri 93 magnetURI: received.magnetUri
94 } 94 }
95 95
@@ -101,7 +101,7 @@ function seed (path, callback) {
101} 101}
102 102
103function add (magnetUri, callback) { 103function add (magnetUri, callback) {
104 var data = { 104 const data = {
105 _id: magnetUri, 105 _id: magnetUri,
106 args: { 106 args: {
107 magnetUri: magnetUri 107 magnetUri: magnetUri
@@ -111,12 +111,12 @@ function add (magnetUri, callback) {
111 if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id) 111 if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id)
112 112
113 // Finish signal 113 // Finish signal
114 var event_key = nodeKey + '.addDone.' + data._id 114 const event_key = nodeKey + '.addDone.' + data._id
115 ipc.server.on(event_key, function (received) { 115 ipc.server.on(event_key, function (received) {
116 if (!webtorrent.silent) logger.debug('Process added torrent.') 116 if (!webtorrent.silent) logger.debug('Process added torrent.')
117 117
118 // This is a fake object, we just use the magnetUri in this project 118 // This is a fake object, we just use the magnetUri in this project
119 var torrent = { 119 const torrent = {
120 files: received.files 120 files: received.files
121 } 121 }
122 122
@@ -128,7 +128,7 @@ function add (magnetUri, callback) {
128} 128}
129 129
130function remove (magnetUri, callback) { 130function remove (magnetUri, callback) {
131 var data = { 131 const data = {
132 _id: magnetUri, 132 _id: magnetUri,
133 args: { 133 args: {
134 magnetUri: magnetUri 134 magnetUri: magnetUri
@@ -138,11 +138,11 @@ function remove (magnetUri, callback) {
138 if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id) 138 if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id)
139 139
140 // Finish signal 140 // Finish signal
141 var event_key = nodeKey + '.removeDone.' + data._id 141 const event_key = nodeKey + '.removeDone.' + data._id
142 ipc.server.on(event_key, function (received) { 142 ipc.server.on(event_key, function (received) {
143 if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id) 143 if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id)
144 144
145 var err = null 145 let err = null
146 if (received.err) err = received.err 146 if (received.err) err = received.err
147 147
148 ipc.server.off(event_key) 148 ipc.server.off(event_key)
diff --git a/server/lib/webtorrentProcess.js b/server/lib/webtorrentProcess.js
index 7da52523a..c68e97ee4 100644
--- a/server/lib/webtorrentProcess.js
+++ b/server/lib/webtorrentProcess.js
@@ -1,32 +1,32 @@
1'use strict' 1'use strict'
2 2
3var WebTorrent = require('webtorrent') 3const WebTorrent = require('webtorrent')
4var ipc = require('node-ipc') 4const ipc = require('node-ipc')
5 5
6function webtorrent (args) { 6function webtorrent (args) {
7 if (args.length !== 3) { 7 if (args.length !== 3) {
8 throw new Error('Wrong arguments number: ' + args.length + '/3') 8 throw new Error('Wrong arguments number: ' + args.length + '/3')
9 } 9 }
10 10
11 var host = args[1] 11 const host = args[1]
12 var port = args[2] 12 const port = args[2]
13 var nodeKey = 'webtorrentnode' + port 13 const nodeKey = 'webtorrentnode' + port
14 var processKey = 'webtorrentprocess' + port 14 const processKey = 'webtorrentprocess' + port
15 15
16 ipc.config.silent = true 16 ipc.config.silent = true
17 ipc.config.id = processKey 17 ipc.config.id = processKey
18 18
19 if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = [] 19 if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = []
20 else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket' 20 else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket'
21 var wt = new WebTorrent({ dht: false }) 21 const wt = new WebTorrent({ dht: false })
22 22
23 function seed (data) { 23 function seed (data) {
24 var args = data.args 24 const args = data.args
25 var path = args.path 25 const path = args.path
26 var _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 var to_send = { 29 const to_send = {
30 magnetUri: torrent.magnetURI 30 magnetUri: torrent.magnetURI
31 } 31 }
32 32
@@ -35,12 +35,12 @@ function webtorrent (args) {
35 } 35 }
36 36
37 function add (data) { 37 function add (data) {
38 var args = data.args 38 const args = data.args
39 var magnetUri = args.magnetUri 39 const magnetUri = args.magnetUri
40 var _id = data._id 40 const _id = data._id
41 41
42 wt.add(magnetUri, function (torrent) { 42 wt.add(magnetUri, function (torrent) {
43 var to_send = { 43 const to_send = {
44 files: [] 44 files: []
45 } 45 }
46 46
@@ -53,9 +53,9 @@ function webtorrent (args) {
53 } 53 }
54 54
55 function remove (data) { 55 function remove (data) {
56 var args = data.args 56 const args = data.args
57 var magnetUri = args.magnetUri 57 const magnetUri = args.magnetUri
58 var _id = data._id 58 const _id = data._id
59 59
60 try { 60 try {
61 wt.remove(magnetUri, callback) 61 wt.remove(magnetUri, callback)
@@ -65,7 +65,7 @@ function webtorrent (args) {
65 } 65 }
66 66
67 function callback () { 67 function callback () {
68 var to_send = {} 68 const to_send = {}
69 ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, to_send) 69 ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, to_send)
70 } 70 }
71 } 71 }