From e94f767e93acb1c430ae9e98b67f91c7b701dc17 Mon Sep 17 00:00:00 2001 From: SebClem Date: Mon, 22 Jul 2024 11:20:43 +0200 Subject: [PATCH] Add ha to cloud manual upload --- .../backend/src/routes/homeAssistant.ts | 13 ++++ .../backend/src/services/orchestrator.ts | 59 ++++++++++++++++++- .../src/components/homeAssistant/HaList.vue | 19 +++++- .../components/homeAssistant/HaListItem.vue | 8 ++- .../src/services/homeAssistantService.ts | 4 ++ 5 files changed, 99 insertions(+), 4 deletions(-) diff --git a/nextcloud_backup/backend/src/routes/homeAssistant.ts b/nextcloud_backup/backend/src/routes/homeAssistant.ts index e50cf43..62e8b0b 100644 --- a/nextcloud_backup/backend/src/routes/homeAssistant.ts +++ b/nextcloud_backup/backend/src/routes/homeAssistant.ts @@ -1,5 +1,7 @@ import express from "express"; import * as haOsService from "../services/homeAssistantService.js"; +import { uploadToCloud } from "../services/orchestrator.js"; +import logger from "../config/winston.js"; const homeAssistantRouter = express.Router(); @@ -29,6 +31,17 @@ homeAssistantRouter.get("/backup/:slug", (req, res) => { }); }); +homeAssistantRouter.post("/backup/:slug/upload", (req, res) => { + uploadToCloud(req.params.slug) + .then(() => { + logger.info("All good !"); + }) + .catch(() => { + logger.error("Something wrong !"); + }); + res.sendStatus(202); +}); + homeAssistantRouter.get("/addons", (req, res) => { haOsService .getAddonList() diff --git a/nextcloud_backup/backend/src/services/orchestrator.ts b/nextcloud_backup/backend/src/services/orchestrator.ts index 9a3e2bd..0eb7065 100644 --- a/nextcloud_backup/backend/src/services/orchestrator.ts +++ b/nextcloud_backup/backend/src/services/orchestrator.ts @@ -4,7 +4,10 @@ import logger from "../config/winston.js"; import messageManager from "../tools/messageManager.js"; import * as statusTools from "../tools/status.js"; import { BackupType } from "../types/services/backupConfig.js"; -import type { AddonModel } from "../types/services/ha_os_response.js"; +import type { + AddonModel, + BackupDetailModel, +} from "../types/services/ha_os_response.js"; import { WorkflowType } from "../types/services/orchecstrator.js"; import * as backupConfigService from "./backupConfigService.js"; import * as homeAssistantService from "./homeAssistantService.js"; @@ -89,7 +92,10 @@ export function doBackupWorkflow(type: WorkflowType) { }) .then(() => { logger.info("Backup workflow finished successfully !"); - messageManager.info("Backup workflow finished successfully !"); + messageManager.info( + "Backup workflow finished successfully !", + `name: ${name}` + ); const status = statusTools.getStatus(); status.last_backup.success = true; status.last_backup.last_try = DateTime.now(); @@ -105,6 +111,55 @@ export function doBackupWorkflow(type: WorkflowType) { }); } +export function uploadToCloud(slug: string) { + const webdavConfig = getWebdavConfig(); + let tmpBackupFile = ""; + let backupInfo = {} as BackupDetailModel; + + return webDavService + .checkWebdavLogin(webdavConfig) + .then(() => { + return homeAssistantService.getBackupInfo(slug); + }) + .then((response) => { + backupInfo = response.body.data; + return homeAssistantService.downloadSnapshot(slug); + }) + .then((tmpFile) => { + tmpBackupFile = tmpFile; + if (webdavConfig.chunckedUpload) { + return webDavService.chunkedUpload( + tmpFile, + getBackupFolder(WorkflowType.MANUAL, webdavConfig) + + backupInfo.name + + ".tar", + webdavConfig + ); + } else { + return webDavService.webdavUploadFile( + tmpFile, + getBackupFolder(WorkflowType.MANUAL, webdavConfig) + + backupInfo.name + + ".tar", + webdavConfig + ); + } + }) + .then(() => { + logger.info(`Successfully uploaded ${backupInfo.name} to cloud.`); + messageManager.info( + "Successfully uploaded backup to cloud.", + `Name: ${backupInfo.name}` + ); + }) + .catch(() => { + if (tmpBackupFile != "") { + unlinkSync(tmpBackupFile); + } + return Promise.reject(new Error()); + }); +} + // This methods remove addon that are no installed in HA from the conf array function sanitizeAddonList(addonInConf: string[], addonInHA: AddonModel[]) { return addonInConf.filter((value) => addonInHA.some((v) => v.slug == value)); diff --git a/nextcloud_backup/frontend/src/components/homeAssistant/HaList.vue b/nextcloud_backup/frontend/src/components/homeAssistant/HaList.vue index f9e4c6a..00e8952 100644 --- a/nextcloud_backup/frontend/src/components/homeAssistant/HaList.vue +++ b/nextcloud_backup/frontend/src/components/homeAssistant/HaList.vue @@ -37,6 +37,7 @@ :key="item.slug" :item="item" :index="index" + @upload="upload" > @@ -52,12 +53,18 @@ diff --git a/nextcloud_backup/frontend/src/services/homeAssistantService.ts b/nextcloud_backup/frontend/src/services/homeAssistantService.ts index f78ce91..c99dcba 100644 --- a/nextcloud_backup/frontend/src/services/homeAssistantService.ts +++ b/nextcloud_backup/frontend/src/services/homeAssistantService.ts @@ -21,3 +21,7 @@ export function getBackups() { export function getBackupDetail(slug: string) { return kyClient.get(`homeAssistant/backup/${slug}`).json(); } + +export function uploadHomeAssistantBackup(slug: string) { + return kyClient.post(`homeAssistant/backup/${slug}/upload`); +}