From a8764bfad7571fffe9b9d92573dd1333d1904af9 Mon Sep 17 00:00:00 2001 From: Thomas Weber Date: Sat, 28 Dec 2024 00:44:41 -0800 Subject: [PATCH] Check Scratch.canDownload before downloading files (#1798) https://github.com/TurboWarp/scratch-gui/issues/969 --- extensions/-SIPC-/recording.js | 16 ++++---- extensions/files.js | 67 +++++++++++++++++----------------- package-lock.json | 3 +- 3 files changed, 42 insertions(+), 44 deletions(-) diff --git a/extensions/-SIPC-/recording.js b/extensions/-SIPC-/recording.js index 82d2bebb55..c42863f4a2 100644 --- a/extensions/-SIPC-/recording.js +++ b/extensions/-SIPC-/recording.js @@ -96,17 +96,17 @@ return; } console.log("Stop recording"); - mediaRecorder.addEventListener("stop", function () { + mediaRecorder.addEventListener("stop", async function () { const blob = new Blob(recordedChunks, { type: "audio/wav" }); + recordedChunks = []; + const url = URL.createObjectURL(blob); - const downloadLink = document.createElement("a"); - downloadLink.href = url; - downloadLink.download = name; - document.body.appendChild(downloadLink); - downloadLink.click(); - document.body.removeChild(downloadLink); + try { + await Scratch.download(url, name); + } catch (e) { + console.error(e); + } URL.revokeObjectURL(url); - recordedChunks = []; }); mediaRecorder.stop(); mediaRecorder = null; diff --git a/extensions/files.js b/extensions/files.js index af703b8028..f21616f395 100644 --- a/extensions/files.js +++ b/extensions/files.js @@ -215,26 +215,18 @@ } }); - /** - * @param {string} url a data:, blob:, or same-origin URL - * @param {string} file - */ - const downloadURL = (url, file) => { - const link = document.createElement("a"); - link.href = url; - link.download = file; - document.body.appendChild(link); - link.click(); - link.remove(); - }; - /** * @param {Blob} blob Data to download * @param {string} file Name of the file + * @returns {Promise} */ - const downloadBlob = (blob, file) => { + const downloadBlob = async (blob, file) => { const url = URL.createObjectURL(blob); - downloadURL(url, file); + try { + await Scratch.download(url, file); + } catch (e) { + console.error(e); + } URL.revokeObjectURL(url); }; @@ -255,17 +247,16 @@ * @param {string} url * @param {string} file */ - const downloadUntrustedURL = (url, file) => { - // Don't want to return a Promise here when not actually needed + const downloadUntrustedURL = async (url, file) => { if (isDataURL(url)) { - downloadURL(url, file); - } else { - return Scratch.fetch(url) - .then((res) => res.blob()) - .then((blob) => { - downloadBlob(blob, file); - }); + // TODO: Scratch.fetch's better handling of data: means this is probably not needed anymore + // and it the blob: probably works better with big files + return Scratch.download(url, file); } + + const res = await Scratch.fetch(url); + const blob = await res.blob(); + await downloadBlob(blob, file); }; class Files { @@ -424,18 +415,26 @@ return showFilePrompt(args.extension, args.as); } - download(args) { - downloadBlob( - new Blob([Scratch.Cast.toString(args.text)]), - Scratch.Cast.toString(args.file) - ); + async download(args) { + try { + await downloadBlob( + new Blob([Scratch.Cast.toString(args.text)]), + Scratch.Cast.toString(args.file) + ); + } catch (e) { + console.error(e); + } } - downloadURL(args) { - return downloadUntrustedURL( - Scratch.Cast.toString(args.url), - Scratch.Cast.toString(args.file) - ); + async downloadURL(args) { + try { + await downloadUntrustedURL( + Scratch.Cast.toString(args.url), + Scratch.Cast.toString(args.file) + ); + } catch (e) { + console.error(e); + } } setOpenMode(args) { diff --git a/package-lock.json b/package-lock.json index 83846488e3..1a25d60101 100644 --- a/package-lock.json +++ b/package-lock.json @@ -238,8 +238,7 @@ }, "node_modules/@turbowarp/types": { "version": "0.0.12", - "resolved": "git+ssh://git@github.com/TurboWarp/types-tw.git#1f858811919809efa4dbd789d27f8e513a32ee75", - "integrity": "sha512-OsBAn//X6giPsnBQ2ycA8ZRlMBXhnrWo89/qe5gWEaT0oHSqEN+leGNQJylR28UdbNddSTTjkcnAZ0cptHwKnA==", + "resolved": "git+ssh://git@github.com/TurboWarp/types-tw.git#1dbfe25bcb4ef14012f3b4b6b480005ea494f5d9", "license": "Apache-2.0" }, "node_modules/@types/estree": {