From 521ec8e044e58633530e1864e7abc6e22554d7d3 Mon Sep 17 00:00:00 2001 From: hailey Date: Tue, 6 May 2025 10:54:08 -0700 Subject: swap out cropper library (#8327) * mostly implement * type errors * unused import * rm comment * stop accidentally deleting the image while compressing * upgrade * type fixes * upgrade, remove timeout * bump * rm mock * bump --------- Co-authored-by: Samuel Newman --- src/lib/media/manip.ts | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'src/lib/media/manip.ts') diff --git a/src/lib/media/manip.ts b/src/lib/media/manip.ts index f6ef8347d..ff5b71ace 100644 --- a/src/lib/media/manip.ts +++ b/src/lib/media/manip.ts @@ -1,5 +1,4 @@ import {Image as RNImage, Share as RNShare} from 'react-native' -import {Image} from 'react-native-image-crop-picker' import uuid from 'react-native-uuid' import { cacheDirectory, @@ -20,17 +19,17 @@ import RNFetchBlob from 'rn-fetch-blob' import {POST_IMG_MAX} from '#/lib/constants' import {logger} from '#/logger' import {isAndroid, isIOS} from '#/platform/detection' -import {Dimensions} from './types' +import {type PickerImage} from './picker.shared' +import {type Dimensions} from './types' export async function compressIfNeeded( - img: Image, + img: PickerImage, maxSize: number = 1000000, -): Promise { - const origUri = `file://${img.path}` +): Promise { if (img.size < maxSize) { return img } - const resizedImage = await doResize(origUri, { + const resizedImage = await doResize(normalizePath(img.path), { width: img.width, height: img.height, mode: 'stretch', @@ -166,7 +165,10 @@ interface DoResizeOpts { maxSize: number } -async function doResize(localUri: string, opts: DoResizeOpts): Promise { +async function doResize( + localUri: string, + opts: DoResizeOpts, +): Promise { // We need to get the dimensions of the image before we resize it. Previously, the library we used allowed us to enter // a "max size", and it would do the "best possible size" calculation for us. // Now instead, we have to supply the final dimensions to the manipulation function instead. @@ -181,6 +183,7 @@ async function doResize(localUri: string, opts: DoResizeOpts): Promise { let minQualityPercentage = 0 let maxQualityPercentage = 101 // exclusive let newDataUri + const intermediateUris = [] while (maxQualityPercentage - minQualityPercentage > 1) { const qualityPercentage = Math.round( @@ -195,6 +198,8 @@ async function doResize(localUri: string, opts: DoResizeOpts): Promise { }, ) + intermediateUris.push(resizeRes.uri) + const fileInfo = await getInfoAsync(resizeRes.uri) if (!fileInfo.exists) { throw new Error( @@ -214,8 +219,12 @@ async function doResize(localUri: string, opts: DoResizeOpts): Promise { } else { maxQualityPercentage = qualityPercentage } + } - safeDeleteAsync(resizeRes.uri) + for (const intermediateUri of intermediateUris) { + if (newDataUri?.path !== normalizePath(intermediateUri)) { + safeDeleteAsync(intermediateUri) + } } if (newDataUri) { -- cgit 1.4.1