Repository navigation
Fix - heic context lost #85231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix - heic context lost #85231
Changes from all commits
99faf34
55abec5
2757212
f635b6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,85 @@ | ||
| import {manipulateAsync} from 'expo-image-manipulator'; | ||
| import {ImageManipulator} from 'expo-image-manipulator'; | ||
| import {Platform} from 'react-native'; | ||
| import RNFetchBlob from 'react-native-blob-util'; | ||
| import ImageSize from 'react-native-image-size'; | ||
| import Log from '@libs/Log'; | ||
| import getSaveFormat from './getSaveFormat'; | ||
| import type {CropOrRotateImage} from './types'; | ||
|
|
||
| /** | ||
| * Crops and rotates the image on ios/android | ||
| * Crops and rotates the image on ios/android. | ||
| * On iOS, falls back to the original unprocessed image if manipulation fails | ||
| * (e.g. CGContext allocation failure on 48MP photos). | ||
| */ | ||
| const cropOrRotateImage: CropOrRotateImage = (uri, actions, options) => | ||
| new Promise((resolve) => { | ||
| new Promise((resolve, reject) => { | ||
| const format = getSaveFormat(options.type); | ||
| // We need to remove the base64 value from the result, as it is causing crashes on Release builds. | ||
| // More info: https://github.com/Expensify/App/issues/37963#issuecomment-1989260033 | ||
| manipulateAsync(uri, actions, {compress: options.compress, format}).then(({base64, ...result}) => { | ||
| RNFetchBlob.fs.stat(result.uri.replace('file://', '')).then(({size}) => { | ||
| resolve({ | ||
| ...result, | ||
| size, | ||
| type: options.type || 'image/jpeg', | ||
| name: options.name || 'fileName.jpg', | ||
| }); | ||
| const context = ImageManipulator.manipulate(uri); | ||
| for (const action of actions) { | ||
| if ('crop' in action) { | ||
| context.crop(action.crop); | ||
| } else if ('rotate' in action) { | ||
| context.rotate(action.rotate); | ||
| } | ||
| } | ||
| context | ||
| .renderAsync() | ||
| .then((imageRef) => imageRef.saveAsync({compress: options.compress, format})) | ||
| // We need to remove the base64 value from the result, as it is causing crashes on Release builds. | ||
| // More info: https://github.com/Expensify/App/issues/37963#issuecomment-1989260033 | ||
| .then(({base64, ...result}) => { | ||
| RNFetchBlob.fs | ||
| .stat(result.uri.replace('file://', '')) | ||
| .then(({size}) => { | ||
| resolve({ | ||
| ...result, | ||
| size, | ||
| type: options.type || 'image/jpeg', | ||
| name: options.name || 'fileName.jpg', | ||
| }); | ||
| }) | ||
| .catch(reject); | ||
| }) | ||
| .catch((error) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 Cross-Platform Compatibility & Android Side-Effects - Unintended Fallback on Android Issue: As noted in the issue affected devices and PR description - this logic fix targets an issue existent on iOS mWeb / Native. However, we modified Why it matters: If an Android device fails to manipulate an image (e.g., encountering an OutOfMemoryError typical of lower-end Android devices loading large bit-maps), this Uploading heavily uncompressed local images on Android will cause immediate "Network Request Failed" errors due to payload size limits, or further OOM crashes in the network layer. Suggested Fix: Isolate this fallback logic platform-specifically, using platform specific files as per our guidelines. Pseudocode: import {Platform} from 'react-native';
// Inside the catch block...
.catch((error) => {
if (Platform.OS === 'android') {
Log.warn('Image manipulation failed on Android', {error: error instanceof Error ? error.message : String(error)});
// Allow Android to fail fast and maintain its current lifecycle
return reject(error);
}
// Proceed with iOS fallback logic...
}) |
||
| if (Platform.OS !== 'ios') { | ||
| reject(error); | ||
| return; | ||
| } | ||
|
Comment on lines
+44
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you split this up to ios and android files to remove this check?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should I create new PR with that quick fix? |
||
|
|
||
| Log.warn('Error cropping/rotating image, falling back to original', {error: error instanceof Error ? error.message : String(error)}); | ||
| const filePath = uri.replace('file://', ''); | ||
| ImageSize.getSize(uri) | ||
| .then(({width, height}) => { | ||
| RNFetchBlob.fs | ||
| .stat(filePath) | ||
| .then(({size}) => { | ||
| resolve({ | ||
| uri, | ||
| width: width ?? 0, | ||
| height: height ?? 0, | ||
| size, | ||
| type: options.type || 'image/jpeg', | ||
| name: options.name || 'fileName.jpg', | ||
| }); | ||
| }) | ||
| .catch(reject); | ||
| }) | ||
| .catch(() => { | ||
| RNFetchBlob.fs | ||
| .stat(filePath) | ||
| .then(({size}) => { | ||
| resolve({ | ||
| uri, | ||
| width: 0, | ||
| height: 0, | ||
| size, | ||
| type: options.type || 'image/jpeg', | ||
| name: options.name || 'fileName.jpg', | ||
| }); | ||
| }) | ||
| .catch(reject); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| export default cropOrRotateImage; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,32 @@ | ||
| import {manipulateAsync} from 'expo-image-manipulator'; | ||
| import {ImageManipulator} from 'expo-image-manipulator'; | ||
| import getSaveFormat from './getSaveFormat'; | ||
| import type {CropOrRotateImage} from './types'; | ||
|
|
||
| const cropOrRotateImage: CropOrRotateImage = (uri, actions, options) => | ||
| new Promise((resolve) => { | ||
| new Promise((resolve, reject) => { | ||
| const format = getSaveFormat(options.type); | ||
| manipulateAsync(uri, actions, {compress: options.compress, format}).then((result) => { | ||
| fetch(result.uri) | ||
| .then((res) => res.blob()) | ||
| .then((blob) => { | ||
| const file = new File([blob], options.name || 'fileName.jpeg', {type: options.type || 'image/jpeg'}); | ||
| file.uri = URL.createObjectURL(file); | ||
| resolve(file); | ||
| }); | ||
| }); | ||
| const context = ImageManipulator.manipulate(uri); | ||
| for (const action of actions) { | ||
| if ('crop' in action) { | ||
| context.crop(action.crop); | ||
| } else if ('rotate' in action) { | ||
| context.rotate(action.rotate); | ||
| } | ||
| } | ||
| context | ||
| .renderAsync() | ||
| .then((imageRef) => imageRef.saveAsync({compress: options.compress, format})) | ||
| .then((result) => | ||
| fetch(result.uri) | ||
| .then((res) => res.blob()) | ||
| .then((blob) => { | ||
| const file = new File([blob], options.name || 'fileName.jpeg', {type: options.type || 'image/jpeg'}); | ||
| file.uri = URL.createObjectURL(file); | ||
| resolve(file); | ||
| }) | ||
| .catch(reject), | ||
| ) | ||
| .catch(reject); | ||
| }); | ||
|
|
||
| export default cropOrRotateImage; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This caused #95825, fixed in #96024.
The fallback reuses the original file when HEIC conversion fails — useFilesValidation.tsx:154, AttachmentPicker/index.native.tsx:223.
That file is still HEIC, and the server rejects
image/heiconUploadReceipt— so uploads failed silently and expenses were created without receipts (~185 users/week). Falling back to an unconverted file only works if the consumer accepts that format.