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 <mozzius@protonmail.com>
This commit is contained in:
hailey
2025-05-06 10:54:08 -07:00
committed by GitHub
parent 973538d246
commit 521ec8e044
23 changed files with 145 additions and 215 deletions
+17 -8
View File
@@ -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<Image> {
const origUri = `file://${img.path}`
): Promise<PickerImage> {
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<Image> {
async function doResize(
localUri: string,
opts: DoResizeOpts,
): Promise<PickerImage> {
// 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<Image> {
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<Image> {
},
)
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<Image> {
} else {
maxQualityPercentage = qualityPercentage
}
}
safeDeleteAsync(resizeRes.uri)
for (const intermediateUri of intermediateUris) {
if (newDataUri?.path !== normalizePath(intermediateUri)) {
safeDeleteAsync(intermediateUri)
}
}
if (newDataUri) {