aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/customValidators.js48
-rw-r--r--server/helpers/requests.js114
2 files changed, 72 insertions, 90 deletions
diff --git a/server/helpers/customValidators.js b/server/helpers/customValidators.js
index 9c3ff38ef..a6cf680e5 100644
--- a/server/helpers/customValidators.js
+++ b/server/helpers/customValidators.js
@@ -7,8 +7,7 @@ const VIDEOS_CONSTRAINTS_FIELDS = constants.VIDEOS_CONSTRAINTS_FIELDS
7 7
8const customValidators = { 8const customValidators = {
9 exists: exists, 9 exists: exists,
10 isEachAddRemoteVideosValid: isEachAddRemoteVideosValid, 10 isEachRemoteVideosValid: isEachRemoteVideosValid,
11 isEachRemoveRemoteVideosValid: isEachRemoveRemoteVideosValid,
12 isArray: isArray, 11 isArray: isArray,
13 isVideoAuthorValid: isVideoAuthorValid, 12 isVideoAuthorValid: isVideoAuthorValid,
14 isVideoDateValid: isVideoDateValid, 13 isVideoDateValid: isVideoDateValid,
@@ -25,23 +24,26 @@ function exists (value) {
25 return value !== undefined && value !== null 24 return value !== undefined && value !== null
26} 25}
27 26
28function isEachAddRemoteVideosValid (videos) { 27function isEachRemoteVideosValid (requests) {
29 return videos.every(function (video) { 28 return requests.every(function (request) {
30 return isVideoAuthorValid(video.author) && 29 const video = request.data
31 isVideoDateValid(video.createdDate) && 30 return (
32 isVideoDescriptionValid(video.description) && 31 isRequestTypeAddValid(request.type) &&
33 isVideoDurationValid(video.duration) && 32 isVideoAuthorValid(video.author) &&
34 isVideoMagnetUriValid(video.magnetUri) && 33 isVideoDateValid(video.createdDate) &&
35 isVideoNameValid(video.name) && 34 isVideoDescriptionValid(video.description) &&
36 isVideoPodUrlValid(video.podUrl) && 35 isVideoDurationValid(video.duration) &&
37 isVideoTagsValid(video.tags) && 36 isVideoMagnetUriValid(video.magnetUri) &&
38 isVideoThumbnailValid(video.thumbnailBase64) 37 isVideoNameValid(video.name) &&
39 }) 38 isVideoPodUrlValid(video.podUrl) &&
40} 39 isVideoTagsValid(video.tags) &&
41 40 isVideoThumbnailValid(video.thumbnailBase64)
42function isEachRemoveRemoteVideosValid (videos) { 41 ) ||
43 return videos.every(function (video) { 42 (
44 return isVideoMagnetUriValid(video.magnetUri) 43 isRequestTypeRemoveValid(request.type) &&
44 isVideoNameValid(video.name) &&
45 isVideoMagnetUriValid(video.magnetUri)
46 )
45 }) 47 })
46} 48}
47 49
@@ -49,6 +51,14 @@ function isArray (value) {
49 return Array.isArray(value) 51 return Array.isArray(value)
50} 52}
51 53
54function isRequestTypeAddValid (value) {
55 return value === 'add'
56}
57
58function isRequestTypeRemoveValid (value) {
59 return value === 'remove'
60}
61
52function isVideoAuthorValid (value) { 62function isVideoAuthorValid (value) {
53 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR) 63 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR)
54} 64}
diff --git a/server/helpers/requests.js b/server/helpers/requests.js
index 1e1bb4111..871342d60 100644
--- a/server/helpers/requests.js
+++ b/server/helpers/requests.js
@@ -1,12 +1,10 @@
1'use strict' 1'use strict'
2 2
3const async = require('async')
4const config = require('config') 3const config = require('config')
5const request = require('request')
6const replay = require('request-replay') 4const replay = require('request-replay')
5const request = require('request')
7 6
8const constants = require('../initializers/constants') 7const constants = require('../initializers/constants')
9const logger = require('./logger')
10const peertubeCrypto = require('./peertubeCrypto') 8const peertubeCrypto = require('./peertubeCrypto')
11 9
12const http = config.get('webserver.https') ? 'https' : 'http' 10const http = config.get('webserver.https') ? 'https' : 'http'
@@ -14,93 +12,67 @@ const host = config.get('webserver.host')
14const port = config.get('webserver.port') 12const port = config.get('webserver.port')
15 13
16const requests = { 14const requests = {
17 makeMultipleRetryRequest: makeMultipleRetryRequest 15 makeRetryRequest: makeRetryRequest,
16 makeSecureRequest: makeSecureRequest
18} 17}
19 18
20function makeMultipleRetryRequest (allData, pods, callbackEach, callback) { 19function makeRetryRequest (params, callback) {
21 if (!callback) { 20 replay(
22 callback = callbackEach 21 request(params, callback),
23 callbackEach = null 22 {
24 } 23 retries: constants.RETRY_REQUESTS,
24 factor: 3,
25 maxTimeout: Infinity,
26 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
27 }
28 )
29}
25 30
26 const url = http + '://' + host + ':' + port 31function makeSecureRequest (params, callback) {
27 let signature 32 const myUrl = http + '://' + host + ':' + port
28 33
29 // Add signature if it is specified in the params 34 const requestParams = {
30 if (allData.method === 'POST' && allData.data && allData.sign === true) { 35 url: params.toPod.url + params.path
31 signature = peertubeCrypto.sign(url)
32 } 36 }
33 37
34 // Make a request for each pod 38 // Add data with POST requst ?
35 async.each(pods, function (pod, callbackEachAsync) { 39 if (params.method === 'POST') {
36 function callbackEachRetryRequest (err, response, body, url, pod) { 40 requestParams.json = {}
37 if (callbackEach !== null) {
38 callbackEach(err, response, body, url, pod, function () {
39 callbackEachAsync()
40 })
41 } else {
42 callbackEachAsync()
43 }
44 }
45 41
46 const params = { 42 // Add signature if it is specified in the params
47 url: pod.url + allData.path, 43 if (params.sign === true) {
48 method: allData.method 44 requestParams.json.signature = {
45 url: myUrl,
46 signature: peertubeCrypto.sign(myUrl)
47 }
49 } 48 }
50 49
51 // Add data with POST requst ? 50 // If there are data informations
52 if (allData.method === 'POST' && allData.data) { 51 if (params.data) {
53 // Encrypt data ? 52 // Encrypt data
54 if (allData.encrypt === true) { 53 if (params.encrypt === true) {
55 peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(allData.data), function (err, encrypted) { 54 peertubeCrypto.encrypt(params.toPod.publicKey, JSON.stringify(params.data), function (err, encrypted) {
56 if (err) return callback(err) 55 if (err) return callback(err)
57 56
58 params.json = { 57 requestParams.json.data = encrypted.data
59 data: encrypted.data, 58 requestParams.json.key = encrypted.key
60 key: encrypted.key
61 }
62 59
63 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) 60 request.post(requestParams, callback)
64 }) 61 })
65 } else { 62 } else {
66 params.json = { data: allData.data } 63 // No encryption
67 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) 64 requestParams.json.data = params.data
65 request.post(requestParams, callback)
68 } 66 }
69 } else { 67 } else {
70 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) 68 // No data
69 request.post(requestParams, callback)
71 } 70 }
72 }, callback) 71 } else {
72 request.get(requestParams, callback)
73 }
73} 74}
74 75
75// --------------------------------------------------------------------------- 76// ---------------------------------------------------------------------------
76 77
77module.exports = requests 78module.exports = requests
78
79// ---------------------------------------------------------------------------
80
81function makeRetryRequest (params, fromUrl, toPod, signature, callbackEach) {
82 // Append the signature
83 if (signature) {
84 params.json.signature = {
85 url: fromUrl,
86 signature: signature
87 }
88 }
89
90 logger.debug('Make retry requests to %s.', toPod.url)
91
92 replay(
93 request.post(params, function (err, response, body) {
94 callbackEach(err, response, body, params.url, toPod)
95 }),
96 {
97 retries: constants.REQUEST_RETRIES,
98 factor: 3,
99 maxTimeout: Infinity,
100 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
101 }
102 ).on('replay', function (replay) {
103 logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.',
104 params.url, replay.error.code, replay.error.message, replay.number, replay.delay)
105 })
106}