From 3ffba9faf8bda98bc16e606bf8650f3e88bbc4ed Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 22 Mar 2024 09:49:05 +0100 Subject: [PATCH 01/30] Add download file option --- .../actions/DownloadFile.svelte | 28 +++++++++++++++++++ .../ButtonActionEditor/actions/index.js | 1 + .../controls/ButtonActionEditor/manifest.json | 5 ++++ 3 files changed, 34 insertions(+) create mode 100644 packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/DownloadFile.svelte diff --git a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/DownloadFile.svelte b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/DownloadFile.svelte new file mode 100644 index 0000000000..1c1974155b --- /dev/null +++ b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/DownloadFile.svelte @@ -0,0 +1,28 @@ + + +
+ + (parameters.value = e.detail)} + /> +
+ + diff --git a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/index.js b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/index.js index eb354d6557..587993377d 100644 --- a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/index.js +++ b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/index.js @@ -22,3 +22,4 @@ export { default as PromptUser } from "./PromptUser.svelte" export { default as OpenSidePanel } from "./OpenSidePanel.svelte" export { default as CloseSidePanel } from "./CloseSidePanel.svelte" export { default as ClearRowSelection } from "./ClearRowSelection.svelte" +export { default as DownloadFile } from "./DownloadFile.svelte" diff --git a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/manifest.json b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/manifest.json index 9391baf3dc..6d1794c991 100644 --- a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/manifest.json +++ b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/manifest.json @@ -161,6 +161,11 @@ "name": "Clear Row Selection", "type": "data", "component": "ClearRowSelection" + }, + { + "name": "Download file", + "type": "data", + "component": "DownloadFile" } ] } From 496679f3ebcf3b60b03573e47ec0f76900b12e40 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 22 Mar 2024 10:38:09 +0100 Subject: [PATCH 02/30] Download config --- .../ButtonActionEditor/actions/DownloadFile.svelte | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/DownloadFile.svelte b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/DownloadFile.svelte index 1c1974155b..741b21a768 100644 --- a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/DownloadFile.svelte +++ b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/DownloadFile.svelte @@ -1,17 +1,23 @@
(parameters.value = e.detail)} />
From 349b22ba252ac93450bc549463ce748352a67910 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 22 Mar 2024 11:34:05 +0100 Subject: [PATCH 03/30] Move downloadfile to frontend-core --- .../components/start/ExportAppModal.svelte | 38 ++----------------- packages/frontend-core/src/utils/download.js | 31 +++++++++++++++ 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/packages/builder/src/components/start/ExportAppModal.svelte b/packages/builder/src/components/start/ExportAppModal.svelte index ec0cf42fe0..8cf2025c39 100644 --- a/packages/builder/src/components/start/ExportAppModal.svelte +++ b/packages/builder/src/components/start/ExportAppModal.svelte @@ -8,6 +8,7 @@ Input, notifications, } from "@budibase/bbui" + import { downloadFile } from "@budibase/frontend-core" import { createValidationStore } from "helpers/validation/yup" export let app @@ -55,42 +56,11 @@ const exportApp = async () => { const id = published ? app.prodId : app.devId const url = `/api/backups/export?appId=${id}` - await downloadFile(url, { - excludeRows: !includeInternalTablesRows, - encryptPassword: password, - }) - } - - async function downloadFile(url, body) { try { - const response = await fetch(url, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(body), + await downloadFile(url, { + excludeRows: !includeInternalTablesRows, + encryptPassword: password, }) - - if (response.ok) { - const contentDisposition = response.headers.get("Content-Disposition") - - const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec( - contentDisposition - ) - - const filename = matches[1].replace(/['"]/g, "") - - const url = URL.createObjectURL(await response.blob()) - - const link = document.createElement("a") - link.href = url - link.download = filename - link.click() - - URL.revokeObjectURL(url) - } else { - notifications.error("Error exporting the app.") - } } catch (error) { notifications.error(error.message || "Error downloading the exported app") } diff --git a/packages/frontend-core/src/utils/download.js b/packages/frontend-core/src/utils/download.js index 89c8572253..f3701b357b 100644 --- a/packages/frontend-core/src/utils/download.js +++ b/packages/frontend-core/src/utils/download.js @@ -34,3 +34,34 @@ export async function downloadStream(streamResponse) { URL.revokeObjectURL(blobUrl) } + +export async function downloadFile(url, body) { + const response = await fetch(url, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(body), + }) + + if (response.ok) { + const contentDisposition = response.headers.get("Content-Disposition") + + const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec( + contentDisposition + ) + + const filename = matches[1].replace(/['"]/g, "") + + const url = URL.createObjectURL(await response.blob()) + + const link = document.createElement("a") + link.href = url + link.download = filename + link.click() + + URL.revokeObjectURL(url) + } else { + notifications.error("Error exporting the app.") + } +} From 1a7e845c5648df17bf42e4ae997c213e36f18b70 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 22 Mar 2024 11:40:27 +0100 Subject: [PATCH 04/30] Remove notifications from frontend-core --- .../components/start/ExportAppModal.svelte | 6 ++- packages/frontend-core/src/utils/download.js | 39 ++++++++++--------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/packages/builder/src/components/start/ExportAppModal.svelte b/packages/builder/src/components/start/ExportAppModal.svelte index 8cf2025c39..3a995be7ae 100644 --- a/packages/builder/src/components/start/ExportAppModal.svelte +++ b/packages/builder/src/components/start/ExportAppModal.svelte @@ -56,11 +56,15 @@ const exportApp = async () => { const id = published ? app.prodId : app.devId const url = `/api/backups/export?appId=${id}` + try { - await downloadFile(url, { + const downloaded = await downloadFile(url, { excludeRows: !includeInternalTablesRows, encryptPassword: password, }) + if (!downloaded) { + notifications.error("Error exporting the app.") + } } catch (error) { notifications.error(error.message || "Error downloading the exported app") } diff --git a/packages/frontend-core/src/utils/download.js b/packages/frontend-core/src/utils/download.js index f3701b357b..b887122969 100644 --- a/packages/frontend-core/src/utils/download.js +++ b/packages/frontend-core/src/utils/download.js @@ -44,24 +44,25 @@ export async function downloadFile(url, body) { body: JSON.stringify(body), }) - if (response.ok) { - const contentDisposition = response.headers.get("Content-Disposition") - - const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec( - contentDisposition - ) - - const filename = matches[1].replace(/['"]/g, "") - - const url = URL.createObjectURL(await response.blob()) - - const link = document.createElement("a") - link.href = url - link.download = filename - link.click() - - URL.revokeObjectURL(url) - } else { - notifications.error("Error exporting the app.") + if (!response.ok) { + return false } + + const contentDisposition = response.headers.get("Content-Disposition") + + const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec( + contentDisposition + ) + + const filename = matches[1].replace(/['"]/g, "") + + const url = URL.createObjectURL(await response.blob()) + + const link = document.createElement("a") + link.href = url + link.download = filename + link.click() + + URL.revokeObjectURL(url) + return true } From 19caf3cddfb6e93890f52aad5ace602c542ad754 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 22 Mar 2024 11:44:32 +0100 Subject: [PATCH 05/30] Copy --- .../controls/ButtonActionEditor/manifest.json | 2 +- packages/client/src/utils/buttonActions.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/manifest.json b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/manifest.json index 6d1794c991..2840a0d662 100644 --- a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/manifest.json +++ b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/manifest.json @@ -163,7 +163,7 @@ "component": "ClearRowSelection" }, { - "name": "Download file", + "name": "Download File", "type": "data", "component": "DownloadFile" } diff --git a/packages/client/src/utils/buttonActions.js b/packages/client/src/utils/buttonActions.js index 68478b76ac..a4471ac4aa 100644 --- a/packages/client/src/utils/buttonActions.js +++ b/packages/client/src/utils/buttonActions.js @@ -400,6 +400,23 @@ const closeSidePanelHandler = () => { sidePanelStore.actions.close() } +const downloadFileHandler = (action, context) => { + download(action.parameters.value, `file.jpg`) + // const x = processStringSync(action.parameters.value, context) + // console.warn(x) + + // // Built total context for this action + // const totalContext = { + // ...context, + // state: get(stateStore), + // actions: buttonContext, + // } + + // action = enrichDataBindings(action, totalContext) + + // console.error(action) +} + const handlerMap = { ["Fetch Row"]: fetchRowHandler, ["Save Row"]: saveRowHandler, @@ -418,6 +435,7 @@ const handlerMap = { ["Prompt User"]: promptUserHandler, ["Open Side Panel"]: openSidePanelHandler, ["Close Side Panel"]: closeSidePanelHandler, + ["Download File"]: downloadFileHandler, } const confirmTextMap = { From df05cf23454c3ce1635d44087bb414923d2a1fb5 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 25 Mar 2024 10:24:20 +0100 Subject: [PATCH 06/30] Lint --- packages/frontend-core/src/utils/download.js | 36 ++++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/frontend-core/src/utils/download.js b/packages/frontend-core/src/utils/download.js index b887122969..55ba899ee6 100644 --- a/packages/frontend-core/src/utils/download.js +++ b/packages/frontend-core/src/utils/download.js @@ -46,23 +46,23 @@ export async function downloadFile(url, body) { if (!response.ok) { return false + } else { + const contentDisposition = response.headers.get("Content-Disposition") + + const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec( + contentDisposition + ) + + const filename = matches[1].replace(/['"]/g, "") + + const url = URL.createObjectURL(await response.blob()) + + const link = document.createElement("a") + link.href = url + link.download = filename + link.click() + + URL.revokeObjectURL(url) + return true } - - const contentDisposition = response.headers.get("Content-Disposition") - - const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec( - contentDisposition - ) - - const filename = matches[1].replace(/['"]/g, "") - - const url = URL.createObjectURL(await response.blob()) - - const link = document.createElement("a") - link.href = url - link.download = filename - link.click() - - URL.revokeObjectURL(url) - return true } From 12f9b47954e987aa8acbc549a78fe56a3fc19949 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 25 Mar 2024 12:29:38 +0100 Subject: [PATCH 07/30] Implement download by url --- .../actions/DownloadFile.svelte | 74 +++++++++++++++++-- packages/client/src/utils/buttonActions.js | 26 ++++--- 2 files changed, 80 insertions(+), 20 deletions(-) diff --git a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/DownloadFile.svelte b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/DownloadFile.svelte index 741b21a768..702f0cc911 100644 --- a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/DownloadFile.svelte +++ b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/DownloadFile.svelte @@ -1,7 +1,9 @@
- (parameters.value = e.detail)} + table.label} + getOptionValue={table => table.resourceId} + /> + +