Skip to content

Commit 5a7c7d3

Browse files
committed
chore: change to trigger the pipeline testing JS action trigger
1 parent 5750a94 commit 5a7c7d3

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

packages/jsActions/nanoflow-actions-native/src/other/Base64DecodeToImage.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +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 { Base64 } from "js-base64";
9+
import RNBlobUtil from "react-native-blob-util";
910

1011
// BEGIN EXTRA CODE
1112
// END EXTRA CODE
@@ -16,7 +17,7 @@ import { Base64 } from "js-base64";
1617
* @param {MxObject} image
1718
* @returns {Promise.<boolean>}
1819
*/
19-
export function Base64DecodeToImage(base64: string, image: mendix.lib.MxObject): Promise<boolean> {
20+
export async function Base64DecodeToImage(base64: string, image: mendix.lib.MxObject): Promise<boolean> {
2021
// BEGIN USER CODE
2122

2223
if (!base64) {
@@ -26,8 +27,57 @@ export function Base64DecodeToImage(base64: string, image: mendix.lib.MxObject):
2627
throw new Error("image should not be null");
2728
}
2829

29-
const blob = new Blob([Base64.toUint8Array(base64)], { type: "image/png" });
30+
// Native platform
31+
if (navigator && navigator.product === "ReactNative") {
32+
try {
33+
// Remove data URI prefix if present (e.g., "data:image/png;base64,")
34+
let cleanBase64 = base64;
35+
if (base64.includes(",")) {
36+
cleanBase64 = base64.split(",")[1];
37+
}
38+
39+
// Remove any whitespace/newlines
40+
cleanBase64 = cleanBase64.replace(/\s/g, "");
41+
42+
// Validate base64 format
43+
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(cleanBase64)) {
44+
throw new Error("Invalid base64 format");
45+
}
46+
47+
// Create a temporary file path
48+
const tempPath = `${RNBlobUtil.fs.dirs.CacheDir}/temp_image_${Date.now()}.png`;
49+
50+
// Write Base64 data to a temporary file
51+
await RNBlobUtil.fs.writeFile(tempPath, cleanBase64, "base64");
3052

53+
// Fetch the file as a blob
54+
const res = await fetch(`file://${tempPath}`);
55+
const blob = await res.blob();
56+
57+
return new Promise((resolve, reject) => {
58+
mx.data.saveDocument(
59+
image.getGuid(),
60+
"camera image",
61+
{},
62+
blob,
63+
() => {
64+
RNBlobUtil.fs.unlink(tempPath).catch(() => {});
65+
resolve(true);
66+
},
67+
error => {
68+
RNBlobUtil.fs.unlink(tempPath).catch(() => {});
69+
reject(error);
70+
}
71+
);
72+
});
73+
} catch (error) {
74+
console.error("Failed to decode base64 to image:", error);
75+
return false;
76+
}
77+
}
78+
79+
// Other platforms
80+
const blob = new Blob([Base64.toUint8Array(base64)], { type: "image/png" });
3181
return new Promise((resolve, reject) => {
3282
mx.data.saveDocument(image.getGuid(), "camera image", {}, blob, () => resolve(true), reject);
3383
});

0 commit comments

Comments
 (0)