diff options
Diffstat (limited to 'src/lib/media/manip.ts')
-rw-r--r-- | src/lib/media/manip.ts | 31 |
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( |