2022-09-29 12:42:25 +02:00
|
|
|
import fs from "fs";
|
2022-09-30 19:13:39 +02:00
|
|
|
import Joi from "joi";
|
2022-09-29 12:42:25 +02:00
|
|
|
import logger from "../config/winston.js";
|
|
|
|
import { default_root } from "../tools/pathTools.js";
|
|
|
|
import WebdavConfigValidation from "../types/services/webdavConfigValidation.js";
|
2022-09-30 19:13:39 +02:00
|
|
|
import {
|
|
|
|
WebdavConfig,
|
|
|
|
WebdavEndpointType,
|
|
|
|
} from "../types/services/webdavConfig.js";
|
2022-09-29 12:42:25 +02:00
|
|
|
|
|
|
|
const webdavConfigPath = "/data/webdavConfigV2.json";
|
|
|
|
|
2022-09-30 19:13:39 +02:00
|
|
|
const NEXTCLOUD_ENDPOINT = "/remote.php/dav/files/$username";
|
2022-09-29 12:42:25 +02:00
|
|
|
|
2022-09-30 19:13:39 +02:00
|
|
|
export function validateWebdavConfig(config: WebdavConfig) {
|
2022-09-29 12:42:25 +02:00
|
|
|
const validator = Joi.object(WebdavConfigValidation);
|
2022-10-24 22:58:19 +02:00
|
|
|
return validator.validateAsync(config, {
|
|
|
|
abortEarly: false
|
|
|
|
});
|
2022-09-29 12:42:25 +02:00
|
|
|
}
|
|
|
|
|
2022-09-30 19:13:39 +02:00
|
|
|
export function saveWebdavConfig(config: WebdavConfig) {
|
2022-09-29 12:42:25 +02:00
|
|
|
fs.writeFileSync(webdavConfigPath, JSON.stringify(config, undefined, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getWebdavConfig(): WebdavConfig {
|
|
|
|
if (!fs.existsSync(webdavConfigPath)) {
|
2022-09-30 19:13:39 +02:00
|
|
|
logger.warn("Webdav Config file not found, creating default one !");
|
2022-09-29 12:42:25 +02:00
|
|
|
const defaultConfig = getWebdavDefaultConfig();
|
|
|
|
saveWebdavConfig(defaultConfig);
|
|
|
|
return defaultConfig;
|
|
|
|
} else {
|
|
|
|
return JSON.parse(fs.readFileSync(webdavConfigPath).toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 19:13:39 +02:00
|
|
|
export function getEndpoint(config: WebdavConfig) {
|
|
|
|
if (config.webdavEndpoint.type == WebdavEndpointType.NEXTCLOUD) {
|
|
|
|
return NEXTCLOUD_ENDPOINT.replace("$username", config.username);
|
|
|
|
} else if (config.webdavEndpoint.customEndpoint) {
|
|
|
|
return config.webdavEndpoint.customEndpoint.replace(
|
|
|
|
"$username",
|
|
|
|
config.username
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 12:42:25 +02:00
|
|
|
export function getWebdavDefaultConfig(): WebdavConfig {
|
|
|
|
return {
|
|
|
|
url: "",
|
|
|
|
username: "",
|
|
|
|
password: "",
|
|
|
|
backupDir: default_root,
|
|
|
|
allowSelfSignedCerts: false,
|
|
|
|
webdavEndpoint: {
|
|
|
|
type: WebdavEndpointType.NEXTCLOUD,
|
2022-09-30 19:13:39 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|