diff --git a/src/stores/admin/database.ts b/src/stores/admin/database.ts index c6a5d54..24b49b9 100644 --- a/src/stores/admin/database.ts +++ b/src/stores/admin/database.ts @@ -12,24 +12,37 @@ export const useDatabaseStore = defineStore("database", { }; }, actions: { - async import() { - this.status = "working"; - http - .post("/admin/database") - .then((result) => { - this.data = result.data; - this.status = "success"; - }) - .catch(() => { - this.status = "failed"; - }); - }, - async export(secret: string) { + async import(noEncryption: boolean, secret: string, requestData: any) { try { + this.data = {}; this.status = "working"; - const postData = {secret: secret}; - const response: AxiosResponse = await http.post("/admin/database", postData); - this.data = response.data.data; + this.failureReason = ''; + this.ex = {}; + 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"; } catch (ex) { this.status = "failed"; diff --git a/src/views/admin/settings/Database.vue b/src/views/admin/settings/Database.vue index 3933984..0a1e701 100644 --- a/src/views/admin/settings/Database.vue +++ b/src/views/admin/settings/Database.vue @@ -6,25 +6,28 @@ @@ -46,7 +49,7 @@ export default defineComponent({ data() { return { dataUrl: '' as string, - dataImport: '' as string, + noEncryption: false as boolean, file: null, downloadFilename: '' as string, secret: '' as string, @@ -55,40 +58,43 @@ export default defineComponent({ computed: { ...mapState(useDatabaseStore, ["data", "status", "failureReason"]), disabled() : boolean { - return this.status === 'working' || this.secret === ''; + return this.status === 'working' || (this.secret === '' && !this.noEncryption); }, uploadDisabled() : boolean { - return this.status === 'working' || this.secret === '' || this.file === null; + return this.status === 'working' || (this.secret === '' && !this.noEncryption) || this.file === null; } }, mounted() { this.secret = '' as string; + this.noEncryption = false; }, methods: { ...mapActions(useDatabaseStore, ["export", "import"]), addFile(e) { + this.dataUrl = ''; + + if (e.target.files?.length !== 1) { + return; + } this.file = e.target.files[0]; - console.log(this.file); }, async exportDatabase() { try { if (this.dataUrl) { this.dataUrl = ''; } - await this.export(this.secret); - this.dataUrl = 'data:application/octet-stream,' + this.data; - this.downloadFilename = `${new Date().toISOString().replace(/:/g, '-')}_database_export.enc`; + await this.export(this.noEncryption, this.secret); + this.dataUrl = 'data:application/octet-stream,' + encodeURIComponent(JSON.stringify(this.data)); + this.downloadFilename = `${new Date().toISOString().replace(/:/g, '-')}_database_export.${this.noEncryption ? 'json' : 'enc'}`; } catch(ex) { console.log(ex); } - }, - onChangeFileUpload() { - }, async importDatabase() { try { - await this.import(this.secret, this.dataImport); + const fileContent = await this.file.text(); + await this.import(this.noEncryption, this.secret, fileContent); } catch(ex) { console.log(ex);