Fix image sharing on iOS (#561)

zio/stable
Paul Frazee 2023-05-02 20:00:22 -05:00 committed by GitHub
parent bd80db619b
commit ddb8ebb412
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 9 deletions

View File

@ -1,13 +1,13 @@
import RNFetchBlob from 'rn-fetch-blob' import RNFetchBlob from 'rn-fetch-blob'
import ImageResizer from '@bam.tech/react-native-image-resizer' import ImageResizer from '@bam.tech/react-native-image-resizer'
import {Image as RNImage} from 'react-native' import {Image as RNImage, Share as RNShare} from 'react-native'
import {Image} from 'react-native-image-crop-picker' import {Image} from 'react-native-image-crop-picker'
import RNFS from 'react-native-fs' import * as RNFS from 'react-native-fs'
import uuid from 'react-native-uuid' import uuid from 'react-native-uuid'
import * as Sharing from 'expo-sharing' import * as Sharing from 'expo-sharing'
import {Dimensions} from './types' import {Dimensions} from './types'
import {POST_IMG_MAX} from 'lib/constants' import {POST_IMG_MAX} from 'lib/constants'
import {isAndroid} from 'platform/detection' import {isAndroid, isIOS} from 'platform/detection'
export async function compressAndResizeImageForPost( export async function compressAndResizeImageForPost(
image: Image, image: Image,
@ -128,11 +128,26 @@ export async function saveImageModal({uri}: {uri: string}) {
fileCache: true, fileCache: true,
}).fetch('GET', uri) }).fetch('GET', uri)
// NOTE
// assuming PNG
// we're currently relying on the fact our CDN only serves pngs
// -prf
let imagePath = downloadResponse.path() let imagePath = downloadResponse.path()
await Sharing.shareAsync(normalizePath(imagePath, true), { imagePath = normalizePath(await moveToPermanentPath(imagePath, '.png'), true)
mimeType: 'image/png',
UTI: 'public.png', // NOTE
}) // for some reason expo-sharing refuses to work on iOS
// ...and visa versa
// -prf
if (isIOS) {
await RNShare.share({url: imagePath})
} else {
await Sharing.shareAsync(imagePath, {
mimeType: 'image/png',
UTI: 'image/png',
})
}
RNFS.unlink(imagePath) RNFS.unlink(imagePath)
} }
@ -187,7 +202,7 @@ async function doResize(localUri: string, opts: DoResizeOpts): Promise<Image> {
) )
} }
async function moveToPermanentPath(path: string): Promise<string> { async function moveToPermanentPath(path: string, ext = ''): Promise<string> {
/* /*
Since this package stores images in a temp directory, we need to move the file to a permanent location. Since this package stores images in a temp directory, we need to move the file to a permanent location.
Relevant: IOS bug when trying to open a second time: Relevant: IOS bug when trying to open a second time:
@ -195,11 +210,26 @@ async function moveToPermanentPath(path: string): Promise<string> {
*/ */
const filename = uuid.v4() const filename = uuid.v4()
const destinationPath = `${RNFS.TemporaryDirectoryPath}/${filename}` const destinationPath = joinPath(
RNFS.TemporaryDirectoryPath,
`${filename}${ext}`,
)
await RNFS.moveFile(path, destinationPath) await RNFS.moveFile(path, destinationPath)
return normalizePath(destinationPath) return normalizePath(destinationPath)
} }
function joinPath(a: string, b: string) {
if (a.endsWith('/')) {
if (b.startsWith('/')) {
return a.slice(0, -1) + b
}
return a + b
} else if (b.startsWith('/')) {
return a + b
}
return a + '/' + b
}
function normalizePath(str: string, allPlatforms = false): string { function normalizePath(str: string, allPlatforms = false): string {
if (isAndroid || allPlatforms) { if (isAndroid || allPlatforms) {
if (!str.startsWith('file://')) { if (!str.startsWith('file://')) {