Browse Source

fix: use string instead of File

pull/1397/head
tetuaoro 8 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 { try {
await setupStore.runMigration(backupFile.value); const content = await readFileContent(backupFile.value);
await setupStore.runMigration(content);
emit('validated', null); emit('validated', null);
} catch (error) { } catch (error) {
if (error instanceof FetchError) { 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> </script>

2
src/app/stores/setup.ts

@ -20,7 +20,7 @@ export const useSetupStore = defineStore('Setup', () => {
/** /**
* @throws if unsuccessful * @throws if unsuccessful
*/ */
async function runMigration(file: File) { async function runMigration(file: string) {
const response = await api.setupMigration({ file }); const response = await api.setupMigration({ file });
return response.success; 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', { return $fetch('/api/admin/migration', {
method: 'post', method: 'post',
body: { file }, 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) => { export default defineEventHandler(async (event) => {
const { file } = await readValidatedBody( const { file } = await readValidatedBody(event, validateZod(fileType, event));
event, const file_ = JSON.parse(file) as OldConfig;
validateZod(fileType_, event)
);
// TODO: handle migration // TODO: handle migration
console.log('fileType_', file); console.log('file_', file_);
return { success: true }; return { success: true };
}); });

Loading…
Cancel
Save