Browse Source

fix: use string instead of File

pull/1397/head
tetuaoro 6 months ago
committed by Bernd Storath
parent
commit
f83041ba1e
  1. 18
      src/app/components/setup/migration.vue
  2. 2
      src/app/stores/setup.ts
  3. 2
      src/app/utils/api.ts
  4. 32
      src/server/api/admin/migration.post.ts

18
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<string> {
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);
});
}
</script>

2
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;
}

2
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 },

32
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<string, Client>;
};
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 };
});

Loading…
Cancel
Save