prevent corruption of downloaded file when base64 string is used in download action (#10254)
This commit is contained in:
parent
c127f98cd3
commit
dde68ce753
|
|
@ -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({
|
||||
|
|
|
|||
11
app/client/src/sagas/ActionExecution/downloadActionUtils.ts
Normal file
11
app/client/src/sagas/ActionExecution/downloadActionUtils.ts
Normal 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);
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user