mirror of
https://github.com/Sebclem/hassio-nextcloud-backup.git
synced 2024-11-22 17:22:58 +01:00
84 lines
2.2 KiB
TypeScript
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;
|