Ensure we run transfers one after the other rather that potentially having them overlap if the user clicks around https://github.com/espruino/EspruinoAppLoaderCore/issues/67

master
Gordon Williams 2025-06-06 15:13:59 +01:00
parent 9d84683505
commit 700a9f9f4a
3 changed files with 68 additions and 63 deletions

116
backup.js
View File

@ -7,7 +7,7 @@ function bangleDownload() {
Progress.show({title:"Scanning...",sticky:true});
var normalFiles, storageFiles;
console.log("Listing normal files...");
Comms.reset()
return Comms.reset()
.then(() => Comms.showMessage("Backing up..."))
.then(() => Comms.listFiles({sf:false}))
.then(f => {
@ -66,70 +66,74 @@ function bangleDownload() {
}
function bangleUpload() {
Espruino.Core.Utils.fileOpenDialog({
id:"backup",
type:"arraybuffer",
mimeType:".zip,application/zip"}, function(data) {
if (data===undefined) return;
var promise = Promise.resolve();
var zip = new JSZip();
var cmds = "";
zip.loadAsync(data).then(function(zip) {
return showPrompt("Restore from ZIP","Are you sure? This will overwrite existing apps");
}).then(()=>{
Progress.show({title:`Reading ZIP`});
zip.forEach(function (path, file){
console.log("path");
promise = promise
.then(() => file.async("string"))
.then(data => {
console.log("decoded", path);
if (data.length==0) { // https://github.com/espruino/BangleApps/issues/1593
console.log("Can't restore files of length 0, ignoring "+path);
} else if (path.startsWith(BACKUP_STORAGEFILE_DIR)) {
path = path.substr(BACKUP_STORAGEFILE_DIR.length+1);
cmds += AppInfo.getStorageFileUploadCommands(path, data)+"\n";
} else if (!path.includes("/")) {
cmds += AppInfo.getFileUploadCommands(path, data)+"\n";
} else console.log("Ignoring "+path);
return new Promise(resolve =>
Espruino.Core.Utils.fileOpenDialog({
id:"backup",
type:"arraybuffer",
mimeType:".zip,application/zip"}, function(data) {
if (data===undefined) return;
var promise = Promise.resolve();
var zip = new JSZip();
var cmds = "";
zip.loadAsync(data).then(function(zip) {
return showPrompt("Restore from ZIP","Are you sure? This will overwrite existing apps");
}).then(()=>{
Progress.show({title:`Reading ZIP`});
zip.forEach(function (path, file){
console.log("path");
promise = promise
.then(() => file.async("string"))
.then(data => {
console.log("decoded", path);
if (data.length==0) { // https://github.com/espruino/BangleApps/issues/1593
console.log("Can't restore files of length 0, ignoring "+path);
} else if (path.startsWith(BACKUP_STORAGEFILE_DIR)) {
path = path.substr(BACKUP_STORAGEFILE_DIR.length+1);
cmds += AppInfo.getStorageFileUploadCommands(path, data)+"\n";
} else if (!path.includes("/")) {
cmds += AppInfo.getFileUploadCommands(path, data)+"\n";
} else console.log("Ignoring "+path);
});
});
return promise;
})
.then(()=>new Promise(resolve => {
showPrompt("Erase Storage","Erase Storage? If restoring a complete backup you should erase storage, but in some cases you may want to upload files from a ZIP while keeping your Bangle's existing data.").then(()=>resolve(true), ()=>resolve(false));
}))
.then(eraseStorage => {
if (eraseStorage) {
Progress.hide({sticky:true});
Progress.show({title:`Erasing...`});
return Comms.removeAllApps();
}})
.then(() => {
Progress.hide({sticky:true});
Progress.show({title:`Restoring...`, sticky:true});
return Comms.showMessage(`Restoring...`); })
.then(() => Comms.write("\x10"+Comms.getProgressCmd()+"\n"))
.then(() => Comms.uploadCommandList(cmds, 0, cmds.length))
.then(() => getInstalledApps(true))
.then(() => Comms.showMessage(Const.MESSAGE_RELOAD))
.then(() => {
Progress.hide({sticky:true});
showToast('Restore complete!', 'success');
resolve();
})
.catch(err => {
Progress.hide({sticky:true});
showToast('Restore failed, ' + err, 'error');
resolve();
});
return promise;
})
.then(()=>new Promise(resolve => {
showPrompt("Erase Storage","Erase Storage? If restoring a complete backup you should erase storage, but in some cases you may want to upload files from a ZIP while keeping your Bangle's existing data.").then(()=>resolve(true), ()=>resolve(false));
}))
.then(eraseStorage => {
if (eraseStorage) {
Progress.hide({sticky:true});
Progress.show({title:`Erasing...`});
return Comms.removeAllApps();
}})
.then(() => {
Progress.hide({sticky:true});
Progress.show({title:`Restoring...`, sticky:true});
return Comms.showMessage(`Restoring...`); })
.then(() => Comms.write("\x10"+Comms.getProgressCmd()+"\n"))
.then(() => Comms.uploadCommandList(cmds, 0, cmds.length))
.then(() => getInstalledApps(true))
.then(() => Comms.showMessage(Const.MESSAGE_RELOAD))
.then(() => {
Progress.hide({sticky:true});
showToast('Restore complete!', 'success');
})
.catch(err => {
Progress.hide({sticky:true});
showToast('Restore failed, ' + err, 'error');
});
return promise;
});
}
}
window.addEventListener('load', (event) => {
document.getElementById("downloadallapps").addEventListener("click",event=>{
bangleDownload();
startOperation({name:"Backup Apps"}, () => bangleDownload);
});
document.getElementById("uploadallapps").addEventListener("click",event=>{
bangleUpload();
startOperation({name:"Restore Apps"}, () => bangleUpload);
});
});

2
core

@ -1 +1 @@
Subproject commit ef99424a9fbd01be504841a6c759ba4292a542f7
Subproject commit 7e7475ba3ab253099481a81e487aaacb9384f974

View File

@ -212,7 +212,7 @@ window.addEventListener('load', (event) => {
el = document.getElementById("reinstallall");
if (el) el.addEventListener("click",event=>{
var promise = showPrompt("Reinstall","Really re-install all apps?").then(() => {
Comms.reset().then(_ =>
startOperation({name:"Reinstall All Apps"}, () => Comms.reset().then(_ =>
getInstalledApps()
).then(installedapps => {
console.log(installedapps);
@ -232,14 +232,14 @@ window.addEventListener('load', (event) => {
).catch(err=>{
Progress.hide({sticky:true});
showToast("App re-install failed, "+err,"error");
});
}));
});
});
// Button to install all default apps in one go
el = document.getElementById("installdefault");
if (el) el.addEventListener("click", event=>{
getInstalledApps().then(() => {
startOperation({name:"Install Default Apps"}, () => getInstalledApps().then(() => {
if (device.id == "BANGLEJS")
return httpGet("defaultapps_banglejs1.json");
if (device.id == "BANGLEJS2")
@ -250,15 +250,16 @@ window.addEventListener('load', (event) => {
}).catch(err=>{
Progress.hide({sticky:true});
showToast("App Install failed, "+err,"error");
});
}));
});
// Button to reset the Bangle's settings
el = document.getElementById("defaultbanglesettings");
if (el) el.addEventListener("click", event=>{
showPrompt("Reset Settings","Really reset Bangle.js settings?").then(() => {
Comms.write("\x10require('Storage').erase('setting.json');load()\n");
showToast("Settings reset!", "success");
startOperation({name:"Reset Settings"}, () =>
Comms.write("\x10require('Storage').erase('setting.json');load()\n").then(() =>
showToast("Settings reset!", "success")));
}, function() { /* cancelled */ });
});