From f83041ba1ef8eba1492b234f5187f2344859496b Mon Sep 17 00:00:00 2001 From: tetuaoro <65575727+tetuaoro@users.noreply.github.com> Date: Wed, 9 Oct 2024 15:15:49 +0200 Subject: [PATCH] fix: use string instead of File --- src/app/components/setup/migration.vue | 18 ++++++++++++++- src/app/stores/setup.ts | 2 +- src/app/utils/api.ts | 2 +- src/server/api/admin/migration.post.ts | 32 ++++++++++++++++++++++---- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/app/components/setup/migration.vue b/src/app/components/setup/migration.vue index c9d5e804..c58aa5fa 100644 --- a/src/app/components/setup/migration.vue +++ b/src/app/components/setup/migration.vue @@ -53,7 +53,9 @@ async function sendFile() { } try { - await setupStore.runMigration(backupFile.value); + const content = await readFileContent(backupFile.value); + + await setupStore.runMigration(content); emit('validated', null); } catch (error) { if (error instanceof FetchError) { @@ -64,4 +66,18 @@ async function sendFile() { } } } + +async function readFileContent(file: File): Promise { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + + reader.onload = (event) => { + resolve(event.target?.result as string); + }; + reader.onerror = (error) => { + reject(error); + }; + reader.readAsText(file); + }); +} diff --git a/src/app/stores/setup.ts b/src/app/stores/setup.ts index 2ae5ad98..6ea61e74 100644 --- a/src/app/stores/setup.ts +++ b/src/app/stores/setup.ts @@ -20,7 +20,7 @@ export const useSetupStore = defineStore('Setup', () => { /** * @throws if unsuccessful */ - async function runMigration(file: File) { + async function runMigration(file: string) { const response = await api.setupMigration({ file }); return response.success; } diff --git a/src/app/utils/api.ts b/src/app/utils/api.ts index 23a4cddc..206d6a86 100644 --- a/src/app/utils/api.ts +++ b/src/app/utils/api.ts @@ -144,7 +144,7 @@ class API { }); } - async setupMigration({ file }: { file: File }) { + async setupMigration({ file }: { file: string }) { return $fetch('/api/admin/migration', { method: 'post', body: { file }, diff --git a/src/server/api/admin/migration.post.ts b/src/server/api/admin/migration.post.ts index fb796776..5aa49c9e 100644 --- a/src/server/api/admin/migration.post.ts +++ b/src/server/api/admin/migration.post.ts @@ -1,9 +1,31 @@ +// TODO: check what are missing +type Client = { + id: string; + name: string; + address: string; + privateKey: string; + publicKey: string; + preSharedKey: string; + createdAt: string; + updatedAt: string; + enabled: boolean; +}; + +type OldConfig = { + server: { + privateKey: string; + publicKey: string; + address: string; + }; + clients: Record; +}; + export default defineEventHandler(async (event) => { - const { file } = await readValidatedBody( - event, - validateZod(fileType_, event) - ); + const { file } = await readValidatedBody(event, validateZod(fileType, event)); + const file_ = JSON.parse(file) as OldConfig; + // TODO: handle migration - console.log('fileType_', file); + console.log('file_', file_); + return { success: true }; });