hassio-nextcloud-backup/nextcloud_backup/backend/src/routes/webdav.ts
2024-08-13 16:43:13 +02:00

84 lines
2.2 KiB
TypeScript

import express from "express";
import Joi from "joi";
import { getBackupConfig } from "../services/backupConfigService.js";
import {
getWebdavConfig,
validateWebdavConfig,
} from "../services/webdavConfigService.js";
import * as webdavService from "../services/webdavService.js";
import * as pathTools from "../tools/pathTools.js";
import type { WebdavDelete } from "../types/services/webdav.js";
import { WebdavDeleteValidation } from "../types/services/webdavValidation.js";
const webdavRouter = express.Router();
webdavRouter.get("/backup/auto", (req, res) => {
const config = getWebdavConfig();
const backupConf = getBackupConfig();
validateWebdavConfig(config)
.then(() => {
return webdavService.checkWebdavLogin(config);
})
.then(async () => {
const value = await webdavService.getBackups(
pathTools.auto,
config,
backupConf.nameTemplate
);
res.json(value);
})
.catch((reason) => {
res.status(500);
res.json(reason);
});
});
webdavRouter.get("/backup/manual", (req, res) => {
const config = getWebdavConfig();
const backupConf = getBackupConfig();
validateWebdavConfig(config)
.then(() => {
return webdavService.checkWebdavLogin(config);
})
.then(async () => {
const value = await webdavService.getBackups(
pathTools.manual,
config,
backupConf.nameTemplate
);
res.json(value);
})
.catch((reason) => {
res.status(500);
res.json(reason);
});
});
webdavRouter.delete("/", (req, res) => {
const body = req.body as WebdavDelete;
const validator = Joi.object(WebdavDeleteValidation);
const config = getWebdavConfig();
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);
});
});
export default webdavRouter;