hassio-nextcloud-backup/nextcloud_backup/backend/src/services/webdavConfigService.ts

109 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-09-29 12:42:25 +02:00
import fs from "fs";
import Joi from "joi";
2022-09-29 12:42:25 +02:00
import logger from "../config/winston.js";
2024-07-11 15:47:27 +02:00
import * as pathTools from "../tools/pathTools.js";
2022-09-29 12:42:25 +02:00
import { default_root } from "../tools/pathTools.js";
2024-07-11 15:47:27 +02:00
import { WorkflowType } from "../types/services/orchecstrator.js";
import {
2024-02-18 17:19:37 +01:00
type WebdavConfig,
WebdavEndpointType,
} from "../types/services/webdavConfig.js";
2022-11-14 14:23:15 +01:00
import WebdavConfigValidation from "../types/services/webdavConfigValidation.js";
2022-09-29 12:42:25 +02:00
const webdavConfigPath = "/data/webdavConfigV2.json";
const NEXTCLOUD_ENDPOINT = "/remote.php/dav/files/$username";
const NEXTCLOUD_CHUNK_ENDPOINT = "/remote.php/dav/uploads/$username";
2022-09-29 12:42:25 +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, {
2022-11-14 14:23:15 +01:00
abortEarly: false,
2022-10-24 22:58:19 +02:00
});
2022-09-29 12:42:25 +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)) {
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 {
2024-07-11 15:47:27 +02:00
return JSON.parse(
fs.readFileSync(webdavConfigPath).toString()
) as WebdavConfig;
2022-09-29 12:42:25 +02:00
}
}
export function getEndpoint(config: WebdavConfig) {
2022-11-14 14:23:15 +01:00
let endpoint: string;
if (config.webdavEndpoint.type == WebdavEndpointType.NEXTCLOUD) {
2022-11-14 14:23:15 +01:00
endpoint = NEXTCLOUD_ENDPOINT.replace("$username", config.username);
} else if (config.webdavEndpoint.customEndpoint) {
2022-11-14 14:23:15 +01:00
endpoint = config.webdavEndpoint.customEndpoint.replace(
"$username",
config.username
);
} else {
return "";
}
if (!endpoint.startsWith("/")) {
endpoint = "/" + endpoint;
2022-11-14 14:23:15 +01:00
}
if (!endpoint.endsWith("/")) {
return endpoint + "/";
}
2022-11-14 14:23:15 +01:00
return endpoint;
}
export function getChunkEndpoint(config: WebdavConfig) {
let endpoint: string;
if (config.webdavEndpoint.type == WebdavEndpointType.NEXTCLOUD) {
endpoint = NEXTCLOUD_CHUNK_ENDPOINT.replace("$username", config.username);
} else if (config.webdavEndpoint.customChunkEndpoint) {
endpoint = config.webdavEndpoint.customChunkEndpoint.replace(
"$username",
config.username
);
} else {
return "";
}
if (!endpoint.startsWith("/")) {
endpoint = "/" + endpoint;
}
if (!endpoint.endsWith("/")) {
return endpoint + "/";
}
return endpoint;
}
export function getBackupFolder(type: WorkflowType, config: WebdavConfig) {
const end = type == WorkflowType.AUTO ? pathTools.auto : pathTools.manual;
return config.backupDir.endsWith("/")
? config.backupDir + end
: config.backupDir + "/" + end;
}
2022-09-29 12:42:25 +02:00
export function getWebdavDefaultConfig(): WebdavConfig {
return {
url: "",
username: "",
password: "",
backupDir: default_root,
allowSelfSignedCerts: false,
2023-01-13 16:18:27 +01:00
chunckedUpload: false,
2022-09-29 12:42:25 +02:00
webdavEndpoint: {
type: WebdavEndpointType.NEXTCLOUD,
},
};
}