Skip to content

Commit f071496

Browse files
authored
fix: images posted to server via fetch get their size inflated significantly for iOS (#6749)
* fix: file size for iOS * chore: add info about problem/hack in Hacks.md
1 parent 32ddc9f commit f071496

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

HACKS.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,15 @@ When we upgrade to a react native version that contentOffset works for android
351351

352352
contentOffset for vertical scrollviews appear to be broken on v0.66.4 (current version). We need the contentOffset to initially hide the HeaderComponent. The workaround currently is to use contentContainerStyle and set the marginTop to -HeightOfHeaderComponent and then remove this when a user scrolls to the top.
353353
Caveat/Introduced bug: User has to scroll to an offset of any number > 0 to revert the marginTop to 0.
354+
355+
## [iOS specific bug]: Images posted to server via fetch in `multipart/form-data` get their size inflated significantly
356+
357+
#### When we can remove this:
358+
359+
When this problem will be fixed in one of the next React Native releases (fixes were already added in 0.66.4, but were eventually [reversed](https://github.com/facebook/react-native/commit/e83feffeba567b4e304181632128ee1caa036c45))
360+
361+
#### Explanation/Context
362+
363+
When we try to upload a file with the `.jpg` extension in `multipart/form-data` format via `fetch`, then the size of this file will be increased on iOS. That bug happens not due to the file content nor for the blob MIME but just due to the file extension. See more about this problem [here](https://github.com/facebook/react-native/issues/27099).
364+
365+
One of the simplest solutions to this problem is to rename the file and add a suffix (for example, from `filename.jpg` to `filename.jpg.toUpload`)

src/app/Scenes/ReverseSearchImageResults/ReverseSearchImageResults.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import { resizeImage } from "app/utils/resizeImage"
1010
import { ReactNativeFile } from "extract-files"
1111
import { Box, Button, Screen, Text } from "palette"
1212
import { useState } from "react"
13-
import { Alert } from "react-native"
13+
import { Alert, Platform } from "react-native"
1414
import { graphql } from "react-relay"
1515
import { fetchQuery } from "relay-runtime"
16+
import RNFetchBlob from "rn-fetch-blob"
1617
import { ReverseSearchImageResultItemFragmentContainer } from "./ReverseSearchImageResultItem"
1718

1819
type SearchImageResults = NonNullable<
@@ -50,6 +51,25 @@ export const ReverseSearchImageResults = () => {
5051
quality: 85,
5152
onlyScaleDown: true,
5253
})
54+
55+
/**
56+
* Images posted to server via fetch get their size inflated significantly for iOS.
57+
* This is a small hack to solve this problem
58+
*
59+
* You can find more context here: https://github.com/facebook/react-native/issues/27099
60+
*/
61+
if (Platform.OS === "ios") {
62+
const updatedPath = replaceFilenameInValue(resizedImage.path, resizedImage.name)
63+
const updatedURI = replaceFilenameInValue(resizedImage.uri, resizedImage.name)
64+
const updatedFilename = replaceFilenameInValue(resizedImage.name, resizedImage.name)
65+
66+
await RNFetchBlob.fs.mv(resizedImage.path, updatedPath)
67+
68+
resizedImage.path = updatedPath
69+
resizedImage.uri = updatedURI
70+
resizedImage.name = updatedFilename
71+
}
72+
5373
const fileImage = new ReactNativeFile({
5474
uri: resizedImage.uri,
5575
name: resizedImage.name,
@@ -116,3 +136,7 @@ export const ReverseSearchImageResults = () => {
116136
</Screen>
117137
)
118138
}
139+
140+
const replaceFilenameInValue = (value: string, filename: string) => {
141+
return value.replace(filename, `${filename}.toUpload`)
142+
}

0 commit comments

Comments
 (0)