about summary refs log tree commit diff
path: root/src/lib/media/manip.ts
diff options
context:
space:
mode:
authorSamuel Newman <mozzius@protonmail.com>2025-05-08 23:50:12 +0300
committerGitHub <noreply@github.com>2025-05-08 23:50:12 +0300
commit207527f1d57b7dbde1201823a37751fb1ee6473e (patch)
treeee86a378a5ce941d1ee3fe20a8abb97e2a153ccd /src/lib/media/manip.ts
parent36697755c72805d141044513d1a08bd45f946a3b (diff)
downloadvoidsky-207527f1d57b7dbde1201823a37751fb1ee6473e.tar.zst
[Android] Save photos to "Bluesky" folder (#8018)
Diffstat (limited to 'src/lib/media/manip.ts')
-rw-r--r--src/lib/media/manip.ts32
1 files changed, 30 insertions, 2 deletions
diff --git a/src/lib/media/manip.ts b/src/lib/media/manip.ts
index 4578406e0..62cbc55ac 100644
--- a/src/lib/media/manip.ts
+++ b/src/lib/media/manip.ts
@@ -126,6 +126,8 @@ export async function shareImageModal({uri}: {uri: string}) {
   safeDeleteAsync(imagePath)
 }
 
+const ALBUM_NAME = 'Bluesky'
+
 export async function saveImageToMediaLibrary({uri}: {uri: string}) {
   // download the file to cache
   // NOTE
@@ -139,8 +141,34 @@ export async function saveImageToMediaLibrary({uri}: {uri: string}) {
   imagePath = normalizePath(await moveToPermanentPath(imagePath, '.png'), true)
 
   // save
-  await MediaLibrary.createAssetAsync(imagePath)
-  safeDeleteAsync(imagePath)
+  try {
+    if (isAndroid) {
+      // android triggers an annoying permission prompt if you try and move an image
+      // between albums. therefore, we need to either create the album with the image
+      // as the starting image, or put it directly into the album
+      const album = await MediaLibrary.getAlbumAsync(ALBUM_NAME)
+      if (album) {
+        // if album exists, put the image straight in there
+        await MediaLibrary.createAssetAsync(imagePath, album)
+      } else {
+        // otherwise, create album with asset (albums must always have at least one asset)
+        await MediaLibrary.createAlbumAsync(
+          ALBUM_NAME,
+          undefined,
+          undefined,
+          imagePath,
+        )
+      }
+    } else {
+      await MediaLibrary.createAssetAsync(imagePath)
+    }
+  } catch (err) {
+    logger.error(err instanceof Error ? err : String(err), {
+      message: 'Failed to save image to media library',
+    })
+  } finally {
+    safeDeleteAsync(imagePath)
+  }
 }
 
 export function getImageDim(path: string): Promise<Dimensions> {