11import 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' ;
33import localFileCreate from '@libs/localFileCreate' ;
44import 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 } )
0 commit comments