Created
December 20, 2023 23:15
-
-
Save ankineri/37706968a097c0b24d6590b1fe4bdcf8 to your computer and use it in GitHub Desktop.
Delete all files from a Prusa sl1s 3d printer - run in console of the printer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function invoke_xhr(method, url) { | |
return new Promise(function (resolve, reject) { | |
let xhr = new XMLHttpRequest(); | |
xhr.open(method, url); | |
xhr.onload = function () { | |
if (this.status >= 200 && this.status < 300) { | |
resolve(xhr.response); | |
} else { | |
reject({ | |
status: this.status, | |
statusText: xhr.statusText | |
}); | |
} | |
}; | |
xhr.onerror = function () { | |
reject({ | |
status: this.status, | |
statusText: xhr.statusText | |
}); | |
}; | |
xhr.send(); | |
}); | |
} | |
async function getFiles() { | |
const data = JSON.parse(await invoke_xhr("GET", "/api/files?recursive=true")).files; | |
console.error("Found " + data.length + " files. Use await deleteFiles(allFiles) to delete them. This will delete all files, use with caution!\nFor recursive deletion, use await deleteFiles(allFiles, true)."); | |
return data; | |
} | |
async function deleteFiles(files, recursive = false) { | |
console.log("Deleting " + files.length + " files. You might see some errors in console, but that's fine."); | |
for (const file of files) { | |
if (file.type === "folder") { | |
if (recursive) { | |
deleteFiles(file.children, true); | |
} else { | |
console.log("Skipping folder " + file.path); | |
} | |
continue; | |
} | |
console.log("Deleting " + file.path); | |
await invoke_xhr("DELETE", file.refs.resource); | |
} | |
console.log("All done!"); | |
} | |
allFiles = await getFiles(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment