Repository navigation
Fix iOS scan crash with 30 HEIC images when creating expense #99679
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
Merged
stitesExpensify
merged 23 commits into
Expensify:main
from
nabi-ebrahimi:fix-gallery-heic-sequential-processing
Sep 9, 2026
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
fe90615
Fix iOS HEIC gallery batch conversion OOM
nabi-ebrahimi 2d682c3
Merge branch 'main' into fix-gallery-heic-sequential-processing
nabi-ebrahimi 5604790
test added
nabi-ebrahimi 4b3d0a5
Merge branch 'main' into fix-gallery-heic-sequential-processing
nabi-ebrahimi 5881365
Merge branch 'main' into fix-gallery-heic-sequential-processing
nabi-ebrahimi 6621abc
Merge branch 'main' into fix-gallery-heic-sequential-processing
nabi-ebrahimi b6ab747
Merge branch 'main' into fix-gallery-heic-sequential-processing
nabi-ebrahimi 66de2e9
Localize attachment error fallback copy
nabi-ebrahimi 35f6439
Merge branch 'main' into fix-gallery-heic-sequential-processing
nabi-ebrahimi 574be48
Merge branch 'main' into fix-gallery-heic-sequential-processing
nabi-ebrahimi ee0b963
Collapse picker failures into a single alert
nabi-ebrahimi 0c5e13a
Use canonical translate type
nabi-ebrahimi a796615
Keep converted asset fileName a string
nabi-ebrahimi 2c5a4f5
Move picker asset processing to a lib module
nabi-ebrahimi a459c8d
Add coverage for skip and order preservation
nabi-ebrahimi d2cb822
Release native images quietly
nabi-ebrahimi 7eb377b
Merge branch 'main' into fix-gallery-heic-sequential-processing
nabi-ebrahimi 15eb441
Merge branch 'main' into fix-gallery-heic-sequential-processing
nabi-ebrahimi 6c904c5
Resolve picker asset processing per platform
nabi-ebrahimi c8b7e71
Mock getFileName in picker asset tests
nabi-ebrahimi b1534c2
Add shared type for picker asset processing
nabi-ebrahimi a3f8c01
Add tests for fallbacks and partial success
nabi-ebrahimi bd99257
Document heicConverter divergence
nabi-ebrahimi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
src/libs/fileDownload/processPickedAssets/index.native.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import {getFileName, verifyFileFormat} from '@libs/fileDownload/FileUtils'; | ||
| import Log from '@libs/Log'; | ||
|
|
||
| import CONST from '@src/CONST'; | ||
|
|
||
| import type {Asset} from 'react-native-image-picker'; | ||
|
|
||
| import {ImageManipulator, SaveFormat} from 'expo-image-manipulator'; | ||
|
|
||
| import type ProcessPickedAssetsFunction from './types'; | ||
|
|
||
| /** | ||
| * Ensures asset has proper fileName and type properties. Callers only pass assets that have a `uri`. | ||
| */ | ||
| function processAssetWithFallbacks(asset: Asset): Asset { | ||
| return { | ||
| ...asset, | ||
| fileName: asset.fileName ?? getFileName(asset.uri ?? ''), | ||
| // Default to JPEG if no type specified | ||
| type: asset.type ?? 'image/jpeg', | ||
| }; | ||
| } | ||
|
|
||
| function getErrorMessage(error: unknown, fallback: string): string { | ||
| return error instanceof Error && error.message ? error.message : fallback; | ||
| } | ||
|
|
||
| /** | ||
| * Frees a native image resource. Releasing is best-effort cleanup, so a failure here must never change | ||
| * whether the converted asset is kept. | ||
| */ | ||
| function releaseQuietly(releasable: {release: () => void}) { | ||
| try { | ||
| releasable.release(); | ||
| } catch (error) { | ||
| Log.warn('Failed to release native image resource', {error: getErrorMessage(error, 'An unknown error occurred')}); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Transcodes a single HEIC image to JPEG, returning `undefined` if the conversion fails. | ||
| * | ||
| * The native context and the rendered bitmap are released as soon as they are no longer needed rather | ||
| * than waiting for the garbage collector, which has no visibility into the native memory they retain. | ||
| * | ||
| * This repeats the manipulate/render/save sequence from `heicConverter` rather than calling it, because | ||
| * that helper decides what to convert from the `.heic`/`.heif` extension. react-native-image-picker | ||
| * relabels the extension without transcoding, so this path has to detect HEIC from the file signature | ||
| * instead and cannot go through `convertHeicImage`. | ||
| */ | ||
| async function convertHeicToJpeg(uri: string): Promise<Asset | undefined> { | ||
| const imageManipulatorContext = ImageManipulator.manipulate(uri); | ||
| try { | ||
| const manipulatedImage = await imageManipulatorContext.renderAsync(); | ||
| try { | ||
| const manipulationResult = await manipulatedImage.saveAsync({format: SaveFormat.JPEG}); | ||
| return { | ||
| uri: manipulationResult.uri, | ||
| fileName: getFileName(manipulationResult.uri), | ||
| type: 'image/jpeg', | ||
| width: manipulationResult.width, | ||
| height: manipulationResult.height, | ||
| }; | ||
| } finally { | ||
| releaseQuietly(manipulatedImage); | ||
| } | ||
| } catch (error) { | ||
| Log.warn('Failed to convert HEIC image, skipping asset', {error: getErrorMessage(error, 'An unknown error occurred')}); | ||
| return undefined; | ||
| } finally { | ||
| releaseQuietly(imageManipulatorContext); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Convert the picked assets one at a time, transcoding any HEIC images to JPEG. | ||
| * | ||
| * The conversion is deliberately sequential: `ImageManipulator` decodes each image into a full-size | ||
| * bitmap in native memory, so converting a whole selection at once (the picker allows up to | ||
| * `CONST.API_ATTACHMENT_VALIDATIONS.MAX_FILE_LIMIT` files) holds every bitmap simultaneously and the | ||
| * OS terminates the app for exceeding its memory limit. Processing one image at a time keeps the peak | ||
| * at a single bitmap regardless of how many files were picked. | ||
| */ | ||
| const processPickedAssetsSequentially: ProcessPickedAssetsFunction = async (assets, showGeneralAlert, translate) => { | ||
| const processedAssets: Asset[] = []; | ||
| // Collected instead of alerted inline so the whole selection produces a single alert: alerting per | ||
| // asset would leave the user dismissing one native modal after another. | ||
| const failureMessages = new Set<string>(); | ||
|
|
||
| for (const asset of assets) { | ||
| if (!asset.uri) { | ||
| continue; | ||
| } | ||
|
|
||
| if (!asset.type?.startsWith('image')) { | ||
| // Ensure the asset has proper fileName and type | ||
| processedAssets.push(processAssetWithFallbacks(asset)); | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| // eslint-disable-next-line no-await-in-loop -- converting one image at a time is the point, see the doc comment above | ||
| const isHEIC = await verifyFileFormat({fileUri: asset.uri, formatSignatures: CONST.HEIC_SIGNATURES}); | ||
|
|
||
| if (!isHEIC) { | ||
| // Ensure the asset has proper fileName and type for non-HEIC images | ||
| processedAssets.push(processAssetWithFallbacks(asset)); | ||
| continue; | ||
| } | ||
|
|
||
| // react-native-image-picker incorrectly changes file extension without transcoding the HEIC file, so we are doing it manually if we detect HEIC signature | ||
| // eslint-disable-next-line no-await-in-loop -- converting one image at a time is the point, see the doc comment above | ||
| const convertedAsset = await convertHeicToJpeg(asset.uri); | ||
|
|
||
| if (convertedAsset) { | ||
| processedAssets.push(convertedAsset); | ||
| } else { | ||
| failureMessages.add(translate('attachmentPicker.errorWhileConvertingHeic')); | ||
| } | ||
| } catch (error) { | ||
| failureMessages.add(getErrorMessage(error, translate('attachmentPicker.errorWhileSelectingAttachment'))); | ||
| } | ||
| } | ||
|
|
||
| if (failureMessages.size > 0) { | ||
| showGeneralAlert([...failureMessages].join('\n')); | ||
| } | ||
|
|
||
| return processedAssets.length > 0 ? processedAssets : undefined; | ||
| }; | ||
|
|
||
| export default processPickedAssetsSequentially; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import type ProcessPickedAssetsFunction from './types'; | ||
|
|
||
| /** | ||
| * Web has no native image picker, so nothing reaches this module. It exists so the native | ||
| * implementation resolves through a platform-agnostic import path. | ||
| */ | ||
| const processPickedAssetsSequentially: ProcessPickedAssetsFunction = (assets) => Promise.resolve(assets.length > 0 ? assets : undefined); | ||
|
|
||
| export default processPickedAssetsSequentially; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import type {LocaleContextProps} from '@components/LocaleContextProvider'; | ||
|
|
||
| import type {Asset} from 'react-native-image-picker'; | ||
|
|
||
| type ProcessPickedAssetsFunction = (assets: Asset[], showGeneralAlert: (message?: string) => void, translate: LocaleContextProps['translate']) => Promise<Asset[] | undefined>; | ||
|
|
||
| export default ProcessPickedAssetsFunction; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.