Skip to content

Commit 9c25e83

Browse files
author
ilya.veklenko
committed
refactor (SDK send function):
-merge to functions (send and sendEncoded) to one with options. -don't set any custom properies to fileReader instance
1 parent 1eddf55 commit 9c25e83

File tree

1 file changed

+64
-87
lines changed

1 file changed

+64
-87
lines changed

libs/backendless.js

Lines changed: 64 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -3371,105 +3371,67 @@
33713371
return builder;
33723372
}
33733373

3374-
function send(e) {
3375-
var xhr = new XMLHttpRequest(),
3376-
boundary = '-backendless-multipart-form-boundary-' + getNow(),
3377-
builder = getBuilder(this.fileName, e.target.result, boundary),
3378-
badResponse = function(xhr) {
3379-
var result = {};
3380-
try {
3381-
result = JSON.parse(xhr.responseText);
3382-
} catch (e) {
3383-
result.message = xhr.responseText;
3384-
}
3385-
result.statusCode = xhr.status;
3386-
return result;
3387-
};
3388-
3389-
xhr.open("POST", this.uploadPath, true);
3390-
xhr.setRequestHeader('content-type', 'multipart/form-data; boundary=' + boundary);
3391-
xhr.setRequestHeader('application-id', Backendless.applicationId);
3392-
xhr.setRequestHeader("secret-key", Backendless.secretKey);
3393-
xhr.setRequestHeader("application-type", "JS");
3394-
3395-
if ((currentUser != null && currentUser["user-token"])) {
3396-
xhr.setRequestHeader("user-token", currentUser["user-token"]);
3397-
} else if (Backendless.LocalCache.exists("user-token")) {
3398-
xhr.setRequestHeader("user-token", Backendless.LocalCache.get("user-token"));
3399-
}
3400-
3401-
if (UIState !== null) {
3402-
xhr.setRequestHeader("uiState", UIState);
3403-
}
3404-
3405-
var asyncHandler = this.asyncHandler;
3374+
function sendData(options) {
3375+
var async = options.async;
3376+
var encoded = options.encoded;
3377+
var boundary = '-backendless-multipart-form-boundary-' + getNow();
3378+
var xhr = new XMLHttpRequest();
34063379

3407-
if (asyncHandler) {
3408-
xhr.onreadystatechange = function() {
3409-
if (xhr.readyState == 4) {
3410-
if (xhr.status >= 200 && xhr.status < 300) {
3411-
asyncHandler.success(JSON.parse(xhr.responseText));
3412-
} else {
3413-
asyncHandler.fault(JSON.parse(xhr.responseText));
3414-
}
3415-
}
3416-
};
3417-
}
3418-
3419-
xhr.sendAsBinary(builder);
3380+
var badResponse = function (xhr) {
3381+
var result = {};
34203382

3421-
if (asyncHandler) {
3422-
return xhr;
3423-
}
3383+
try {
3384+
result = JSON.parse(xhr.responseText);
3385+
} catch (e) {
3386+
result.message = xhr.responseText;
3387+
}
34243388

3425-
if (xhr.status >= 200 && xhr.status < 300) {
3426-
return xhr.responseText ? JSON.parse(xhr.responseText) : true;
3427-
} else {
3428-
throw badResponse(xhr);
3429-
}
3430-
}
3389+
result.statusCode = xhr.status;
34313390

3432-
function sendEncoded(e) {
3433-
var xhr = new XMLHttpRequest(),
3434-
boundary = '-backendless-multipart-form-boundary-' + getNow(),
3435-
badResponse = function(xhr) {
3436-
var result = {};
3437-
try {
3438-
result = JSON.parse(xhr.responseText);
3439-
} catch (e) {
3440-
result.message = xhr.responseText;
3441-
}
3442-
result.statusCode = xhr.status;
3443-
return result;
3444-
};
3391+
return result;
3392+
};
34453393

3446-
var asyncHandler = this.asyncHandler;
3394+
xhr.open(options.method, options.url, !!async);
34473395

3448-
xhr.open("PUT", this.uploadPath, !!asyncHandler);
3449-
xhr.setRequestHeader('Content-Type', 'text/plain');
34503396
xhr.setRequestHeader('application-id', Backendless.applicationId);
34513397
xhr.setRequestHeader("secret-key", Backendless.secretKey);
34523398
xhr.setRequestHeader("application-type", "JS");
34533399

3400+
if (encoded) {
3401+
xhr.setRequestHeader('Content-Type', 'text/plain');
3402+
} else {
3403+
xhr.setRequestHeader('content-type', 'multipart/form-data; boundary=' + boundary);
3404+
}
3405+
34543406
if (UIState !== null) {
34553407
xhr.setRequestHeader("uiState", UIState);
34563408
}
34573409

3458-
if (asyncHandler) {
3410+
var userToken = currentUser && currentUser["user-token"] || Backendless.LocalCache.get("user-token");
3411+
3412+
if (userToken) {
3413+
xhr.setRequestHeader("user-token", userToken);
3414+
}
3415+
3416+
if (async) {
34593417
xhr.onreadystatechange = function() {
34603418
if (xhr.readyState == 4) {
34613419
if (xhr.status >= 200 && xhr.status < 300) {
3462-
asyncHandler.success(JSON.parse(xhr.responseText));
3420+
async.success(JSON.parse(xhr.responseText));
34633421
} else {
3464-
asyncHandler.fault(JSON.parse(xhr.responseText));
3422+
async.fault(JSON.parse(xhr.responseText));
34653423
}
34663424
}
34673425
};
34683426
}
34693427

3470-
xhr.send(e.target.result.split(',')[1]);
3428+
if (encoded) {
3429+
xhr.send(options.data);
3430+
} else {
3431+
xhr.sendAsBinary(getBuilder(options.fileName, options.data, boundary));
3432+
}
34713433

3472-
if (asyncHandler) {
3434+
if (async) {
34733435
return xhr;
34743436
}
34753437

@@ -3571,13 +3533,18 @@
35713533

35723534
try {
35733535
var reader = new FileReader();
3574-
reader.fileName = encodeURIComponent(fileName).replace(/'/g, "%27").replace(/"/g, "%22");
3575-
reader.uploadPath = baseUrl;
3576-
reader.onloadend = sendEncoded;
3577-
3578-
if (async) {
3579-
reader.asyncHandler = async;
3580-
}
3536+
var fileName = encodeURIComponent(fileName).replace(/'/g, "%27").replace(/"/g, "%22");
3537+
reader.fileName = fileName;
3538+
reader.onloadend = function (e) {
3539+
sendData({
3540+
url: baseUrl,
3541+
data: e.target.result.split(',')[1],
3542+
fileName: fileName,
3543+
encoded: true,
3544+
async: async,
3545+
method: 'PUT'
3546+
});
3547+
};
35813548

35823549
reader.onerror = function(evn) {
35833550
async.fault(evn);
@@ -3613,14 +3580,24 @@
36133580
for (var i = 0, len = files.length; i < len; i++) {
36143581
try {
36153582
var reader = new FileReader();
3616-
reader.fileName = encodeURIComponent(files[i].name).replace(/'/g, "%27").replace(/"/g, "%22");
3617-
reader.uploadPath = baseUrl + reader.fileName + overwriting;
3618-
reader.onloadend = send;
3619-
reader.asyncHandler = async;
3583+
var fileName = encodeURIComponent(files[i].name).replace(/'/g, "%27").replace(/"/g, "%22");
3584+
var url = baseUrl + fileName + overwriting;
3585+
3586+
reader.fileName = fileName;
3587+
reader.onloadend = function (e) {
3588+
sendData({
3589+
url: url,
3590+
data: e.target.result,
3591+
fileName: fileName,
3592+
async: async,
3593+
method: 'POST'
3594+
});
3595+
};
3596+
36203597
reader.onerror = function(evn) {
36213598
async.fault(evn);
36223599
};
3623-
reader.readAsArrayBuffer(files[i]);
3600+
reader.readAsBinaryString(files[i]);
36243601

36253602
} catch (err) {
36263603
filesError++;

0 commit comments

Comments
 (0)