Skip to content

Commit

Permalink
[Draft] Fixes image failing on reupload issue (#128)
Browse files Browse the repository at this point in the history
* Fixes image failing on reupload issue

* Use tmp folder instead of documents

* lint
  • Loading branch information
arrygoo authored Jan 31, 2023
1 parent d26f024 commit 0ce4f57
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"react-native-svg": "^12.4.0",
"react-native-tab-view": "^3.3.0",
"react-native-url-polyfill": "^1.3.0",
"react-native-uuid": "^2.0.1",
"react-native-version-number": "^0.3.6",
"react-native-web": "^0.17.7",
"rn-fetch-blob": "^0.12.0",
Expand Down
21 changes: 19 additions & 2 deletions src/view/com/composer/PhotoCarouselPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, {useCallback} from 'react'
import {Image, StyleSheet, TouchableOpacity, ScrollView} from 'react-native'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import uuid from 'react-native-uuid'
import {
openPicker,
openCamera,
openCropper,
} from 'react-native-image-crop-picker'
import RNFS from 'react-native-fs'
import {
UserLocalPhotosModel,
PhotoIdentifier,
Expand All @@ -26,6 +28,18 @@ const IMAGE_PARAMS = {
compressImageQuality: 1.0,
}

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
}

export async function cropPhoto(
path: string,
imgWidth = MAX_WIDTH,
Expand All @@ -44,8 +58,10 @@ export async function cropPhoto(
width,
height,
})

const img = await compressIfNeeded(cropperRes, MAX_SIZE)
return img.path
const permanentPath = await moveToPremanantPath(img.path)
return permanentPath
}

export const PhotoCarouselPicker = ({
Expand Down Expand Up @@ -114,7 +130,8 @@ export const PhotoCarouselPicker = ({
height,
})
const finalImg = await compressIfNeeded(cropperRes, MAX_SIZE)
result.push(finalImg.path)
const permanentPath = await moveToPremanantPath(finalImg.path)
result.push(permanentPath)
}
onSelectPhotos([...selectedPhotos, ...result])
})
Expand Down

0 comments on commit 0ce4f57

Please sign in to comment.