about summary refs log tree commit diff
path: root/src/lib/images.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/images.ts')
-rw-r--r--src/lib/images.ts32
1 files changed, 23 insertions, 9 deletions
diff --git a/src/lib/images.ts b/src/lib/images.ts
index 8d5eaded0..609e03bda 100644
--- a/src/lib/images.ts
+++ b/src/lib/images.ts
@@ -2,8 +2,8 @@ import RNFetchBlob from 'rn-fetch-blob'
 import ImageResizer from '@bam.tech/react-native-image-resizer'
 import {Share} from 'react-native'
 import RNFS from 'react-native-fs'
-
-import * as Toast from '../view/com/util/Toast'
+import uuid from 'react-native-uuid'
+import * as Toast from 'view/com/util/Toast'
 
 export interface DownloadAndResizeOpts {
   uri: string
@@ -23,16 +23,12 @@ export interface Image {
 }
 
 export async function downloadAndResize(opts: DownloadAndResizeOpts) {
-  let appendExt
+  let appendExt = 'jpeg'
   try {
     const urip = new URL(opts.uri)
     const ext = urip.pathname.split('.').pop()
-    if (ext === 'jpg' || ext === 'jpeg') {
-      appendExt = 'jpeg'
-    } else if (ext === 'png') {
+    if (ext === 'png') {
       appendExt = 'png'
-    } else {
-      return
     }
   } catch (e: any) {
     console.error('Invalid URI', opts.uri, e)
@@ -109,12 +105,18 @@ export async function compressIfNeeded(
   if (img.size < maxSize) {
     return img
   }
-  return await resize(origUri, {
+  const resizedImage = await resize(origUri, {
     width: img.width,
     height: img.height,
     mode: 'stretch',
     maxSize,
   })
+  const finalImageMovedPath = await moveToPremanantPath(resizedImage.path)
+  const finalImg = {
+    ...resizedImage,
+    path: finalImageMovedPath,
+  }
+  return finalImg
 }
 
 export interface Dim {
@@ -150,3 +152,15 @@ export const saveImageModal = async ({uri}: {uri: string}) => {
   }
   RNFS.unlink(imagePath)
 }
+
+export const moveToPremanantPath = async (path: string) => {
+  /*
+  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:
+  https://github.com/ivpusic/react-native-image-crop-picker/issues/1199
+  */
+  const filename = uuid.v4()
+  const destinationPath = `${RNFS.TemporaryDirectoryPath}/${filename}`
+  RNFS.moveFile(path, destinationPath)
+  return destinationPath
+}