From 0c0fc9ac8f223c496d50855c04c001550173bee5 Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Thu, 28 May 2020 14:33:37 +0100 Subject: [PATCH] Improve upload of binary files --- CHANGELOG.md | 1 + js/appinfo.js | 9 +++++++-- js/utils.js | 15 +++++++++++++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5cd6aef5..29b6aa6e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/js/appinfo.js b/js/appinfo.js index 54f1458db..b0b4e05ca 100644 --- a/js/appinfo.js +++ b/js/appinfo.js @@ -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;i127) 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); diff --git a/js/utils.js b/js/utils.js index 859c5b10d..69dcda93b 100644 --- a/js/utils.js +++ b/js/utils.js @@ -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 reject()); oReq.addEventListener("abort", () => reject()); @@ -41,6 +51,7 @@ function httpGet(url) { oReq.onerror = function () { reject("HTTP Request failed"); }; + oReq.responseType = 'blob'; oReq.send(); }); }