From c4ee4c6395864cd9621e820a9cc8bc06b72ccb7b Mon Sep 17 00:00:00 2001 From: Dwynr Date: Thu, 15 Jun 2023 14:40:27 +0200 Subject: [PATCH] feat: only compress if result size is < orignial, api retries --- src/lib/api/api.ts | 20 ++++++++++++++----- src/lib/services/cameraUpload/cameraUpload.ts | 14 +++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/lib/api/api.ts b/src/lib/api/api.ts index 20e6778a..d6aedafa 100644 --- a/src/lib/api/api.ts +++ b/src/lib/api/api.ts @@ -76,7 +76,7 @@ export const apiRequest = ({ return new Promise(async (resolve, reject) => { const dbAPIKey = typeof apiKey === "string" && apiKey.length === 64 ? apiKey : getAPIKey() const cacheKey = "apiCache:" + method.toUpperCase() + ":" + endpoint + ":" + JSON.stringify(data) - let maxTries = 16 + let maxTries = 32 let tries = 0 const retryTimeout = 1000 @@ -139,11 +139,21 @@ export const apiRequest = ({ return } - if (typeof response.data.code === "string" && response.data.code === "api_key_not_found") { - const navigation = useStore.getState().navigation + if (typeof response.data.code === "string") { + if (response.data.code === "api_key_not_found") { + const navigation = useStore.getState().navigation - if (typeof navigation !== "undefined") { - logout({ navigation }) + if (typeof navigation !== "undefined") { + logout({ navigation }) + + return + } + } + + if (response.data.code === "internal_error") { + console.error(new Error(response.data.message)) + + setTimeout(request, retryTimeout) return } diff --git a/src/lib/services/cameraUpload/cameraUpload.ts b/src/lib/services/cameraUpload/cameraUpload.ts index 9eecb8f2..dc0f308d 100644 --- a/src/lib/services/cameraUpload/cameraUpload.ts +++ b/src/lib/services/cameraUpload/cameraUpload.ts @@ -495,6 +495,12 @@ export const compressImage = async (inputPath: string) => { } try { + const statsBefore = await fs.stat(inputPath) + + if (!statsBefore.exists) { + return inputPath + } + const compressed = await ImageResizer.createResizedImage( toExpoFsPath(inputPath), 999999999, @@ -514,6 +520,14 @@ export const compressImage = async (inputPath: string) => { await fs.unlink(inputPath) } + const statsAfter = await fs.stat(compressed.path) + + if (statsAfter.exists && statsAfter.size >= statsBefore.size) { + await fs.unlink(compressed.path) + + return inputPath + } + return toExpoFsPath(compressed.path) } catch (e) { console.error(e)