Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
import { Base64 } from "js-base64";
import RNBlobUtil from "react-native-blob-util";

// BEGIN EXTRA CODE
// END EXTRA CODE
Expand All @@ -16,7 +17,7 @@
* @param {MxObject} image
* @returns {Promise.<boolean>}
*/
export function Base64DecodeToImage(base64: string, image: mendix.lib.MxObject): Promise<boolean> {
export async function Base64DecodeToImage(base64: string, image: mendix.lib.MxObject): Promise<boolean> {
// BEGIN USER CODE

if (!base64) {
Expand All @@ -26,6 +27,56 @@
throw new Error("image should not be null");
}

// Native platform
if (navigator && navigator.product === "ReactNative") {
try {
// Remove data URI prefix if present (e.g., "data:image/png;base64,")
let cleanBase64 = base64;
if (base64.includes(",")) {
cleanBase64 = base64.split(",")[1];
}

// Remove any whitespace/newlines
cleanBase64 = cleanBase64.replace(/\s/g, "");

// Validate base64 format
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(cleanBase64)) {
throw new Error("Invalid base64 format");
}

// Create a temporary file path
const tempPath = `${RNBlobUtil.fs.dirs.CacheDir}/temp_image_${Date.now()}.png`;

// Write Base64 data to a temporary file
await RNBlobUtil.fs.writeFile(tempPath, cleanBase64, "base64");

// Fetch the file as a blob
const res = await fetch(`file://${tempPath}`);
const blob = await res.blob();

return new Promise((resolve, reject) => {
mx.data.saveDocument(
image.getGuid(),
"camera image",
{},
blob,
() => {
RNBlobUtil.fs.unlink(tempPath).catch(() => {});

Check failure on line 64 in packages/jsActions/nanoflow-actions-native/src/other/Base64DecodeToImage.ts

View workflow job for this annotation

GitHub Actions / Unit tests

Unexpected empty arrow function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@saurabhchavan1711 The empty callback function here is intended so I suggest we either do a one-line ts check skip here and below. So the pipeline will pass as well.
That should work but If it doesn't, perhaps we can simply do a console.info for general logging within the callback body.

resolve(true);
},
error => {
RNBlobUtil.fs.unlink(tempPath).catch(() => {});

Check failure on line 68 in packages/jsActions/nanoflow-actions-native/src/other/Base64DecodeToImage.ts

View workflow job for this annotation

GitHub Actions / Unit tests

Unexpected empty arrow function
reject(error);
}
);
});
} catch (error) {
console.error("Failed to decode base64 to image:", error);
return false;
}
}

// Other platforms
const blob = new Blob([Base64.toUint8Array(base64)], { type: "image/png" });

return new Promise((resolve, reject) => {
Expand Down
Loading