Skip to content

Commit 0cde72b

Browse files
[MOO-1317]: add downloaded file encryption fix backward compatibility (#129)
2 parents f05c748 + dd64ba9 commit 0cde72b

File tree

1 file changed

+32
-19
lines changed
  • packages/jsActions/mobile-resources-native/src/file-download

1 file changed

+32
-19
lines changed

packages/jsActions/mobile-resources-native/src/file-download/DownloadFile.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
77
// Other code you write will be lost the next time you deploy the project.
88
import { Platform } from "react-native";
9-
import RNBlobUtil, { Mediatype } from "react-native-blob-util";
9+
import RNBlobUtil from "react-native-blob-util";
1010
import FileViewer from "react-native-file-viewer";
1111
import mimeTypes from "mime";
1212

@@ -15,12 +15,12 @@ function formatPath(...pathArgs: string[]): string {
1515
return pathArgs.filter(arg => !!arg).join("/");
1616
}
1717

18-
function sanitizeFileName(name: string) {
18+
function sanitizeFileName(name: string): string {
1919
/* eslint-disable-next-line no-control-regex */
2020
return name.replace(/[<>"?:|*/\\\u0000-\u001F\u007F]/g, "_");
2121
}
2222

23-
async function getUniqueFilePath(path: string, fileName: string) {
23+
async function getUniqueFilePath(path: string, fileName: string): Promise<string> {
2424
const insertionIndex = fileName.lastIndexOf(".");
2525
const fileNameWithoutExtension = fileName.substring(0, insertionIndex);
2626
const extension = fileName.substring(insertionIndex);
@@ -35,6 +35,22 @@ async function getUniqueFilePath(path: string, fileName: string) {
3535
return uniqueFilePath;
3636
}
3737

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+
3854
// END EXTRA CODE
3955

4056
/**
@@ -60,22 +76,19 @@ export async function DownloadFile(file: mendix.lib.MxObject, openWithOS: boolea
6076
accessiblePath = await getUniqueFilePath(baseDir, sanitizedFileName);
6177
await RNBlobUtil.fs.cp(filePath, accessiblePath);
6278
} 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+
}
7992
}
8093
if (openWithOS) {
8194
await FileViewer.open(accessiblePath, {

0 commit comments

Comments
 (0)