Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check Scratch.canDownload before downloading files #1798

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading