2022-09-29 00:05:53 +02:00
|
|
|
import express from "express";
|
2022-09-29 12:42:25 +02:00
|
|
|
import {
|
|
|
|
getBackupConfig,
|
|
|
|
saveBackupConfig,
|
|
|
|
validateBackupConfig,
|
|
|
|
} from "../services/backupConfigService.js";
|
2024-07-11 15:47:27 +02:00
|
|
|
import {
|
|
|
|
getWebdavConfig,
|
|
|
|
saveWebdavConfig,
|
|
|
|
validateWebdavConfig,
|
|
|
|
} from "../services/webdavConfigService.js";
|
2024-04-18 10:42:22 +02:00
|
|
|
import { checkWebdavLogin } from "../services/webdavService.js";
|
2024-07-11 15:47:27 +02:00
|
|
|
import type { BackupConfig } from "../types/services/backupConfig.js";
|
2024-07-11 17:07:32 +02:00
|
|
|
import type { ValidationError } from "joi";
|
2024-07-11 15:47:27 +02:00
|
|
|
import type { WebdavConfig } from "../types/services/webdavConfig.js";
|
2022-09-29 00:05:53 +02:00
|
|
|
|
|
|
|
const configRouter = express.Router();
|
|
|
|
|
2024-07-11 15:47:27 +02:00
|
|
|
configRouter.get("/backup", (req, res) => {
|
2022-09-29 12:42:25 +02:00
|
|
|
res.json(getBackupConfig());
|
|
|
|
});
|
|
|
|
|
2024-07-11 15:47:27 +02:00
|
|
|
configRouter.put("/backup", (req, res) => {
|
|
|
|
validateBackupConfig(req.body as BackupConfig)
|
2024-08-09 17:08:20 +02:00
|
|
|
.then(
|
|
|
|
() => {
|
|
|
|
return saveBackupConfig(req.body as BackupConfig);
|
|
|
|
},
|
|
|
|
(error: ValidationError) => {
|
|
|
|
res.status(400).json(error.details);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.then(
|
|
|
|
() => {
|
|
|
|
res.status(204).send();
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
res.status(400).json({
|
|
|
|
message: "Fail to init cron, please check cron config",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2022-09-29 00:05:53 +02:00
|
|
|
});
|
|
|
|
|
2024-07-11 15:47:27 +02:00
|
|
|
configRouter.get("/webdav", (req, res) => {
|
2022-09-29 12:42:25 +02:00
|
|
|
res.json(getWebdavConfig());
|
|
|
|
});
|
|
|
|
|
2024-07-11 15:47:27 +02:00
|
|
|
configRouter.put("/webdav", (req, res) => {
|
|
|
|
validateWebdavConfig(req.body as WebdavConfig)
|
2024-04-18 10:42:22 +02:00
|
|
|
.then(() => {
|
2024-07-11 15:47:27 +02:00
|
|
|
return checkWebdavLogin(req.body as WebdavConfig, true);
|
2024-04-18 10:42:22 +02:00
|
|
|
})
|
2022-09-29 12:42:25 +02:00
|
|
|
.then(() => {
|
2024-07-11 15:47:27 +02:00
|
|
|
saveWebdavConfig(req.body as WebdavConfig);
|
2024-07-22 11:20:10 +02:00
|
|
|
res.status(204).send();
|
2022-09-29 12:42:25 +02:00
|
|
|
})
|
2024-07-11 17:07:32 +02:00
|
|
|
.catch((error: ValidationError) => {
|
2022-09-29 12:42:25 +02:00
|
|
|
res.status(400);
|
2024-07-11 17:07:32 +02:00
|
|
|
if (error.details) {
|
2024-07-11 15:47:27 +02:00
|
|
|
res.json(error.details);
|
|
|
|
} else {
|
|
|
|
res.json(error);
|
|
|
|
}
|
2022-09-29 12:42:25 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-29 00:05:53 +02:00
|
|
|
export default configRouter;
|