hassio-nextcloud-backup/nextcloud_backup/backend/src/routes/webdav.ts

67 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-09-30 23:52:27 +02:00
import express from "express";
2022-11-14 14:23:15 +01:00
import Joi from "joi";
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();
const backupConf = getBackupConfig();
2022-09-30 23:52:27 +02:00
validateWebdavConfig(config)
2022-11-14 14:23:15 +01:00
.then(async () => {
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);
});
});
webdavRouter.get("/backup/manual", (req, res, next) => {
const config = getWebdavConfig();
const backupConf = getBackupConfig();
2022-09-30 23:52:27 +02:00
validateWebdavConfig(config)
2022-11-14 14:23:15 +01:00
.then(async () => {
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);
});
});
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)
.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;