about summary refs log tree commit diff
path: root/src/lib/media/manip.web.ts
diff options
context:
space:
mode:
authorMatthieu Sieben <matthieusieben@users.noreply.github.com>2024-05-12 23:18:42 +0200
committerGitHub <noreply@github.com>2024-05-12 14:18:42 -0700
commit00a57df5b16bc946c50079914962cc2819011e80 (patch)
tree4040fad00e74757d846bc503147b9e601e443c84 /src/lib/media/manip.web.ts
parent4458b031732149d6f9c107582b9e4ec343385518 (diff)
downloadvoidsky-00a57df5b16bc946c50079914962cc2819011e80.tar.zst
✅ Fix "Download CAR file" on mobile (#3816)
* download CAR file using AtpAgent instead of building URL

* add loader icon on download car button

* actually save to disk on android

* style nits

* bottom margin nit

* localize toast

* remove fallback so back button works correctly

* keep throwing an error if mime type isn't used

* be more explicit with toasts

* send errors to sentry when encountered

---------

Co-authored-by: Hailey <me@haileyok.com>
Diffstat (limited to 'src/lib/media/manip.web.ts')
-rw-r--r--src/lib/media/manip.web.ts25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/lib/media/manip.web.ts b/src/lib/media/manip.web.ts
index 522aa2e51..25315ebbd 100644
--- a/src/lib/media/manip.web.ts
+++ b/src/lib/media/manip.web.ts
@@ -1,6 +1,7 @@
-import {Dimensions} from './types'
 import {Image as RNImage} from 'react-native-image-crop-picker'
-import {getDataUriSize, blobToDataUri} from './util'
+
+import {Dimensions} from './types'
+import {blobToDataUri, getDataUriSize} from './util'
 
 export async function compressIfNeeded(
   img: RNImage,
@@ -138,3 +139,23 @@ function createResizedImage(
     img.src = dataUri
   })
 }
+
+export async function saveBytesToDisk(
+  filename: string,
+  bytes: Uint8Array,
+  type: string,
+) {
+  const blob = new Blob([bytes], {type})
+  const url = URL.createObjectURL(blob)
+  await downloadUrl(url, filename)
+  // Firefox requires a small delay
+  setTimeout(() => URL.revokeObjectURL(url), 100)
+  return true
+}
+
+async function downloadUrl(href: string, filename: string) {
+  const a = document.createElement('a')
+  a.href = href
+  a.download = filename
+  a.click()
+}