aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/lib/ts/download.ts
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/lib/ts/download.ts')
-rw-r--r--app/src/lib/ts/download.ts33
1 files changed, 33 insertions, 0 deletions
diff --git a/app/src/lib/ts/download.ts b/app/src/lib/ts/download.ts
new file mode 100644
index 0000000..a6167d7
--- /dev/null
+++ b/app/src/lib/ts/download.ts
@@ -0,0 +1,33 @@
+// Yoinked from https://web.dev/patterns/files/save-a-file/ for the polyfill
+
+export default async function saveFile(blob: ArrayBuffer, suggestedName: string | undefined) {
+ const supportsFSAccess = 'showSaveFilePicker' in window &&
+ (() => {
+ try {
+ return window.self === window.top;
+ } catch {
+ return false;
+ }
+ })();
+
+ if (supportsFSAccess) {
+ try {
+ const handle = await window.showSaveFilePicker({
+ suggestedName: suggestedName,
+ })
+ const writable = await handle.createWritable();
+ await writable.write(blob);
+ await writable.close();
+ } catch (err: unknown) {
+ // TypeScript
+ if ((err as Error)?.name !== 'AbortError') {
+ console.error(err);
+ }
+ }
+ } else {
+ const anchor = document.createElement('a');
+ anchor.download = suggestedName ?? 'truncated';
+ anchor.href = URL.createObjectURL(new Blob([blob]));
+ anchor.click();
+ }
+} \ No newline at end of file