Import und Export der Datenbank

This commit is contained in:
Anton Schegg 2024-10-26 20:31:07 +02:00
parent c657b36292
commit 903c2f7560
2 changed files with 57 additions and 38 deletions

View file

@ -12,24 +12,37 @@ export const useDatabaseStore = defineStore("database", {
}; };
}, },
actions: { actions: {
async import() { async import(noEncryption: boolean, secret: string, requestData: any) {
this.status = "working";
http
.post("/admin/database")
.then((result) => {
this.data = result.data;
this.status = "success";
})
.catch(() => {
this.status = "failed";
});
},
async export(secret: string) {
try { try {
this.data = {};
this.status = "working"; this.status = "working";
const postData = {secret: secret}; this.failureReason = '';
const response: AxiosResponse = await http.post("/admin/database", postData); this.ex = {};
this.data = response.data.data; const requestHeaders = {headers: {'Content-Type': 'application/json', 'x-decrypt-no' : noEncryption}};
if (!noEncryption) {
requestHeaders.headers['x-decrypt-with'] = secret;
}
await http.post("/admin/database", requestData, requestHeaders);
this.status = "success";
} catch (ex) {
this.status = "failed";
this.failureReason = (ex as Error).message;
this.ex = (ex as Error);
throw ex;
}
},
async export(noEncryption: boolean, secret: string) {
try {
this.data = {};
this.status = "working";
this.failureReason = '';
this.ex = {};
const requestHeaders = {headers: {'Content-Type': 'application/json', 'x-encrypt-no' : noEncryption}};
if (!noEncryption) {
requestHeaders.headers['x-encrypt-with'] = secret;
}
const response: AxiosResponse = await http.get("/admin/database", requestHeaders);
this.data = response.data;
this.status = "success"; this.status = "success";
} catch (ex) { } catch (ex) {
this.status = "failed"; this.status = "failed";

View file

@ -6,25 +6,28 @@
</div> </div>
</template> </template>
<template #main> <template #main>
<Spinner v-if="status == 'working'" class="mx-auto" />
<p v-else-if="status == 'failed'">Fehler</p>
<div class="flex flex-row justify-start items-center gap-2"> <div class="flex flex-row justify-start items-center gap-2">
<label for="file" class="w-1/4">Zum Importieren Datei auswählen</label> <label for="file" class="w-2/6">Zum Importieren Datei auswählen</label>
<input type="file" class="grow" id="file" @change="addFile"/> <input type="file" class="" id="file" @change="addFile"/>
</div> </div>
<div class="flex flex-row justify-start items-center gap-2"> <div class="flex flex-row justify-start items-center gap-2">
<label for="encKey" class="w-1/4 text-nowrap">Encryption Key</label> <label for="noEnc" class="w-2/6">Ohne Verschlüsselung</label>
<input type="password" id="encKey" required v-model="secret" /> <input type="checkbox" class="w-fit" id="noEnc" required v-model="noEncryption" />
</div>
<div class="flex flex-row justify-start items-center gap-2">
<label for="encKey" class="w-2/6">Encryption Key</label>
<input :disabled="noEncryption" type="password" id="encKey" required v-model="secret" />
</div> </div>
<div class="flex flex-row justify-end gap-2"> <div class="flex flex-row justify-end gap-2">
<button primary :disabled="uploadDisabled" class="!w-fit" @click="importDatabase">Import</button> <span v-if="status == 'failed'">Fehler</span>
<button :disabled="disabled" primary class="!w-fit" @click="exportDatabase">Exportieren</button>
</div>
<div class="flex flex-row justify-start gap-2">
<a class="download-link" v-if="dataUrl !== ''" :href="dataUrl" :download="downloadFilename">Download {{data.length}} Bytes</a>
<Spinner v-if="status == 'working'" class="my-auto" /> <Spinner v-if="status == 'working'" class="my-auto" />
<SuccessCheckmark v-else-if="status == 'success'" /> <SuccessCheckmark v-else-if="status == 'success'" />
<FailureXMark v-else-if="status == 'failed'" /> <FailureXMark v-else-if="status == 'failed'" />
<button primary :disabled="uploadDisabled" class="!w-fit" @click="importDatabase">Import</button>
<button :disabled="disabled" primary class="!w-fit" @click="exportDatabase">Exportieren</button>
</div>
<div class="flex flex-row justify-end gap-2">
<a class="download-link" v-if="dataUrl !== ''" :href="dataUrl" :download="downloadFilename">Hier für Download ({{dataUrl.length}} Bytes) klicken</a>
</div> </div>
</template> </template>
</MainTemplate> </MainTemplate>
@ -46,7 +49,7 @@ export default defineComponent({
data() { data() {
return { return {
dataUrl: '' as string, dataUrl: '' as string,
dataImport: '' as string, noEncryption: false as boolean,
file: null, file: null,
downloadFilename: '' as string, downloadFilename: '' as string,
secret: '' as string, secret: '' as string,
@ -55,40 +58,43 @@ export default defineComponent({
computed: { computed: {
...mapState(useDatabaseStore, ["data", "status", "failureReason"]), ...mapState(useDatabaseStore, ["data", "status", "failureReason"]),
disabled() : boolean { disabled() : boolean {
return this.status === 'working' || this.secret === ''; return this.status === 'working' || (this.secret === '' && !this.noEncryption);
}, },
uploadDisabled() : boolean { uploadDisabled() : boolean {
return this.status === 'working' || this.secret === '' || this.file === null; return this.status === 'working' || (this.secret === '' && !this.noEncryption) || this.file === null;
} }
}, },
mounted() { mounted() {
this.secret = '' as string; this.secret = '' as string;
this.noEncryption = false;
}, },
methods: { methods: {
...mapActions(useDatabaseStore, ["export", "import"]), ...mapActions(useDatabaseStore, ["export", "import"]),
addFile(e) { addFile(e) {
this.dataUrl = '';
if (e.target.files?.length !== 1) {
return;
}
this.file = e.target.files[0]; this.file = e.target.files[0];
console.log(this.file);
}, },
async exportDatabase() { async exportDatabase() {
try { try {
if (this.dataUrl) { if (this.dataUrl) {
this.dataUrl = ''; this.dataUrl = '';
} }
await this.export(this.secret); await this.export(this.noEncryption, this.secret);
this.dataUrl = 'data:application/octet-stream,' + this.data; this.dataUrl = 'data:application/octet-stream,' + encodeURIComponent(JSON.stringify(this.data));
this.downloadFilename = `${new Date().toISOString().replace(/:/g, '-')}_database_export.enc`; this.downloadFilename = `${new Date().toISOString().replace(/:/g, '-')}_database_export.${this.noEncryption ? 'json' : 'enc'}`;
} }
catch(ex) { catch(ex) {
console.log(ex); console.log(ex);
} }
},
onChangeFileUpload() {
}, },
async importDatabase() { async importDatabase() {
try { try {
await this.import(this.secret, this.dataImport); const fileContent = await this.file.text();
await this.import(this.noEncryption, this.secret, fileContent);
} }
catch(ex) { catch(ex) {
console.log(ex); console.log(ex);