2022-05-25 20:26:10 +12:00
< script >
2023-06-14 21:47:46 +12:00
import {
ModalContent,
2023-07-06 00:49:39 +12:00
keepOpen,
2023-06-14 21:47:46 +12:00
Toggle,
Body,
InlineAlert,
2023-06-13 22:21:54 +12:00
Input,
2023-06-14 21:47:46 +12:00
notifications,
} from "@budibase/bbui"
2023-06-13 22:57:56 +12:00
import { createValidationStore } from "helpers/validation/yup"
2022-05-25 20:26:10 +12:00
export let app
2022-09-10 07:06:29 +12:00
export let published
2023-06-13 21:00:01 +12:00
let includeInternalTablesRows = true
let encypt = true
2022-05-25 20:26:10 +12:00
2023-06-13 23:42:30 +12:00
let password = null
2023-06-13 22:57:56 +12:00
const validation = createValidationStore()
2023-06-20 01:53:02 +12:00
validation.addValidatorType("password", "password", true, { minLength : 8 } )
2023-06-13 23:42:30 +12:00
$: validation.observe("password", password)
2022-05-25 20:26:10 +12:00
2023-06-13 22:15:32 +12:00
const Step = { CONFIG : "config" , SET_PASSWORD : "set_password" }
2023-06-13 23:45:57 +12:00
let currentStep = Step.CONFIG
2023-06-13 22:15:32 +12:00
$: exportButtonText = published ? "Export published" : "Export latest"
$: stepConfig = {
[Step.CONFIG]: {
title: published ? "Export published app" : "Export latest app",
confirmText: encypt ? "Continue" : exportButtonText,
onConfirm: () => {
if (!encypt) {
exportApp()
} else {
currentStep = Step.SET_PASSWORD
2023-07-06 00:49:39 +12:00
return keepOpen
2023-06-13 22:15:32 +12:00
}
},
2023-06-13 23:42:30 +12:00
isValid: true,
2023-06-13 22:15:32 +12:00
},
[Step.SET_PASSWORD]: {
title: "Add password to encrypt your export",
confirmText: exportButtonText,
2023-06-13 22:57:56 +12:00
onConfirm: async () => {
2023-06-13 23:42:30 +12:00
await validation.check({ password } )
2023-06-13 22:57:56 +12:00
if (!$validation.valid) {
2023-07-06 00:49:39 +12:00
return keepOpen
2023-06-13 22:21:54 +12:00
}
2023-06-13 23:45:57 +12:00
exportApp(password)
2023-06-13 22:15:32 +12:00
},
2023-06-13 23:42:30 +12:00
isValid: $validation.valid,
2023-06-13 22:15:32 +12:00
},
}
2022-09-10 07:06:29 +12:00
2023-06-14 21:47:46 +12:00
const exportApp = async () => {
2022-09-10 07:06:29 +12:00
const id = published ? app.prodId : app.devId
2023-06-14 22:06:46 +12:00
const url = `/api/backups/export?appId=${ id } `
2023-06-14 22:30:05 +12:00
await downloadFile(url, {
excludeRows: !includeInternalTablesRows,
encryptPassword: password,
})
2023-06-14 21:47:46 +12:00
}
2023-06-14 22:21:47 +12:00
async function downloadFile(url, body) {
2023-06-14 21:47:46 +12:00
try {
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.")
}
} catch (error) {
2023-06-15 00:48:37 +12:00
notifications.error(error.message || "Error downloading the exported app")
2023-06-14 21:47:46 +12:00
}
2022-05-25 20:26:10 +12:00
}
< / script >
2023-06-13 22:15:32 +12:00
< ModalContent
title={ stepConfig [ currentStep ]. title }
confirmText={ stepConfig [ currentStep ]. confirmText }
onConfirm={ stepConfig [ currentStep ]. onConfirm }
2023-06-13 23:42:30 +12:00
disabled={ ! stepConfig [ currentStep ]. isValid }
2023-06-13 22:15:32 +12:00
>
2023-06-13 22:21:54 +12:00
{ #if currentStep === Step . CONFIG }
2023-06-13 22:57:56 +12:00
< Body >
< Toggle
text="Export rows from internal tables"
bind:value={ includeInternalTablesRows }
/>
< Toggle text = "Encrypt my export" bind:value = { encypt } / >
< / Body >
{ #if ! encypt }
< InlineAlert
header="Do not share your budibase application exports publicly as they may contain sensitive information such as database credentials or secret keys."
2023-06-13 22:21:54 +12:00
/>
{ /if }
{ /if }
{ #if currentStep === Step . SET_PASSWORD }
< Input
2023-06-13 22:57:56 +12:00
type="password"
2023-06-13 22:21:54 +12:00
label="Password"
placeholder="Type here..."
bind:value={ password }
2023-06-13 22:57:56 +12:00
error={ $validation . errors . password }
2023-06-13 21:00:01 +12:00
/>
{ /if }
2022-05-25 20:26:10 +12:00
< / ModalContent >