stop accidentally deleting the image while compressing
This commit is contained in:
@@ -26,11 +26,10 @@ export async function compressIfNeeded(
|
|||||||
img: PickerImage,
|
img: PickerImage,
|
||||||
maxSize: number = 1000000,
|
maxSize: number = 1000000,
|
||||||
): Promise<PickerImage> {
|
): Promise<PickerImage> {
|
||||||
const origUri = `file://${img.path}`
|
|
||||||
if (img.size < maxSize) {
|
if (img.size < maxSize) {
|
||||||
return img
|
return img
|
||||||
}
|
}
|
||||||
const resizedImage = await doResize(origUri, {
|
const resizedImage = await doResize(normalizePath(img.path), {
|
||||||
width: img.width,
|
width: img.width,
|
||||||
height: img.height,
|
height: img.height,
|
||||||
mode: 'stretch',
|
mode: 'stretch',
|
||||||
@@ -184,6 +183,7 @@ async function doResize(
|
|||||||
let minQualityPercentage = 0
|
let minQualityPercentage = 0
|
||||||
let maxQualityPercentage = 101 // exclusive
|
let maxQualityPercentage = 101 // exclusive
|
||||||
let newDataUri
|
let newDataUri
|
||||||
|
const intermediateUris = []
|
||||||
|
|
||||||
while (maxQualityPercentage - minQualityPercentage > 1) {
|
while (maxQualityPercentage - minQualityPercentage > 1) {
|
||||||
const qualityPercentage = Math.round(
|
const qualityPercentage = Math.round(
|
||||||
@@ -198,6 +198,8 @@ async function doResize(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
intermediateUris.push(resizeRes.uri)
|
||||||
|
|
||||||
const fileInfo = await getInfoAsync(resizeRes.uri)
|
const fileInfo = await getInfoAsync(resizeRes.uri)
|
||||||
if (!fileInfo.exists) {
|
if (!fileInfo.exists) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -217,8 +219,12 @@ async function doResize(
|
|||||||
} else {
|
} else {
|
||||||
maxQualityPercentage = qualityPercentage
|
maxQualityPercentage = qualityPercentage
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
safeDeleteAsync(resizeRes.uri)
|
for (const intermediateUri of intermediateUris) {
|
||||||
|
if (newDataUri?.path !== normalizePath(intermediateUri)) {
|
||||||
|
safeDeleteAsync(intermediateUri)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newDataUri) {
|
if (newDataUri) {
|
||||||
|
|||||||
Reference in New Issue
Block a user