1
0
Fork 0

Improve upload of binary files

master
Gordon Williams 2020-05-28 14:33:37 +01:00
parent 572a468cab
commit 0c0fc9ac8f
3 changed files with 21 additions and 4 deletions

View File

@ -18,3 +18,4 @@ Changed for individual apps are listed in `apps/appname/ChangeLog`
* Adding '#search' after the URL (when not the name of a 'filter' chip) will set up search for that term
* If `bin/pre-publish.sh` has been run and recent.csv created, add 'Sort By' chip
* New 'espruinotools' which fixes pretokenise issue when ID follows ID (fix #416)
* Improve upload of binary files

View File

@ -7,9 +7,14 @@ if (typeof btoa==="undefined") {
// Converts a string into most efficient way to send to Espruino (either json, base64, or compressed base64)
function toJS(txt) {
let isBinary = false;
for (let i=0;i<txt.length;i++) {
let ch = txt.charCodeAt(i);
if (ch==0 || ch>127) isBinary=true;
}
let json = JSON.stringify(txt);
let b64 = "atob("+JSON.stringify(btoa(json))+")";
let js = b64.length < json.length ? b64 : json;
let b64 = "atob("+JSON.stringify(btoa(txt))+")";
let js = (isBinary || (b64.length < json.length)) ? b64 : json;
if (typeof heatshrink !== "undefined") {
let ua = new Uint8Array(txt.length);

View File

@ -32,8 +32,18 @@ function httpGet(url) {
return new Promise((resolve,reject) => {
let oReq = new XMLHttpRequest();
oReq.addEventListener("load", () => {
if (oReq.status==200) resolve(oReq.responseText)
else reject(oReq.status+" - "+oReq.statusText);
// ensure we actually load the data as a raw 8 bit string (not utf-8/etc)
if (oReq.status==200) {
let a = new FileReader();
a.onloadend = function() {
let bytes = new Uint8Array(a.result);
let str = "";
for (let i=0;i<bytes.length;i++)
str += String.fromCharCode(bytes[i]);
resolve(str)
};
a.readAsArrayBuffer(oReq.response);
} else reject(oReq.status+" - "+oReq.statusText);
});
oReq.addEventListener("error", () => reject());
oReq.addEventListener("abort", () => reject());
@ -41,6 +51,7 @@ function httpGet(url) {
oReq.onerror = function () {
reject("HTTP Request failed");
};
oReq.responseType = 'blob';
oReq.send();
});
}