about summary refs log tree commit diff
path: root/src/lib/media/manip.ts
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2023-06-30 11:34:04 -0500
committerGitHub <noreply@github.com>2023-06-30 11:34:04 -0500
commitc72e24f841b5771d74e82114e40ed81f15ec97a7 (patch)
tree9cde253f90c3a22bce0d27fd8991b32213526436 /src/lib/media/manip.ts
parent5fcca171291bbbfc63a0f9eaa153efd5d046c33e (diff)
downloadvoidsky-c72e24f841b5771d74e82114e40ed81f15ec97a7.tar.zst
[APP-716] Add 'save image' button to the lightbox (#926)
* Add 'save image' button to the lightbox

* Fix types

* Fix types
Diffstat (limited to 'src/lib/media/manip.ts')
-rw-r--r--src/lib/media/manip.ts31
1 files changed, 30 insertions, 1 deletions
diff --git a/src/lib/media/manip.ts b/src/lib/media/manip.ts
index c35953703..2f6c25e83 100644
--- a/src/lib/media/manip.ts
+++ b/src/lib/media/manip.ts
@@ -5,6 +5,7 @@ import {Image} from 'react-native-image-crop-picker'
 import * as RNFS from 'react-native-fs'
 import uuid from 'react-native-uuid'
 import * as Sharing from 'expo-sharing'
+import * as MediaLibrary from 'expo-media-library'
 import {Dimensions} from './types'
 import {isAndroid, isIOS} from 'platform/detection'
 
@@ -75,7 +76,7 @@ export async function downloadAndResize(opts: DownloadAndResizeOpts) {
   }
 }
 
-export async function saveImageModal({uri}: {uri: string}) {
+export async function shareImageModal({uri}: {uri: string}) {
   if (!(await Sharing.isAvailableAsync())) {
     // TODO might need to give an error to the user in this case -prf
     return
@@ -107,6 +108,34 @@ export async function saveImageModal({uri}: {uri: string}) {
   RNFS.unlink(imagePath)
 }
 
+export async function saveImageToAlbum({
+  uri,
+  album,
+}: {
+  uri: string
+  album: string
+}) {
+  // download the file to cache
+  // NOTE
+  // assuming PNG
+  // we're currently relying on the fact our CDN only serves pngs
+  // -prf
+  const downloadResponse = await RNFetchBlob.config({
+    fileCache: true,
+  }).fetch('GET', uri)
+  let imagePath = downloadResponse.path()
+  imagePath = normalizePath(await moveToPermanentPath(imagePath, '.png'), true)
+
+  // save to the album (creating as needed)
+  const assetRef = await MediaLibrary.createAssetAsync(imagePath)
+  const albumRef = await MediaLibrary.getAlbumAsync(album)
+  if (albumRef) {
+    await MediaLibrary.addAssetsToAlbumAsync(assetRef, albumRef)
+  } else {
+    await MediaLibrary.createAlbumAsync(album, assetRef)
+  }
+}
+
 export function getImageDim(path: string): Promise<Dimensions> {
   return new Promise((resolve, reject) => {
     RNImage.getSize(