prevent corruption of downloaded file when base64 string is used in download action (#10254)

This commit is contained in:
Favour Ohanekwu 2022-01-08 23:15:09 -08:00 committed by GitHub
parent c127f98cd3
commit dde68ce753
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import { getType, isURL, Types } from "utils/TypeHelpers";
import { getType, Types } from "utils/TypeHelpers";
import downloadjs from "downloadjs";
import AppsmithConsole from "utils/AppsmithConsole";
import Axios from "axios";
@ -7,6 +7,7 @@ import {
DownloadActionDescription,
} from "entities/DataTree/actionTriggers";
import { ActionValidationError } from "sagas/ActionExecution/errorUtils";
import { isBase64String, isUrlString } from "./downloadActionUtils";
export default async function downloadSaga(
action: DownloadActionDescription["payload"],
@ -27,7 +28,7 @@ export default async function downloadSaga(
AppsmithConsole.info({
text: `download('${jsonString}', '${name}', '${type}') was triggered`,
});
} else if (dataType === Types.STRING && isURL(data)) {
} else if (isUrlString(data)) {
// In the event that a url string is supplied, we need to fetch the image with the response type arraybuffer.
// This also covers the case where the file to be downloaded is Binary.
Axios.get(data, { responseType: "arraybuffer" }).then((res) => {
@ -36,6 +37,15 @@ export default async function downloadSaga(
text: `download('${data}', '${name}', '${type}') was triggered`,
});
});
} else if (isBase64String(data)) {
Axios.get(`data:${type};base64,${data}`, {
responseType: "arraybuffer",
}).then((res) => {
downloadjs(res.data, name, type);
AppsmithConsole.info({
text: `download('${data}', '${name}', '${type}') was triggered`,
});
});
} else {
downloadjs(data, name, type);
AppsmithConsole.info({

View File

@ -0,0 +1,11 @@
import { getType, isURL, Types } from "utils/TypeHelpers";
const BASE64_STRING_REGEX = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$/;
export const isBase64String = (data: any) => {
return getType(data) === Types.STRING && BASE64_STRING_REGEX.test(data);
};
export const isUrlString = (data: any) => {
return getType(data) === Types.STRING && isURL(data);
};