1
0
Fork 0

gipy: one more try for file upload

master
frederic wagner 2022-11-08 18:00:19 +01:00
parent b9581a2fd2
commit ea8690e082
1 changed files with 29 additions and 13 deletions

View File

@ -16,17 +16,29 @@
</script>
<script type="module">
function uploadFile(fileid, contents) {
// TODO: use interface.js-provided stuff?
Puck.write(`\x10(function() {
require("Storage").write("${fileid}",'${contents}');
Bluetooth.print("OK");
})()\n`, ret => {
console.log("uploadFile", ret);
if (ret == "OK")
clean();
});
}
// Get the JS needed to upload a file
function getUploadFileCode(fileName, contents) {
var js = [];
if ("string" != typeof contents)
throw new Error("Expecting a string for contents");
if (fileName.length==0 || fileName.length>28)
throw new Error("Invalid filename length");
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";
console.log("imported wasm");
@ -54,10 +66,14 @@
init().then(() => {
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");
uploadFile(gpc_filename, gpc_string);
uploadFile(gpc_filename, gpc_string, ret => {
console.log("uploadFile", ret);
if (ret == "OK")
clean();
});
});
}
reader.readAsText(this.files[0]);