Skip to content

Commit 7f04e3a

Browse files
committed
Change 2FA recovery code filename, and only append time to that file if saving fails on android
1 parent 78b95f5 commit 7f04e3a

8 files changed

Lines changed: 27 additions & 23 deletions

File tree

src/libs/localFileCreate/index.native.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import type LocalFileCreate from './types';
88
* @param textContent content of the file
99
* @returns path, filename and size of the newly created file
1010
*/
11-
const localFileCreate: LocalFileCreate = (fileName, textContent) => {
11+
const localFileCreate: LocalFileCreate = (fileName, textContent, appendTimestamp = true) => {
1212
const {fileExtension} = splitExtensionFromFileName(fileName);
1313
const fileNameWithExtension = fileExtension ? fileName : `${fileName}.txt`;
14-
const newFileName = appendTimeToFileName(fileNameWithExtension);
14+
const newFileName = appendTimestamp ? appendTimeToFileName(fileNameWithExtension) : fileNameWithExtension;
1515
const dir = RNFetchBlob.fs.dirs.DocumentDir;
1616
const path = `${dir}/${newFileName}`;
1717

src/libs/localFileCreate/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import type LocalFileCreate from './types';
77
* @param textContent content of the file
88
* @returns path, filename and size of the newly created file
99
*/
10-
const localFileCreate: LocalFileCreate = (fileName, textContent) => {
10+
const localFileCreate: LocalFileCreate = (fileName, textContent, appendTimestamp = true) => {
1111
const {fileExtension} = splitExtensionFromFileName(fileName);
1212
const fileNameWithExtension = fileExtension ? fileName : `${fileName}.txt`;
13-
const newFileName = appendTimeToFileName(fileNameWithExtension);
13+
const newFileName = appendTimestamp ? appendTimeToFileName(fileNameWithExtension) : fileNameWithExtension;
1414
const blob = new Blob([textContent], {type: getMimeType(fileExtension)});
1515
const url = URL.createObjectURL(blob);
1616

src/libs/localFileCreate/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
type LocalFileCreate = (fileName: string, textContent: string) => Promise<{path: string; newFileName: string; size: number}>;
1+
type LocalFileCreate = (fileName: string, textContent: string, appendTimestamp?: boolean) => Promise<{path: string; newFileName: string; size: number}>;
22

33
export default LocalFileCreate;

src/libs/localFileDownload/index.android.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import RNFetchBlob from 'react-native-blob-util';
2-
import {getMimeType, showGeneralErrorAlert, showSuccessAlert, splitExtensionFromFileName} from '@libs/fileDownload/FileUtils';
2+
import {appendTimeToFileName, getMimeType, showGeneralErrorAlert, showSuccessAlert, splitExtensionFromFileName} from '@libs/fileDownload/FileUtils';
33
import localFileCreate from '@libs/localFileCreate';
44
import type LocalFileDownload from './types';
55

@@ -8,18 +8,22 @@ import type LocalFileDownload from './types';
88
* and textContent, so we're able to copy it to the Android public download dir.
99
* After the file is copied, it is removed from the internal dir.
1010
*/
11-
const localFileDownload: LocalFileDownload = (fileName, textContent, translate, successMessage) => {
12-
localFileCreate(fileName, textContent).then(({path, newFileName}) => {
11+
const localFileDownload: LocalFileDownload = (fileName, textContent, translate, successMessage, shouldShowSuccessAlert, appendTimestamp = true) => {
12+
localFileCreate(fileName, textContent, appendTimestamp).then(({path, newFileName}) => {
1313
const {fileExtension} = splitExtensionFromFileName(newFileName);
14-
RNFetchBlob.MediaCollection.copyToMediaStore(
15-
{
16-
name: newFileName,
17-
parentFolder: '', // subdirectory in the Media Store, empty goes to 'Downloads'
18-
mimeType: getMimeType(fileExtension),
19-
},
20-
'Download',
21-
path,
22-
)
14+
const mimeType = getMimeType(fileExtension);
15+
const tryMediaStore = (name: string) =>
16+
RNFetchBlob.MediaCollection.copyToMediaStore(
17+
{
18+
name,
19+
parentFolder: '', // subdirectory in the Media Store, empty goes to 'Downloads'
20+
mimeType,
21+
},
22+
'Download',
23+
path,
24+
);
25+
tryMediaStore(newFileName)
26+
.catch(() => tryMediaStore(appendTimeToFileName(newFileName)))
2327
.then(() => {
2428
showSuccessAlert(translate, successMessage);
2529
})

src/libs/localFileDownload/index.ios.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import type LocalFileDownload from './types';
99
* and textContent, so we're able to share it using iOS' share API.
1010
* After the file is shared, it is removed from the internal dir.
1111
*/
12-
const localFileDownload: LocalFileDownload = (fileName, textContent, translate, successMessage, shouldShowSuccessAlert) => {
13-
localFileCreate(fileName, textContent).then(({path, newFileName}) => {
12+
const localFileDownload: LocalFileDownload = (fileName, textContent, translate, successMessage, shouldShowSuccessAlert, appendTimestamp = true) => {
13+
localFileCreate(fileName, textContent, appendTimestamp).then(({path, newFileName}) => {
1414
Share.share({url: path, title: newFileName}).finally(() => {
1515
RNFetchBlob.fs.unlink(path);
1616
if (shouldShowSuccessAlert) {

src/libs/localFileDownload/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import type LocalFileDownload from './types';
77
* creates a temporary anchor, just to programmatically click it, so the file
88
* is downloaded by the browser.
99
*/
10-
const localFileDownload: LocalFileDownload = (fileName, textContent, translate, successMessage, shouldShowSuccessAlert) => {
11-
localFileCreate(fileName, textContent).then(({path, newFileName}) => {
10+
const localFileDownload: LocalFileDownload = (fileName, textContent, translate, successMessage, shouldShowSuccessAlert, appendTimestamp = true) => {
11+
localFileCreate(fileName, textContent, appendTimestamp).then(({path, newFileName}) => {
1212
const link = document.createElement('a');
1313
link.download = newFileName;
1414
link.href = path;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {LocalizedTranslate} from '@components/LocaleContextProvider';
22

3-
type LocalFileDownload = (fileName: string, textContent: string, translate: LocalizedTranslate, successMessage?: string, shouldShowSuccessAlert?: boolean) => void;
3+
type LocalFileDownload = (fileName: string, textContent: string, translate: LocalizedTranslate, successMessage?: string, shouldShowSuccessAlert?: boolean, appendTimestamp?: boolean) => void;
44

55
export default LocalFileDownload;

src/pages/settings/Security/TwoFactorAuth/CopyCodesPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function CopyCodesPage({route}: TwoFactorAuthPageProps) {
147147
text={translate('common.download')}
148148
icon={icons.Download}
149149
onPress={() => {
150-
localFileDownload('two-factor-auth-codes', account?.recoveryCodes ?? '', translate);
150+
localFileDownload('DO-NOT-DELETE_Expensify-2FA-RecoveryCodes.txt', account?.recoveryCodes ?? '', translate, undefined, undefined, false);
151151
setError('');
152152
setCodesAreCopied();
153153
announceStatus(translate('fileDownload.success.title'));

0 commit comments

Comments
 (0)