From 0a4f885dbfa94af8ec6f99b44099864a0fde3127 Mon Sep 17 00:00:00 2001 From: "Aimane Chnaif (via MelvinBot)" Date: Wed, 16 Sep 2026 16:10:31 +0000 Subject: [PATCH 1/3] Read the picked file name from the URI when the native picker returns no metadata On iOS the document picker can return a file with name, size and type all null when it fails to read the file's resource values. FilePicker replaced the missing name with the extensionless placeholder 'spreadsheet', so extension checks downstream rejected the file - .ofx and .qfx statement imports failed with 'Invalid file' on device. Recover the name from the URI, which still carries it, and keep the placeholder only when the URI has no extension either. Co-authored-by: Aimane Chnaif --- src/components/FilePicker/index.native.tsx | 21 ++++++++++++++------ src/libs/fileDownload/FileUtils.ts | 23 ++++++++++++++++++++++ tests/unit/FileUtilsTest.ts | 23 ++++++++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/components/FilePicker/index.native.tsx b/src/components/FilePicker/index.native.tsx index 5eda6ac64942..422e9c036813 100644 --- a/src/components/FilePicker/index.native.tsx +++ b/src/components/FilePicker/index.native.tsx @@ -1,7 +1,8 @@ import useLocalize from '@hooks/useLocalize'; -import {cleanFileName} from '@libs/fileDownload/FileUtils'; +import {cleanFileName, getFileNameWithFallback} from '@libs/fileDownload/FileUtils'; import fileURIToPath from '@libs/fileURIToPath'; +import Log from '@libs/Log'; import type {FileObject} from '@src/types/utils/Attachment'; @@ -13,20 +14,22 @@ import RNFetchBlob from 'react-native-blob-util'; import type FilePickerProps from './types'; type LocalCopy = { - name: string | null; + name: string; uri: string; size: number | null; type: string | null; }; +/** Used as the file name only when the picker gives us no name and the URI carries no extension either. */ +const DEFAULT_FILE_NAME = 'spreadsheet'; + /** * The data returned from `show` is different on web and mobile, * use this function to ensure the data will be handled properly. */ const getDataForUpload = (fileData: LocalCopy): Promise => { - const fileName = fileData.name ?? 'spreadsheet'; const fileResult: FileObject = { - name: cleanFileName(fileName), + name: fileData.name, type: fileData.type ?? undefined, uri: fileData.uri, size: fileData.size, @@ -91,11 +94,17 @@ function FilePicker({children}: FilePickerProps) { type: [types.allFiles], }); + const fileName = getFileNameWithFallback(file.name, file.uri, DEFAULT_FILE_NAME); + + if (!file.name) { + Log.warn('[FilePicker] The picker returned a file without metadata, so the file name was read from the URI', {error: file.error, fileName}); + } + const [localCopy] = await keepLocalCopy({ files: [ { uri: file.uri, - fileName: file.name ?? 'spreadsheet', + fileName, }, ], destination: 'cachesDirectory', @@ -106,7 +115,7 @@ function FilePicker({children}: FilePickerProps) { } return { - name: cleanFileName(file.name ?? 'spreadsheet'), + name: cleanFileName(fileName), type: file.type, uri: localCopy.localUri, size: file.size, diff --git a/src/libs/fileDownload/FileUtils.ts b/src/libs/fileDownload/FileUtils.ts index 7cd27ecbd005..dee4deb63513 100644 --- a/src/libs/fileDownload/FileUtils.ts +++ b/src/libs/fileDownload/FileUtils.ts @@ -159,6 +159,28 @@ const splitExtensionFromFileName: SplitExtensionFromFileName = (fullFileName) => return {fileName: splitFileName.join('.'), fileExtension: fileExtension ?? ''}; }; +/** + * Returns the file name, recovering it from the URI when the name itself is missing. + * + * A file picker can hand back a file with no metadata at all - the iOS document picker does this whenever it fails to + * read the file's resource values, leaving `name`, `size` and `type` null. The URI still holds the real file name in + * that case, so take it from there rather than from a placeholder that carries no extension, which every extension + * check downstream would reject. `defaultFileName` is only used when the URI has no extension either, as is the case + * for the `content://` URIs Android returns. + */ +function getFileNameWithFallback(fileName: string | null | undefined, uri: string, defaultFileName: string): string { + if (fileName) { + return fileName; + } + + const fileNameFromURI = getFileName(uri); + if (!splitExtensionFromFileName(fileNameFromURI).fileExtension) { + return defaultFileName; + } + + return fileNameFromURI; +} + /** * Returns the MIME type for a given file extension. * Falls back to 'application/octet-stream' for unrecognized extensions. @@ -946,6 +968,7 @@ export { splitExtensionFromFileName, getMimeType, getFileName, + getFileNameWithFallback, getFileType, cleanFileName, getExportFileName, diff --git a/tests/unit/FileUtilsTest.ts b/tests/unit/FileUtilsTest.ts index da582906c23e..e5db077dea77 100644 --- a/tests/unit/FileUtilsTest.ts +++ b/tests/unit/FileUtilsTest.ts @@ -6,6 +6,7 @@ import { appendTimeToFileName, canvasFallback, getExportFileName, + getFileNameWithFallback, getFileValidationErrorText, getImageDimensionsAfterResize, isHighResolutionImage, @@ -45,6 +46,28 @@ describe('FileUtils', () => { }); }); + describe('getFileNameWithFallback', () => { + it('should return the given file name when there is one', () => { + expect(getFileNameWithFallback('statement.qfx', 'file:///tmp/other.csv', 'spreadsheet')).toEqual('statement.qfx'); + }); + + it('should read the file name from the URI when the picker returns no name', () => { + expect(getFileNameWithFallback(null, 'file:///private/var/mobile/Containers/Data/Application/ABC/tmp/statement.qfx', 'spreadsheet')).toEqual('statement.qfx'); + }); + + it('should decode the file name read from the URI and replace illegal characters', () => { + expect(getFileNameWithFallback(null, 'file:///tmp/bank%20statement%3A2026.qfx', 'spreadsheet')).toEqual('bank statement_2026.qfx'); + }); + + it('should return the default file name when the URI has no extension either', () => { + expect(getFileNameWithFallback(null, 'content://com.android.providers.media.documents/document/12345', 'spreadsheet')).toEqual('spreadsheet'); + }); + + it('should return the default file name when both the name and the URI are empty', () => { + expect(getFileNameWithFallback('', '', 'spreadsheet')).toEqual('spreadsheet'); + }); + }); + describe('appendTimeToFileName', () => { it('should append current time to the end of the file name', () => { const actualFileName = appendTimeToFileName('image.jpg'); From 57d060a65943e2096ae4f01eae2901358fc020d9 Mon Sep 17 00:00:00 2001 From: "Aimane Chnaif (via MelvinBot)" Date: Wed, 16 Sep 2026 16:42:10 +0000 Subject: [PATCH 2/3] Minify the getFileNameWithFallback JSDoc Co-authored-by: Aimane Chnaif --- src/libs/fileDownload/FileUtils.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/libs/fileDownload/FileUtils.ts b/src/libs/fileDownload/FileUtils.ts index dee4deb63513..b23ad8f6cde5 100644 --- a/src/libs/fileDownload/FileUtils.ts +++ b/src/libs/fileDownload/FileUtils.ts @@ -160,13 +160,8 @@ const splitExtensionFromFileName: SplitExtensionFromFileName = (fullFileName) => }; /** - * Returns the file name, recovering it from the URI when the name itself is missing. - * - * A file picker can hand back a file with no metadata at all - the iOS document picker does this whenever it fails to - * read the file's resource values, leaving `name`, `size` and `type` null. The URI still holds the real file name in - * that case, so take it from there rather than from a placeholder that carries no extension, which every extension - * check downstream would reject. `defaultFileName` is only used when the URI has no extension either, as is the case - * for the `content://` URIs Android returns. + * Returns the file name, reading it from the URI when the picker returns none, as the iOS document picker can. + * Falls back to `defaultFileName` only when the URI has no extension either, as with Android's `content://` URIs. */ function getFileNameWithFallback(fileName: string | null | undefined, uri: string, defaultFileName: string): string { if (fileName) { From b40cf3ee9415363bf1cba7c218b10bbc64564226 Mon Sep 17 00:00:00 2001 From: "Aimane Chnaif (via MelvinBot)" Date: Wed, 16 Sep 2026 17:26:46 +0000 Subject: [PATCH 3/3] Remove the FilePicker missing-metadata warning log Co-authored-by: Aimane Chnaif --- src/components/FilePicker/index.native.tsx | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/components/FilePicker/index.native.tsx b/src/components/FilePicker/index.native.tsx index 422e9c036813..dc88f7b89814 100644 --- a/src/components/FilePicker/index.native.tsx +++ b/src/components/FilePicker/index.native.tsx @@ -2,7 +2,6 @@ import useLocalize from '@hooks/useLocalize'; import {cleanFileName, getFileNameWithFallback} from '@libs/fileDownload/FileUtils'; import fileURIToPath from '@libs/fileURIToPath'; -import Log from '@libs/Log'; import type {FileObject} from '@src/types/utils/Attachment'; @@ -96,10 +95,6 @@ function FilePicker({children}: FilePickerProps) { const fileName = getFileNameWithFallback(file.name, file.uri, DEFAULT_FILE_NAME); - if (!file.name) { - Log.warn('[FilePicker] The picker returned a file without metadata, so the file name was read from the URI', {error: file.error, fileName}); - } - const [localCopy] = await keepLocalCopy({ files: [ {