From f297857d70ae6ace90a1dc577d30827fcfbf2ffd Mon Sep 17 00:00:00 2001 From: Simon Smith Date: Wed, 6 Sep 2023 13:53:28 +0100 Subject: [PATCH 1/3] fix-webdav-upload --- db.js | 52 +++++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/db.js b/db.js index 4fd9fbb4..c361985f 100644 --- a/db.js +++ b/db.js @@ -3064,39 +3064,37 @@ module.exports.CreateDB = function (parent, func) { // Upload to the WebDAV folder function performWebDavUpload(client, filepath) { - var fileStream = require('fs').createReadStream(filepath); - fileStream.on('close', function () { if (func) { func('WebDAV upload completed'); } }) - fileStream.on('error', function (err) { if (func) { func('WebDAV (fileUpload) error: ' + err); } }) - fileStream.pipe(client.createWriteStream('/' + webdavfolderName + '/' + require('path').basename(filepath))); - if (func) { func('Uploading using WebDAV...'); } + require('fs').stat(filepath, function(err,stat){ + var fileStream = require('fs').createReadStream(filepath); + fileStream.on('close', function () { if (func) { func('WebDAV upload completed'); } }) + fileStream.on('error', function (err) { if (func) { func('WebDAV (fileUpload) error: ' + err); } }) + fileStream.pipe(client.createWriteStream('/' + webdavfolderName + '/' + require('path').basename(filepath), { headers: { "Content-Length": stat.size } })); + if (func) { func('Uploading using WebDAV...'); } + }); } if (func) { func('Attempting WebDAV upload...'); } const { createClient } = require('webdav'); - const client = createClient(parent.config.settings.autobackup.webdav.url, { username: parent.config.settings.autobackup.webdav.username, password: parent.config.settings.autobackup.webdav.password }); - var directoryItems = client.getDirectoryContents('/'); - directoryItems.then( - function (files) { - var folderFound = false; - for (var i in files) { if ((files[i].basename == webdavfolderName) && (files[i].type == 'directory')) { folderFound = true; } } - if (folderFound == false) { - client.createDirectory(webdavfolderName).then(function (a) { - if (a.statusText == 'Created') { - if (func) { func('WebDAV folder created'); } - performWebDavUpload(client, filename); - } else { - if (func) { func('WebDAV (createDirectory) status: ' + a.statusText); } - } - }).catch(function (err) { - if (func) { func('WebDAV (createDirectory) error: ' + err); } - }); - } else { - performWebDavCleanup(client); + const client = createClient(parent.config.settings.autobackup.webdav.url, { + username: parent.config.settings.autobackup.webdav.username, + password: parent.config.settings.autobackup.webdav.password, + maxContentLength: Infinity, + maxBodyLength: Infinity + }); + client.exists(webdavfolderName).then(function(a){ + if(a){ + performWebDavCleanup(client); + performWebDavUpload(client, filename); + }else{ + client.createDirectory(webdavfolderName, {recursive: true}).then(function (a) { + if (func) { func('WebDAV folder created'); } performWebDavUpload(client, filename); - } + }).catch(function (err) { + if (func) { func('WebDAV (createDirectory) error: ' + err); } + }); } - ).catch(function (err) { - if (func) { func('WebDAV (getDirectoryContents) error: ' + err); } + }).catch(function (err) { + if (func) { func('WebDAV (exists) error: ' + err); } }); } From 749d5b0ea5a917cfa137d9469df67b71598c3588 Mon Sep 17 00:00:00 2001 From: Simon Smith Date: Wed, 6 Sep 2023 13:55:04 +0100 Subject: [PATCH 2/3] fix-google-webdav-module-install --- meshcentral.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/meshcentral.js b/meshcentral.js index 981401d7..7f9fe92e 100644 --- a/meshcentral.js +++ b/meshcentral.js @@ -4022,12 +4022,12 @@ function mainStart() { // Setup encrypted zip support if needed if (config.settings.autobackup && config.settings.autobackup.zippassword) { modules.push('archiver-zip-encrypted'); - // Enable Google Drive Support - if (typeof config.settings.autobackup.googledrive == 'object') { modules.push('googleapis'); } - // Enable WebDAV Support - if (typeof config.settings.autobackup.webdav == 'object') { - if ((typeof config.settings.autobackup.webdav.url != 'string') || (typeof config.settings.autobackup.webdav.username != 'string') || (typeof config.settings.autobackup.webdav.password != 'string')) { addServerWarning("Missing WebDAV parameters.", 2, null, !args.launch); } else { modules.push('webdav@4.11.2'); } - } + } + // Enable Google Drive Support + if (typeof config.settings.autobackup.googledrive == 'object') { modules.push('googleapis'); } + // Enable WebDAV Support + if (typeof config.settings.autobackup.webdav == 'object') { + if ((typeof config.settings.autobackup.webdav.url != 'string') || (typeof config.settings.autobackup.webdav.username != 'string') || (typeof config.settings.autobackup.webdav.password != 'string')) { addServerWarning("Missing WebDAV parameters.", 2, null, !args.launch); } else { modules.push('webdav@4.11.2'); } } // Setup common password blocking From 5fa27a033920461362cc0e1cb4321881acd5908b Mon Sep 17 00:00:00 2001 From: Simon Smith Date: Wed, 6 Sep 2023 13:55:50 +0100 Subject: [PATCH 3/3] update-webdav-module --- meshcentral.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meshcentral.js b/meshcentral.js index 7f9fe92e..6e74024e 100644 --- a/meshcentral.js +++ b/meshcentral.js @@ -4027,7 +4027,7 @@ function mainStart() { if (typeof config.settings.autobackup.googledrive == 'object') { modules.push('googleapis'); } // Enable WebDAV Support if (typeof config.settings.autobackup.webdav == 'object') { - if ((typeof config.settings.autobackup.webdav.url != 'string') || (typeof config.settings.autobackup.webdav.username != 'string') || (typeof config.settings.autobackup.webdav.password != 'string')) { addServerWarning("Missing WebDAV parameters.", 2, null, !args.launch); } else { modules.push('webdav@4.11.2'); } + if ((typeof config.settings.autobackup.webdav.url != 'string') || (typeof config.settings.autobackup.webdav.username != 'string') || (typeof config.settings.autobackup.webdav.password != 'string')) { addServerWarning("Missing WebDAV parameters.", 2, null, !args.launch); } else { modules.push('webdav@4.11.3'); } } // Setup common password blocking