Skip to content

Commit 3e2b79b

Browse files
committed
Added ability to upload to fs
1 parent 8f995f6 commit 3e2b79b

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

core/utils.js

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@
435435
undefined, function() {
436436
allDataSent = true;
437437
// now it's sent, wait for data
438-
var minTimeout = 2; // seconds - how long we wait if we're not getting data
438+
var minTimeout = options.minTimeout || 2; // seconds - how long we wait if we're not getting data
439439
var pollInterval = 200; // milliseconds
440440
var timeoutSeconds = 0;
441441
if (timeout != "cancelled") {
@@ -471,36 +471,53 @@
471471
* Download a file - storageFile or normal file
472472
* @param {string} fileName Path to file to download
473473
* @param {(content?: string) => void} callback Call back with contents of file, or undefined if no content
474+
* @param {Object} options {fs:true} to download from SD card rather than Storage
474475
*/
475-
function downloadFile(fileName, callback) {
476-
var options = {exprPrintsResult:true, maxTimeout:600}; // ten minute timeout
477-
executeExpression(`(function(filename) {
476+
function downloadFile(fileName, callback, options) {
477+
options = options||{};
478+
let exOptions = {exprPrintsResult:true, maxTimeout:600}; // ten minute timeout
479+
let cmd = options.fs ? /*SD card*/`(function(filename) {
480+
var f = E.openFile(filename,"r"), d = f.read(${CHUNKSIZE});
481+
while (d!==undefined) {console.log(btoa(d));d=f.read(${CHUNKSIZE});}
482+
})(${JSON.stringify(fileName)});`: /*Storage*/`(function(filename) {
478483
var s = require("Storage").read(filename);
479484
if(s){ for (var i=0;i<s.length;i+=${CHUNKSIZE}) console.log(btoa(s.substr(i,${CHUNKSIZE}))); } else {
480485
var f=require("Storage").open(filename,"r");var d=f.read(${CHUNKSIZE});
481486
while (d!==undefined) {console.log(btoa(d));d=f.read(${CHUNKSIZE});}
482-
}})(${JSON.stringify(fileName)});`, function(contents) {
487+
}})(${JSON.stringify(fileName)});`
488+
executeExpression(cmd, function(contents) {
483489
if (contents===undefined) callback();
484490
else callback(atob(contents));
485-
}, options);
491+
}, exOptions);
486492
}
487493

488494
/**
489495
* Get the JS needed to upload a file
490496
* @param {string} fileName Path to file to upload
491497
* @param {string} contents Contents of the file being uploaded
498+
* @param {Object} options {fs:true} to download from SD card rather than Storage
492499
* @returns {string} JS code needed to upload file
493500
*/
494-
function getUploadFileCode(fileName, contents) {
501+
function getUploadFileCode(fileName, contents, options) {
502+
options = options||{};
495503
var js = [];
496504
if ("string" != typeof contents)
497505
throw new Error("Expecting a string for contents");
498506
if (fileName.length==0 || fileName.length>28)
499507
throw new Error("Invalid filename length");
500508
var fn = JSON.stringify(fileName);
501-
for (var i=0;i<contents.length;i+=CHUNKSIZE) {
502-
var part = contents.substr(i,CHUNKSIZE);
503-
js.push(`require("Storage").write(${fn},atob(${JSON.stringify(btoa(part))}),${i}${(i==0)?","+contents.length:""})`);
509+
if (options.fs) { // upload to fs
510+
js.push(`var _ul = E.openFile(${fn},"w")`);
511+
for (var i=0;i<contents.length;i+=CHUNKSIZE) {
512+
var part = contents.substr(i,CHUNKSIZE);
513+
js.push(`_ul.write(atob(${JSON.stringify(btoa(part))}))`);
514+
}
515+
js.push(`_ul.close();delete _ul;`);
516+
} else { // upload to Storage
517+
for (var i=0;i<contents.length;i+=CHUNKSIZE) {
518+
var part = contents.substr(i,CHUNKSIZE);
519+
js.push(`require("Storage").write(${fn},atob(${JSON.stringify(btoa(part))}),${i}${(i==0)?","+contents.length:""})`);
520+
}
504521
}
505522
return js.join("\n");
506523
}
@@ -509,9 +526,10 @@ while (d!==undefined) {console.log(btoa(d));d=f.read(${CHUNKSIZE});}
509526
* @param {string} fileName Path to file to upload
510527
* @param {string} contents Contents of the file being uploaded
511528
* @param {(result: string) => void} callback
529+
* @param {Object} options {fs:true} to download from SD card rather than Storage
512530
*/
513-
function uploadFile(fileName, contents, callback) {
514-
var js = getUploadFileCode(fileName, contents).replace(/\n/g,"\n\x10");
531+
function uploadFile(fileName, contents, callback, options) {
532+
var js = getUploadFileCode(fileName, contents, options).replace(/\n/g,"\n\x10");
515533
// executeStatement prepends other code onto the command, so don't add `\x10` at the start of line as then it just ends up in the middle of what's sent
516534
Espruino.Core.Utils.executeStatement(js, callback);
517535
}
@@ -1168,11 +1186,11 @@ while (d!==undefined) {console.log(btoa(d));d=f.read(${CHUNKSIZE});}
11681186
getLexer : getLexer,
11691187
countBrackets : countBrackets,
11701188
getEspruinoPrompt : getEspruinoPrompt,
1171-
executeExpression : function(expr,callback) { executeExpression(expr,callback,{exprPrintsResult:false}); },
1172-
executeStatement : function(statement,callback) { executeExpression(statement,callback,{exprPrintsResult:true}); },
1173-
downloadFile : downloadFile, // (fileName, callback)
1174-
getUploadFileCode : getUploadFileCode, //(fileName, contents);
1175-
uploadFile : uploadFile, // (fileName, contents, callback)
1189+
executeExpression : function(expr,callback,options) { executeExpression(expr, callback, Object.assign({exprPrintsResult:false},options)); },
1190+
executeStatement : function(statement,callback,options) { executeExpression(statement, callback, Object.assign({exprPrintsResult:true},options)); },
1191+
downloadFile : downloadFile, // (fileName, callback, options)
1192+
getUploadFileCode : getUploadFileCode, //(fileName, contents, options);
1193+
uploadFile : uploadFile, // (fileName, contents, callback, options)
11761194
versionToFloat : versionToFloat,
11771195
getURL : getURL,
11781196
getBinaryURL : getBinaryURL,

0 commit comments

Comments
 (0)