Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/components/FilePicker/index.native.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import useLocalize from '@hooks/useLocalize';

import {cleanFileName} from '@libs/fileDownload/FileUtils';
import {cleanFileName, getFileNameWithFallback} from '@libs/fileDownload/FileUtils';
import fileURIToPath from '@libs/fileURIToPath';

import type {FileObject} from '@src/types/utils/Attachment';
Expand All @@ -13,20 +13,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<FileObject> => {
const fileName = fileData.name ?? 'spreadsheet';
const fileResult: FileObject = {
name: cleanFileName(fileName),
name: fileData.name,
type: fileData.type ?? undefined,
uri: fileData.uri,
size: fileData.size,
Expand Down Expand Up @@ -91,11 +93,13 @@ function FilePicker({children}: FilePickerProps) {
type: [types.allFiles],
});

const fileName = getFileNameWithFallback(file.name, file.uri, DEFAULT_FILE_NAME);

const [localCopy] = await keepLocalCopy({
files: [
{
uri: file.uri,
fileName: file.name ?? 'spreadsheet',
fileName,
},
],
destination: 'cachesDirectory',
Expand All @@ -106,7 +110,7 @@ function FilePicker({children}: FilePickerProps) {
}

return {
name: cleanFileName(file.name ?? 'spreadsheet'),
name: cleanFileName(fileName),
type: file.type,
uri: localCopy.localUri,
size: file.size,
Expand Down
18 changes: 18 additions & 0 deletions src/libs/fileDownload/FileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,23 @@ const splitExtensionFromFileName: SplitExtensionFromFileName = (fullFileName) =>
return {fileName: splitFileName.join('.'), fileExtension: fileExtension ?? ''};
};

/**
* 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.
*/
Comment on lines +162 to +165

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MelvinBot minify jsdoc. Too long

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cut it from 7 lines to 2 — kept only why the fallback exists and when the default is used, and dropped the walkthrough of the downstream extension checks. Pushed in 57d060a.

src/libs/fileDownload/FileUtils.ts:162-165:

/**
 * 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.
 */

ESLint on the file reports only the 4 pre-existing no-unsafe-type-assertion errors at lines 595, 705, 732 and 934 — none in the changed region, and unchanged from before this commit.


view run

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.
Expand Down Expand Up @@ -946,6 +963,7 @@ export {
splitExtensionFromFileName,
getMimeType,
getFileName,
getFileNameWithFallback,
getFileType,
cleanFileName,
getExportFileName,
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/FileUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
appendTimeToFileName,
canvasFallback,
getExportFileName,
getFileNameWithFallback,
getFileValidationErrorText,
getImageDimensionsAfterResize,
isHighResolutionImage,
Expand Down Expand Up @@ -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');
Expand Down
Loading