Skip to content

Commit 912154e

Browse files
authored
Merge pull request #4362 from Countly/SER-760
[SER-760] Data migration request issue
2 parents 2fc56b3 + a95df0c commit 912154e

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

api/utils/countly-request/index.js

+43-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
const got = require('got');
6+
const FormData = require('form-data');
67

78
var initParams = function(uri, options, callback) {
89

@@ -141,20 +142,52 @@ module.exports = function(uri, options, callback) {
141142

142143
};
143144

145+
/**
146+
* Uploads a file to the server
147+
* @param {string} url - url to upload file to
148+
* @param {object} fileData - file data object
149+
* @param {string} fileData.fileField - name of the field to upload file as
150+
* @param {string} fileData.fileStream - file stream to upload
151+
* @param {function} callback - callback function
152+
*/
153+
async function uploadFormFile(url, fileData, callback) {
154+
const { fileField, fileStream } = fileData;
155+
156+
const form = new FormData();
157+
form.append(fileField, fileStream);
158+
159+
try {
160+
const response = await got.post(url, {
161+
body: form
162+
});
163+
callback(null, response.body);
164+
}
165+
catch (error) {
166+
callback(error);
167+
}
168+
}
169+
144170
// Add a post method to the request object
145171
module.exports.post = function(uri, options, callback) {
146172
var params = initParams(uri, options, callback);
147173
if (params.options && (params.options.url || params.options.uri)) {
148-
// Make the request using got
149-
got.post(params.options)
150-
.then(response => {
151-
// Call the callback with the response data
152-
params.callback(null, response, response.body);
153-
})
154-
.catch(error => {
155-
// Call the callback with the error
156-
params.callback(error);
157-
});
174+
if (params.options.form) {
175+
// If options include a form, use uploadFormFile
176+
const { url, form } = params.options;
177+
uploadFormFile(url || params.options.uri, form, params.callback);
178+
}
179+
else {
180+
// Make the request using got
181+
got.post(params.options)
182+
.then(response => {
183+
// Call the callback with the response data
184+
params.callback(null, response, response.body);
185+
})
186+
.catch(error => {
187+
// Call the callback with the error
188+
params.callback(error);
189+
});
190+
}
158191
}
159192
else {
160193
// Make the request using got

plugins/data_migration/api/data_migration_helper.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1115,9 +1115,15 @@ module.exports = function(my_db) {
11151115
}
11161116

11171117
update_progress(my_exportid, "sending", "progress", 0, "", true);
1118-
var r = request.post({url: res.server_address + '/i/datamigration/import?exportid=' + my_exportid + '&auth_token=' + res.server_token}, requestCallback);
1119-
var form = r.form();
1120-
form.append("import_file", fs.createReadStream(dir));
1118+
const fileData = {
1119+
fileField: 'import_file',
1120+
fileStream: fs.createReadStream(dir)
1121+
};
1122+
1123+
request.post({
1124+
url: res.server_address + '/i/datamigration/import?exportid=' + my_exportid + '&auth_token=' + res.server_token,
1125+
form: fileData
1126+
}, requestCallback);
11211127
}
11221128
}
11231129
});

0 commit comments

Comments
 (0)