Skip to content

Commit

Permalink
ComposeMenu: Extract default filename for new image picker.
Browse files Browse the repository at this point in the history
This commit adds a function to extract the filename from an image
uri. This will be useful in the following commit when we switch
to a new image picker library. On android, the filename is currently
not returned by the react-native-image-crop-picker so we edit
chooseUploadImageFilename to be able to infer the filename
from the uri instead.
  • Loading branch information
armaanahluwalia committed Dec 7, 2018
1 parent d688069 commit 766ab54
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/compose/ComposeMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type Props = {
onExpandContract: () => void,
};

/*
* Extract the image name from its uri in case the fileName is empty.
*/
export const getDefaultFilenameFromUri = (uri: string) => uri.replace(/^.*[\\/]/, '');

/**
* Adjust `fileName` to one with the right extension for the file format.
*
Expand All @@ -30,7 +35,10 @@ type Props = {
* actual format. The clue we get in the image-picker response is the extension
* found in `uri`.
*/
export const chooseUploadImageFilename = (uri: string, fileName: string): string => {
export const chooseUploadImageFilename = (uri: string, fileName?: string): string => {
if (typeof fileName !== 'string' || fileName === '') {
fileName = getDefaultFilenameFromUri(uri);
}
/*
* Photos in an iPhone's camera roll (taken since iOS 11) are typically in
* HEIF format and have file names with the extension `.HEIC`. When the user
Expand All @@ -41,7 +49,6 @@ export const chooseUploadImageFilename = (uri: string, fileName: string): string
if (/\.jpe?g$/i.test(uri)) {
return fileName.replace(/\.heic$/i, '.jpeg');
}

return fileName;
};

Expand Down
9 changes: 8 additions & 1 deletion src/compose/__tests__/ComposeMenu-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @flow strict-local */
import { chooseUploadImageFilename } from '../ComposeMenu';
import { chooseUploadImageFilename, getDefaultFilenameFromUri } from '../ComposeMenu';

describe('chooseUploadImageFilename', () => {
test('Does nothing if the image uri does not end with an extension for the JPEG format', () => {
Expand All @@ -17,3 +17,10 @@ describe('chooseUploadImageFilename', () => {
},
);
});

describe('getDefaultFilenameFromUri', () => {
test('Returns extracted file name if fileName is left empty', () => {
expect(getDefaultFilenameFromUri('path/to/fileName.jpg')).toBe('fileName.jpg');
expect(getDefaultFilenameFromUri('path/to/fileName.jpg')).toBe('fileName.jpg');
});
});

0 comments on commit 766ab54

Please sign in to comment.