From 0e160298c86eddb21c9d8233268083b883c75542 Mon Sep 17 00:00:00 2001 From: Aryan Goharzad Date: Tue, 7 Feb 2023 15:29:33 -0500 Subject: [PATCH] 154 cached image profile (#167) * Fixes issue where retrying on image upload fails * Lint, longer test time * Longer waitfor time in tests * even longer timeout * longer timeout * missed file * Fixes image cache error on second try for profile screen * lint * lint * lint --- src/lib/images.ts | 22 +++++++++++++++++-- src/view/com/composer/PhotoCarouselPicker.tsx | 22 +++++-------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/lib/images.ts b/src/lib/images.ts index 8d5eaded0f..7b1a939615 100644 --- a/src/lib/images.ts +++ b/src/lib/images.ts @@ -2,7 +2,7 @@ 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 uuid from 'react-native-uuid' import * as Toast from '../view/com/util/Toast' export interface DownloadAndResizeOpts { @@ -109,12 +109,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 +156,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 +} diff --git a/src/view/com/composer/PhotoCarouselPicker.tsx b/src/view/com/composer/PhotoCarouselPicker.tsx index 4bc275a522..162fd0fb29 100644 --- a/src/view/com/composer/PhotoCarouselPicker.tsx +++ b/src/view/com/composer/PhotoCarouselPicker.tsx @@ -1,13 +1,12 @@ 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 {useAnalytics} from '@segment/analytics-react-native' import { openPicker, openCamera, openCropper, } from 'react-native-image-crop-picker' -import RNFS from 'react-native-fs' import { UserLocalPhotosModel, PhotoIdentifier, @@ -16,10 +15,13 @@ import { requestPhotoAccessIfNeeded, requestCameraAccessIfNeeded, } from '../../../lib/permissions' -import {compressIfNeeded, scaleDownDimensions} from '../../../lib/images' +import { + compressIfNeeded, + moveToPremanantPath, + scaleDownDimensions, +} from '../../../lib/images' import {usePalette} from '../../lib/hooks/usePalette' import {useStores} from '../../../state' -import {useAnalytics} from '@segment/analytics-react-native' const MAX_WIDTH = 2000 const MAX_HEIGHT = 2000 @@ -33,18 +35,6 @@ 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,