6
6
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
7
7
// Other code you write will be lost the next time you deploy the project.
8
8
import { Platform } from "react-native" ;
9
- import RNBlobUtil , { Mediatype } from "react-native-blob-util" ;
9
+ import RNBlobUtil from "react-native-blob-util" ;
10
10
import FileViewer from "react-native-file-viewer" ;
11
11
import mimeTypes from "mime" ;
12
12
@@ -15,12 +15,12 @@ function formatPath(...pathArgs: string[]): string {
15
15
return pathArgs . filter ( arg => ! ! arg ) . join ( "/" ) ;
16
16
}
17
17
18
- function sanitizeFileName ( name : string ) {
18
+ function sanitizeFileName ( name : string ) : string {
19
19
/* eslint-disable-next-line no-control-regex */
20
20
return name . replace ( / [ < > " ? : | * / \\ \u0000 - \u001F \u007F ] / g, "_" ) ;
21
21
}
22
22
23
- async function getUniqueFilePath ( path : string , fileName : string ) {
23
+ async function getUniqueFilePath ( path : string , fileName : string ) : Promise < string > {
24
24
const insertionIndex = fileName . lastIndexOf ( "." ) ;
25
25
const fileNameWithoutExtension = fileName . substring ( 0 , insertionIndex ) ;
26
26
const extension = fileName . substring ( insertionIndex ) ;
@@ -35,6 +35,22 @@ async function getUniqueFilePath(path: string, fileName: string) {
35
35
return uniqueFilePath ;
36
36
}
37
37
38
+ function createCopyToMediaStoreFunction (
39
+ fileName : string ,
40
+ mimeType : string | null
41
+ ) : ( filePath : string ) => Promise < string > {
42
+ return ( filePath : string ) =>
43
+ RNBlobUtil . MediaCollection . copyToMediaStore (
44
+ {
45
+ name : fileName ,
46
+ mimeType : mimeType ?? "*" ,
47
+ parentFolder : ""
48
+ } ,
49
+ "Download" ,
50
+ filePath
51
+ ) ;
52
+ }
53
+
38
54
// END EXTRA CODE
39
55
40
56
/**
@@ -60,22 +76,19 @@ export async function DownloadFile(file: mendix.lib.MxObject, openWithOS: boolea
60
76
accessiblePath = await getUniqueFilePath ( baseDir , sanitizedFileName ) ;
61
77
await RNBlobUtil . fs . cp ( filePath , accessiblePath ) ;
62
78
} else {
63
- const tempPath = await getUniqueFilePath ( baseDir , sanitizedFileName ) ;
64
- const base64Data = await mx . readFileBlob ( filePath ) ;
65
- const base64Content = base64Data ?. split ( "," ) [ 1 ] ;
66
- await RNBlobUtil . fs . createFile ( tempPath , base64Content , "base64" ) ;
67
- const mimeType = mimeTypes . getType ( fileName ) as Mediatype ;
68
- accessiblePath = await RNBlobUtil . MediaCollection . copyToMediaStore (
69
- {
70
- name : sanitizedFileName ,
71
- mimeType : mimeType ?? "*" ,
72
- parentFolder : ""
73
- } ,
74
- "Download" ,
75
- tempPath
76
- ) ;
77
-
78
- RNBlobUtil . fs . unlink ( tempPath ) ;
79
+ const mimeType = mimeTypes . getType ( fileName ) ;
80
+ const copyToMediaStore = createCopyToMediaStoreFunction ( sanitizedFileName , mimeType ) ;
81
+ if ( typeof mx . readFileBlob === "function" ) {
82
+ const tempPath = await getUniqueFilePath ( baseDir , sanitizedFileName ) ;
83
+ const base64Data = await mx . readFileBlob ( filePath ) ;
84
+ const base64Content = base64Data ?. split ( "," ) [ 1 ] ;
85
+ await RNBlobUtil . fs . createFile ( tempPath , base64Content , "base64" ) ;
86
+ accessiblePath = await copyToMediaStore ( tempPath ) ;
87
+ RNBlobUtil . fs . unlink ( tempPath ) ;
88
+ } else {
89
+ // this code block is for backward compatibility
90
+ accessiblePath = await copyToMediaStore ( filePath ) ;
91
+ }
79
92
}
80
93
if ( openWithOS ) {
81
94
await FileViewer . open ( accessiblePath , {
0 commit comments