🔨 Add webdav config

This commit is contained in:
SebClem 2022-09-29 12:42:25 +02:00
parent 569374358e
commit fd73ee3176
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
6 changed files with 112 additions and 4 deletions

View File

@ -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;

View File

@ -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";

View File

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

View File

@ -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;

View File

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

View File

@ -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;