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

84 lines
2.2 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();
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();
const backupConf = getBackupConfig();
2022-09-30 23:52:27 +02:00
validateWebdavConfig(config)
.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();
const backupConf = getBackupConfig();
2022-09-30 23:52:27 +02:00
validateWebdavConfig(config)
.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) => {
const body = req.body as WebdavDelete;
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
});
2022-09-30 23:52:27 +02:00
export default webdavRouter;