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";
|
2022-11-14 14:23:15 +01:00
|
|
|
import type { WebdavDelete } from "../types/services/webdav.js";
|
|
|
|
import { WebdavDeleteValidation } from "../types/services/webdavValidation.js";
|
2022-09-30 23:52:27 +02:00
|
|
|
|
|
|
|
const webdavRouter = express.Router();
|
|
|
|
|
|
|
|
webdavRouter.get("/backup/auto", (req, res, next) => {
|
|
|
|
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 () => {
|
|
|
|
const value = await webdavService
|
2022-11-14 16:12:05 +01:00
|
|
|
.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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
webdavRouter.get("/backup/manual", (req, res, next) => {
|
|
|
|
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 () => {
|
|
|
|
const value = await webdavService
|
2022-11-14 16:12:05 +01:00
|
|
|
.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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-14 14:23:15 +01:00
|
|
|
webdavRouter.delete("/", (req, res, next) => {
|
|
|
|
const body: WebdavDelete = req.body;
|
|
|
|
const validator = Joi.object(WebdavDeleteValidation);
|
|
|
|
const config = getWebdavConfig();
|
|
|
|
validateWebdavConfig(config).then(() => {
|
|
|
|
validator
|
|
|
|
.validateAsync(body)
|
2024-04-18 10:42:22 +02:00
|
|
|
.then(() => {
|
|
|
|
return webdavService.checkWebdavLogin(config);
|
|
|
|
})
|
2022-11-14 14:23:15 +01:00
|
|
|
.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-09-30 23:52:27 +02:00
|
|
|
export default webdavRouter;
|