Move downloadfile to frontend-core
This commit is contained in:
parent
496679f3eb
commit
349b22ba25
|
@ -8,6 +8,7 @@
|
||||||
Input,
|
Input,
|
||||||
notifications,
|
notifications,
|
||||||
} from "@budibase/bbui"
|
} from "@budibase/bbui"
|
||||||
|
import { downloadFile } from "@budibase/frontend-core"
|
||||||
import { createValidationStore } from "helpers/validation/yup"
|
import { createValidationStore } from "helpers/validation/yup"
|
||||||
|
|
||||||
export let app
|
export let app
|
||||||
|
@ -55,42 +56,11 @@
|
||||||
const exportApp = async () => {
|
const exportApp = async () => {
|
||||||
const id = published ? app.prodId : app.devId
|
const id = published ? app.prodId : app.devId
|
||||||
const url = `/api/backups/export?appId=${id}`
|
const url = `/api/backups/export?appId=${id}`
|
||||||
await downloadFile(url, {
|
|
||||||
excludeRows: !includeInternalTablesRows,
|
|
||||||
encryptPassword: password,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async function downloadFile(url, body) {
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(url, {
|
await downloadFile(url, {
|
||||||
method: "POST",
|
excludeRows: !includeInternalTablesRows,
|
||||||
headers: {
|
encryptPassword: password,
|
||||||
"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.")
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
notifications.error(error.message || "Error downloading the exported app")
|
notifications.error(error.message || "Error downloading the exported app")
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,3 +34,34 @@ export async function downloadStream(streamResponse) {
|
||||||
|
|
||||||
URL.revokeObjectURL(blobUrl)
|
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.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue