2025-02-02 16:37:46 +01:00
|
|
|
import { defineStore } from "pinia";
|
|
|
|
import { http } from "@/serverCom";
|
|
|
|
import type { AxiosResponse, AxiosProgressEvent } from "axios";
|
|
|
|
import type { BackupRestoreViewModel } from "../../../viewmodels/admin/user/backup.models";
|
|
|
|
|
|
|
|
export const useBackupStore = defineStore("backup", {
|
|
|
|
state: () => {
|
|
|
|
return {
|
|
|
|
backups: [] as Array<string>,
|
|
|
|
loading: null as null | "loading" | "success" | "failed",
|
2025-02-03 11:03:38 +01:00
|
|
|
page: "generated" as "generated" | "uploaded",
|
2025-02-02 16:37:46 +01:00
|
|
|
};
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
fetchBackups() {
|
|
|
|
this.loading = "loading";
|
|
|
|
http
|
2025-02-03 11:03:38 +01:00
|
|
|
.get(`/admin/backup/${this.page}`)
|
2025-02-02 16:37:46 +01:00
|
|
|
.then((result) => {
|
|
|
|
this.backups = result.data;
|
|
|
|
this.loading = "success";
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.loading = "failed";
|
|
|
|
});
|
|
|
|
},
|
|
|
|
fetchBackupById(filename: string): Promise<AxiosResponse<any, any>> {
|
2025-02-03 11:03:38 +01:00
|
|
|
return http.get(`/admin/backup/${this.page}/${filename}`);
|
|
|
|
},
|
|
|
|
async restoreBackup(backup: BackupRestoreViewModel): Promise<AxiosResponse<any, any>> {
|
|
|
|
return await http.post(`/admin/backup/${this.page}/restore`, backup);
|
2025-02-02 16:37:46 +01:00
|
|
|
},
|
|
|
|
async triggerBackupCreate(): Promise<AxiosResponse<any, any>> {
|
|
|
|
const result = await http.post("/admin/backup");
|
|
|
|
this.fetchBackups();
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
async uploadBackup(file: File): Promise<AxiosResponse<any, any>> {
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("file", file);
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "multipart/form-data",
|
|
|
|
},
|
|
|
|
onUploadProgress: (progressEvent: AxiosProgressEvent) => {
|
|
|
|
const { loaded, total = 1 } = progressEvent;
|
|
|
|
console.log("progress", Math.floor((loaded * 100) / total));
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = await http.post("/admin/backup/upload", formData, options);
|
|
|
|
this.fetchBackups();
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|