diff options
author | hailey <me@haileyok.com> | 2025-05-06 10:54:08 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-05-06 10:54:08 -0700 |
commit | 521ec8e044e58633530e1864e7abc6e22554d7d3 (patch) | |
tree | e57139a4cfcb9f8859f5e1af008740fc3e8306e3 /src/lib/media/manip.ts | |
parent | 973538d246a3f76550611e438152f1a6cad75f49 (diff) | |
download | voidsky-521ec8e044e58633530e1864e7abc6e22554d7d3.tar.zst |
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>
Diffstat (limited to 'src/lib/media/manip.ts')
-rw-r--r-- | src/lib/media/manip.ts | 25 |
1 files changed, 17 insertions, 8 deletions
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<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) { |