Skip to content

Commit

Permalink
Check Scratch.canDownload before downloading files (#1798)
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin authored Dec 28, 2024
1 parent dcdef7d commit a8764bf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 44 deletions.
16 changes: 8 additions & 8 deletions extensions/-SIPC-/recording.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
67 changes: 33 additions & 34 deletions extensions/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>}
*/
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);
};

Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a8764bf

Please sign in to comment.