mirror of https://github.com/espruino/BangleApps
gipy: one more try for file upload
parent
b9581a2fd2
commit
ea8690e082
|
@ -16,17 +16,29 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="module">
|
<script type="module">
|
||||||
function uploadFile(fileid, contents) {
|
|
||||||
// TODO: use interface.js-provided stuff?
|
// Get the JS needed to upload a file
|
||||||
Puck.write(`\x10(function() {
|
function getUploadFileCode(fileName, contents) {
|
||||||
require("Storage").write("${fileid}",'${contents}');
|
var js = [];
|
||||||
Bluetooth.print("OK");
|
if ("string" != typeof contents)
|
||||||
})()\n`, ret => {
|
throw new Error("Expecting a string for contents");
|
||||||
console.log("uploadFile", ret);
|
if (fileName.length==0 || fileName.length>28)
|
||||||
if (ret == "OK")
|
throw new Error("Invalid filename length");
|
||||||
clean();
|
var fn = JSON.stringify(fileName);
|
||||||
});
|
for (var i=0;i<contents.length;i+=CHUNKSIZE) {
|
||||||
|
var part = contents.substr(i,CHUNKSIZE);
|
||||||
|
js.push(`require("Storage").write(${fn},atob(${JSON.stringify(btoa(part))}),${i}${(i==0)?","+contents.length:""})`);
|
||||||
}
|
}
|
||||||
|
return js.join("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Upload a file
|
||||||
|
function uploadFile(fileName, contents, callback) {
|
||||||
|
let js = "\x10"+getUploadFileCode(fileName, contents).replace(/\n/g,"\n\x10");
|
||||||
|
let fun = "\x10(function() {" + js + ";\n\x10Bluetooth.print(\"OK\");})()\n";
|
||||||
|
Puck.write(fun, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
import init, { convert_gpx_strings } from "./pkg/gpconv.js";
|
import init, { convert_gpx_strings } from "./pkg/gpconv.js";
|
||||||
console.log("imported wasm");
|
console.log("imported wasm");
|
||||||
|
@ -54,10 +66,14 @@
|
||||||
init().then(() => {
|
init().then(() => {
|
||||||
|
|
||||||
let gpc_file = convert_gpx_strings(reader.result);
|
let gpc_file = convert_gpx_strings(reader.result);
|
||||||
let gpc_string = String.fromCharCode.apply(String, gpc_file).replace(/'/g, "\\'");
|
let gpc_string = String.fromCharCode.apply(String, gpc_file));
|
||||||
|
|
||||||
console.log("uploading");
|
console.log("uploading");
|
||||||
uploadFile(gpc_filename, gpc_string);
|
uploadFile(gpc_filename, gpc_string, ret => {
|
||||||
|
console.log("uploadFile", ret);
|
||||||
|
if (ret == "OK")
|
||||||
|
clean();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
reader.readAsText(this.files[0]);
|
reader.readAsText(this.files[0]);
|
||||||
|
|
Loading…
Reference in New Issue