Use path.join in webdav service

This commit is contained in:
SebClem 2024-08-14 10:17:30 +02:00
parent 726b889b60
commit 696d8e694d
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50

View File

@ -24,6 +24,7 @@ import { getChunkEndpoint, getEndpoint } from "./webdavConfigService.js";
import { pipeline } from "stream/promises"; import { pipeline } from "stream/promises";
import { humanFileSize } from "../tools/toolbox.js"; import { humanFileSize } from "../tools/toolbox.js";
import type { BackupConfig } from "../types/services/backupConfig.js"; import type { BackupConfig } from "../types/services/backupConfig.js";
import path from "path";
const CHUNK_SIZE = 5 * 1024 * 1024; // 5MiB Same as desktop client const CHUNK_SIZE = 5 * 1024 * 1024; // 5MiB Same as desktop client
const CHUNK_NUMBER_SIZE = 5; // To add landing "0" const CHUNK_NUMBER_SIZE = 5; // To add landing "0"
@ -79,16 +80,16 @@ export function checkWebdavLogin(
export async function createBackupFolder(conf: WebdavConfig) { export async function createBackupFolder(conf: WebdavConfig) {
const root_splited = conf.backupDir.split("/").splice(1); const root_splited = conf.backupDir.split("/").splice(1);
let path = "/"; let thiPath = "/";
for (const elem of root_splited) { for (const elem of root_splited) {
if (elem != "") { if (elem != "") {
path = path + elem + "/"; thiPath = path.join(thiPath, elem);
try { try {
await createDirectory(path, conf); await createDirectory(thiPath, conf);
logger.debug(`Path ${path} created.`); logger.debug(`Path ${thiPath} created.`);
} catch (error) { } catch (error) {
if (error instanceof HTTPError && error.response.statusCode == 405) if (error instanceof HTTPError && error.response.statusCode == 405)
logger.debug(`Path ${path} already exist.`); logger.debug(`Path ${thiPath} already exist.`);
else { else {
messageManager.error("Fail to create webdav root folder"); messageManager.error("Fail to create webdav root folder");
logger.error("Fail to create webdav root folder"); logger.error("Fail to create webdav root folder");
@ -104,11 +105,11 @@ export async function createBackupFolder(conf: WebdavConfig) {
} }
for (const elem of [pathTools.auto, pathTools.manual]) { for (const elem of [pathTools.auto, pathTools.manual]) {
try { try {
await createDirectory(conf.backupDir + elem, conf); await createDirectory(path.join(conf.backupDir, elem), conf);
logger.debug(`Path ${conf.backupDir + elem} created.`); logger.debug(`Path ${path.join(conf.backupDir, elem)} created.`);
} catch (error) { } catch (error) {
if (error instanceof HTTPError && error.response.statusCode == 405) { if (error instanceof HTTPError && error.response.statusCode == 405) {
logger.debug(`Path ${conf.backupDir + elem} already exist.`); logger.debug(`Path ${path.join(conf.backupDir, elem)} already exist.`);
} else { } else {
messageManager.error("Fail to create webdav root folder"); messageManager.error("Fail to create webdav root folder");
logger.error("Fail to create webdav root folder"); logger.error("Fail to create webdav root folder");
@ -127,9 +128,9 @@ export async function createBackupFolder(conf: WebdavConfig) {
statusTools.setStatus(status); statusTools.setStatus(status);
} }
function createDirectory(path: string, config: WebdavConfig) { function createDirectory(pathToCreate: string, config: WebdavConfig) {
const endpoint = getEndpoint(config); const endpoint = getEndpoint(config);
return got(config.url + endpoint + path, { return got(path.join(config.url, endpoint, pathToCreate), {
method: "MKCOL" as Method, method: "MKCOL" as Method,
headers: { headers: {
authorization: authorization:
@ -149,7 +150,7 @@ export function getBackups(
return Promise.reject(new Error("Not logged in")); return Promise.reject(new Error("Not logged in"));
} }
const endpoint = getEndpoint(config); const endpoint = getEndpoint(config);
return got(config.url + endpoint + config.backupDir + folder, { return got(path.join(config.url, endpoint, config.backupDir, folder), {
method: "PROPFIND" as Method, method: "PROPFIND" as Method,
headers: { headers: {
authorization: authorization:
@ -201,11 +202,11 @@ function extractBackupInfo(backups: WebdavBackup[], template: string) {
return backups; return backups;
} }
export function deleteBackup(path: string, config: WebdavConfig) { export function deleteBackup(pathToDelete: string, config: WebdavConfig) {
logger.debug(`Deleting Cloud backup ${path}`); logger.debug(`Deleting Cloud backup ${pathToDelete}`);
const endpoint = getEndpoint(config); const endpoint = getEndpoint(config);
return got return got
.delete(config.url + endpoint + path, { .delete(path.join(config.url, endpoint, pathToDelete), {
headers: { headers: {
authorization: authorization:
"Basic " + "Basic " +
@ -279,7 +280,7 @@ export function webdavUploadFile(
}, },
https: { rejectUnauthorized: !config.allowSelfSignedCerts }, https: { rejectUnauthorized: !config.allowSelfSignedCerts },
}; };
const url = config.url + getEndpoint(config) + webdavPath; const url = path.join(config.url, getEndpoint(config), webdavPath);
logger.debug(`...URI: ${encodeURI(url)}`); logger.debug(`...URI: ${encodeURI(url)}`);
logger.debug(`...rejectUnauthorized: ${options.https?.rejectUnauthorized}`); logger.debug(`...rejectUnauthorized: ${options.https?.rejectUnauthorized}`);
@ -343,8 +344,12 @@ export async function chunkedUpload(
const fileSize = fs.statSync(localPath).size; const fileSize = fs.statSync(localPath).size;
const chunkEndpoint = getChunkEndpoint(config); const chunkEndpoint = getChunkEndpoint(config);
const chunkedUrl = config.url + chunkEndpoint + uuid; const chunkedUrl = path.join(config.url, chunkEndpoint, uuid);
const finalDestination = config.url + getEndpoint(config) + webdavPath; const finalDestination = path.join(
config.url,
getEndpoint(config),
webdavPath
);
const status = statusTools.getStatus(); const status = statusTools.getStatus();
status.status = States.BKUP_UPLOAD_CLOUD; status.status = States.BKUP_UPLOAD_CLOUD;
status.progress = -1; status.progress = -1;
@ -388,7 +393,7 @@ export async function chunkedUpload(
try { try {
const chunckNumber = i.toString().padStart(CHUNK_NUMBER_SIZE, "0"); const chunckNumber = i.toString().padStart(CHUNK_NUMBER_SIZE, "0");
await uploadChunk( await uploadChunk(
chunkedUrl + `/${chunckNumber}`, path.join(chunkedUrl, chunckNumber),
finalDestination, finalDestination,
chunk, chunk,
current_size, current_size,
@ -527,7 +532,7 @@ function assembleChunkedUpload(
totalLength: number, totalLength: number,
config: WebdavConfig config: WebdavConfig
) { ) {
const chunckFile = `${url}/.file`; const chunckFile = path.join(url, ".file");
logger.info(`Assemble chuncked upload.`); logger.info(`Assemble chuncked upload.`);
logger.debug(`...URI: ${encodeURI(chunckFile)}`); logger.debug(`...URI: ${encodeURI(chunckFile)}`);
logger.debug(`...Final destination: ${encodeURI(finalDestination)}`); logger.debug(`...Final destination: ${encodeURI(finalDestination)}`);
@ -563,7 +568,7 @@ export function downloadFile(
}, },
https: { rejectUnauthorized: !config.allowSelfSignedCerts }, https: { rejectUnauthorized: !config.allowSelfSignedCerts },
}; };
const url = config.url + getEndpoint(config) + webdavPath; const url = path.join(config.url, getEndpoint(config), webdavPath);
logger.debug(`...URI: ${encodeURI(url)}`); logger.debug(`...URI: ${encodeURI(url)}`);
logger.debug(`...rejectUnauthorized: ${options.https?.rejectUnauthorized}`); logger.debug(`...rejectUnauthorized: ${options.https?.rejectUnauthorized}`);
const status = statusTools.getStatus(); const status = statusTools.getStatus();