2022-09-30 23:52:27 +02:00
|
|
|
import express from "express";
|
2022-11-14 14:23:15 +01:00
|
|
|
import Joi from "joi";
|
2022-11-14 16:12:05 +01:00
|
|
|
import { getBackupConfig } from "../services/backupConfigService.js";
|
2022-09-30 23:52:27 +02:00
|
|
|
import {
|
|
|
|
getWebdavConfig,
|
|
|
|
validateWebdavConfig,
|
|
|
|
} from "../services/webdavConfigService.js";
|
|
|
|
import * as webdavService from "../services/webdavService.js";
|
|
|
|
import * as pathTools from "../tools/pathTools.js";
|
2024-07-22 17:22:24 +02:00
|
|
|
import type { WebdavGenericPath } from "../types/services/webdav.js";
|
2022-11-14 14:23:15 +01:00
|
|
|
import { WebdavDeleteValidation } from "../types/services/webdavValidation.js";
|
2024-07-22 17:22:24 +02:00
|
|
|
import { restoreToHA } from "../services/orchestrator.js";
|
|
|
|
import path from "path";
|
|
|
|
import logger from "../config/winston.js";
|
2022-09-30 23:52:27 +02:00
|
|
|
|
|
|
|
const webdavRouter = express.Router();
|
|
|
|
|
2024-07-11 15:47:27 +02:00
|
|
|
webdavRouter.get("/backup/auto", (req, res) => {
|
2022-09-30 23:52:27 +02:00
|
|
|
const config = getWebdavConfig();
|
2022-11-14 16:12:05 +01:00
|
|
|
const backupConf = getBackupConfig();
|
2022-09-30 23:52:27 +02:00
|
|
|
validateWebdavConfig(config)
|
2024-04-18 10:42:22 +02:00
|
|
|
.then(() => {
|
|
|
|
return webdavService.checkWebdavLogin(config);
|
|
|
|
})
|
2022-11-14 14:23:15 +01:00
|
|
|
.then(async () => {
|
2024-07-11 15:47:27 +02:00
|
|
|
const value = await webdavService.getBackups(
|
|
|
|
pathTools.auto,
|
|
|
|
config,
|
|
|
|
backupConf.nameTemplate
|
|
|
|
);
|
2022-11-14 14:23:15 +01:00
|
|
|
res.json(value);
|
2022-09-30 23:52:27 +02:00
|
|
|
})
|
|
|
|
.catch((reason) => {
|
|
|
|
res.status(500);
|
|
|
|
res.json(reason);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-07-11 15:47:27 +02:00
|
|
|
webdavRouter.get("/backup/manual", (req, res) => {
|
2022-09-30 23:52:27 +02:00
|
|
|
const config = getWebdavConfig();
|
2022-11-14 16:12:05 +01:00
|
|
|
const backupConf = getBackupConfig();
|
2022-09-30 23:52:27 +02:00
|
|
|
validateWebdavConfig(config)
|
2024-04-18 10:42:22 +02:00
|
|
|
.then(() => {
|
|
|
|
return webdavService.checkWebdavLogin(config);
|
|
|
|
})
|
2022-11-14 14:23:15 +01:00
|
|
|
.then(async () => {
|
2024-07-11 15:47:27 +02:00
|
|
|
const value = await webdavService.getBackups(
|
|
|
|
pathTools.manual,
|
|
|
|
config,
|
|
|
|
backupConf.nameTemplate
|
|
|
|
);
|
2022-11-14 14:23:15 +01:00
|
|
|
res.json(value);
|
2022-09-30 23:52:27 +02:00
|
|
|
})
|
|
|
|
.catch((reason) => {
|
|
|
|
res.status(500);
|
|
|
|
res.json(reason);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-07-11 15:47:27 +02:00
|
|
|
webdavRouter.delete("/", (req, res) => {
|
2024-07-22 17:22:24 +02:00
|
|
|
const body = req.body as WebdavGenericPath;
|
2022-11-14 14:23:15 +01:00
|
|
|
const validator = Joi.object(WebdavDeleteValidation);
|
|
|
|
const config = getWebdavConfig();
|
2024-07-11 15:47:27 +02:00
|
|
|
validateWebdavConfig(config)
|
|
|
|
.then(() => {
|
|
|
|
return validator.validateAsync(body);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return webdavService.checkWebdavLogin(config);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
webdavService
|
|
|
|
.deleteBackup(body.path, config)
|
|
|
|
.then(() => {
|
|
|
|
res.status(201).send();
|
|
|
|
})
|
|
|
|
.catch((reason) => {
|
|
|
|
res.status(500).json(reason);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((reason) => {
|
|
|
|
res.status(400).json(reason);
|
|
|
|
});
|
2022-11-14 14:23:15 +01:00
|
|
|
});
|
|
|
|
|
2024-07-22 17:22:24 +02:00
|
|
|
webdavRouter.post("/restore", (req, res) => {
|
|
|
|
const body = req.body as WebdavGenericPath;
|
|
|
|
const validator = Joi.object(WebdavDeleteValidation);
|
|
|
|
validator
|
|
|
|
.validateAsync(body)
|
|
|
|
.then(() => {
|
|
|
|
return restoreToHA(body.path, path.basename(body.path));
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
logger.info("All good !");
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
logger.error("Something wrong !");
|
|
|
|
});
|
|
|
|
res.sendStatus(202);
|
|
|
|
});
|
|
|
|
|
2022-09-30 23:52:27 +02:00
|
|
|
export default webdavRouter;
|