From f8579b2a162a9d390ab438b5851af45a845ae969 Mon Sep 17 00:00:00 2001 From: SebClem Date: Thu, 29 Sep 2022 12:42:25 +0200 Subject: [PATCH] :hammer: Add webdav config --- nextcloud_backup/backend/src/routes/config.ts | 29 ++++++++++++- ...onfigService.ts => backupConfigService.ts} | 2 +- .../src/services/webdavConfigService.ts | 43 +++++++++++++++++++ .../types/services/backupConfigValidation.ts | 4 +- .../src/types/services/webdavConfig.ts | 17 ++++++++ .../types/services/webdavConfigValidation.ts | 21 +++++++++ 6 files changed, 112 insertions(+), 4 deletions(-) rename nextcloud_backup/backend/src/services/{configService.ts => backupConfigService.ts} (93%) create mode 100644 nextcloud_backup/backend/src/services/webdavConfigService.ts create mode 100644 nextcloud_backup/backend/src/types/services/webdavConfig.ts create mode 100644 nextcloud_backup/backend/src/types/services/webdavConfigValidation.ts diff --git a/nextcloud_backup/backend/src/routes/config.ts b/nextcloud_backup/backend/src/routes/config.ts index cd28ab7..5f1db41 100644 --- a/nextcloud_backup/backend/src/routes/config.ts +++ b/nextcloud_backup/backend/src/routes/config.ts @@ -1,9 +1,17 @@ -import { config } from "dotenv"; import express from "express"; -import { saveBackupConfig, validateBackupConfig } from "../services/configService.js"; +import { + getBackupConfig, + saveBackupConfig, + validateBackupConfig, +} from "../services/backupConfigService.js"; +import { getWebdavConfig, saveWebdavConfig, validateWebdavConfig } from "../services/webdavConfigService.js"; const configRouter = express.Router(); +configRouter.get("/backup", (req, res, next) => { + res.json(getBackupConfig()); +}); + configRouter.put("/backup", (req, res, next) => { validateBackupConfig(req.body) .then(() => { @@ -17,4 +25,21 @@ configRouter.put("/backup", (req, res, next) => { }); }); +configRouter.get("/webdav", (req, res, next) => { + res.json(getWebdavConfig()); +}); + +configRouter.put("/webdav", (req, res, next) => { + validateWebdavConfig(req.body) + .then(() => { + saveWebdavConfig(req.body); + res.status(204); + res.send(); + }) + .catch((error) => { + res.status(400); + res.json(error.details); + }); +}); + export default configRouter; diff --git a/nextcloud_backup/backend/src/services/configService.ts b/nextcloud_backup/backend/src/services/backupConfigService.ts similarity index 93% rename from nextcloud_backup/backend/src/services/configService.ts rename to nextcloud_backup/backend/src/services/backupConfigService.ts index ec3c000..598d640 100644 --- a/nextcloud_backup/backend/src/services/configService.ts +++ b/nextcloud_backup/backend/src/services/backupConfigService.ts @@ -1,8 +1,8 @@ import fs from "fs"; import Joi from "joi" import logger from "../config/winston.js"; -import { backupConfigValidation } from "../types/services/backupConfigValidation.js" import { BackupConfig } from "../types/services/backupConfig.js" +import backupConfigValidation from "../types/services/backupConfigValidation.js"; const backupConfigPath = "/data/backupConfigV2.json"; diff --git a/nextcloud_backup/backend/src/services/webdavConfigService.ts b/nextcloud_backup/backend/src/services/webdavConfigService.ts new file mode 100644 index 0000000..060c1aa --- /dev/null +++ b/nextcloud_backup/backend/src/services/webdavConfigService.ts @@ -0,0 +1,43 @@ +import fs from "fs"; +import Joi from "joi" +import logger from "../config/winston.js"; +import { default_root } from "../tools/pathTools.js"; +import WebdavConfigValidation from "../types/services/webdavConfigValidation.js"; +import { WebdavConfig, WebdavEndpointType } from "../types/services/webdavConfig.js" + + +const webdavConfigPath = "/data/webdavConfigV2.json"; + + +export function validateWebdavConfig(config: WebdavConfig){ + const validator = Joi.object(WebdavConfigValidation); + return validator.validateAsync(config); +} + +export function saveWebdavConfig(config: WebdavConfig){ + fs.writeFileSync(webdavConfigPath, JSON.stringify(config, undefined, 2)); +} + +export function getWebdavConfig(): WebdavConfig { + if (!fs.existsSync(webdavConfigPath)) { + logger.warn("Webdav Config file not found, creating default one !") + const defaultConfig = getWebdavDefaultConfig(); + saveWebdavConfig(defaultConfig); + return defaultConfig; + } else { + return JSON.parse(fs.readFileSync(webdavConfigPath).toString()); + } +} + +export function getWebdavDefaultConfig(): WebdavConfig { + return { + url: "", + username: "", + password: "", + backupDir: default_root, + allowSelfSignedCerts: false, + webdavEndpoint: { + type: WebdavEndpointType.NEXTCLOUD, + } + } +} \ No newline at end of file diff --git a/nextcloud_backup/backend/src/types/services/backupConfigValidation.ts b/nextcloud_backup/backend/src/types/services/backupConfigValidation.ts index cbaf304..a835e60 100644 --- a/nextcloud_backup/backend/src/types/services/backupConfigValidation.ts +++ b/nextcloud_backup/backend/src/types/services/backupConfigValidation.ts @@ -39,7 +39,7 @@ const AutoCleanConfig = { }), }; -export const backupConfigValidation = { +const backupConfigValidation = { nameTemplate: Joi.string().required().not().empty(), cron: Joi.array().items(CronConfigValidation).required(), autoClean: Joi.object({ @@ -60,3 +60,5 @@ export const backupConfigValidation = { }), }), }; + +export default backupConfigValidation; diff --git a/nextcloud_backup/backend/src/types/services/webdavConfig.ts b/nextcloud_backup/backend/src/types/services/webdavConfig.ts new file mode 100644 index 0000000..3b28fed --- /dev/null +++ b/nextcloud_backup/backend/src/types/services/webdavConfig.ts @@ -0,0 +1,17 @@ +export enum WebdavEndpointType { + NEXTCLOUD = "NEXTCLOUD", + CUSTOM = "CUSTOM" +} + + +export interface WebdavConfig { + url: string; + username: string; + password: string; + backupDir: string; + allowSelfSignedCerts: boolean + webdavEndpoint: { + type: WebdavEndpointType; + customEndpoint?: string; + } +} \ No newline at end of file diff --git a/nextcloud_backup/backend/src/types/services/webdavConfigValidation.ts b/nextcloud_backup/backend/src/types/services/webdavConfigValidation.ts new file mode 100644 index 0000000..0b40d06 --- /dev/null +++ b/nextcloud_backup/backend/src/types/services/webdavConfigValidation.ts @@ -0,0 +1,21 @@ +import Joi, { not } from "joi"; +import { WebdavEndpointType } from "./webdavConfig.js"; + + +const WebdavConfigValidation = { + url: Joi.string().not().empty().required(), + username: Joi.string().not().empty().required(), + password: Joi.string().not().empty().required(), + backupDir: Joi.string().required(), + allowSelfSignedCerts: Joi.boolean().required(), + webdavEndpoint: Joi.object({ + type: Joi.string().valid(WebdavEndpointType.CUSTOM, WebdavEndpointType.NEXTCLOUD).required(), + customEndpoint: Joi.alternatives().conditional("type", { + is: WebdavEndpointType.CUSTOM, + then: Joi.string().not().empty().required, + otherwise: Joi.disallow() + }) + }).required() +} + +export default WebdavConfigValidation; \ No newline at end of file