Skip to content

Commit

Permalink
feat: only compress if result size is < orignial, api retries
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Jun 15, 2023
1 parent 320d080 commit c4ee4c6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/lib/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 14 additions & 0 deletions src/lib/services/cameraUpload/cameraUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit c4ee4c6

Please sign in to comment.