🚑 Fix ssl setting not working #16

This commit is contained in:
Sebastien 2020-06-14 19:51:28 +02:00
parent 7fa1df0fcc
commit 488e04c8b3
3 changed files with 80 additions and 80 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "Nextcloud Backup", "name": "Nextcloud Backup",
"version": "0.7.5", "version": "0.7.6",
"slug": "nextcloud_backup", "slug": "nextcloud_backup",
"description": "Hass.io snapshot backup to Nextcloud", "description": "Hass.io snapshot backup to Nextcloud",
"url": "https://github.com/Sebclem/hassio-nextcloud-backup", "url": "https://github.com/Sebclem/hassio-nextcloud-backup",

View File

@ -20,17 +20,17 @@ class WebdavTools {
this.username = null; this.username = null;
this.password = null; this.password = null;
} }
init(ssl, host, username, password) { init(ssl, host, username, password) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let status = statusTools.getStatus(); let status = statusTools.getStatus();
logger.info("Initilizing and checking webdav client..."); logger.info("Initilizing and checking webdav client...");
this.baseUrl = (ssl ? "https" : "http") + "://" + host + endpoint; this.baseUrl = (ssl == true ? "https" : "http") + "://" + host + endpoint;
this.username = username; this.username = username;
this.password = password; this.password = password;
try { try {
this.client = createClient(this.baseUrl, { username: username, password: password }); this.client = createClient(this.baseUrl, { username: username, password: password });
this.client.getDirectoryContents("/").then(() => { this.client.getDirectoryContents("/").then(() => {
if (status.error_code == 3) { if (status.error_code == 3) {
status.status = "idle"; status.status = "idle";
@ -42,7 +42,7 @@ class WebdavTools {
this.initFolder().then(() => { this.initFolder().then(() => {
resolve(); resolve();
}); });
}).catch((error) => { }).catch((error) => {
status.status = "error"; status.status = "error";
status.error_code = 3; status.error_code = 3;
@ -61,10 +61,10 @@ class WebdavTools {
logger.error("Can't connect to Nextcloud (" + err + ") !"); logger.error("Can't connect to Nextcloud (" + err + ") !");
reject("Can't connect to Nextcloud (" + err + ") !"); reject("Can't connect to Nextcloud (" + err + ") !");
} }
}); });
} }
initFolder() { initFolder() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.client.createDirectory(pathTools.root).catch(() => { }).then(() => { this.client.createDirectory(pathTools.root).catch(() => { }).then(() => {
@ -75,9 +75,9 @@ class WebdavTools {
}) })
}); });
}); });
} }
confIsValid() { confIsValid() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let status = statusTools.getStatus(); let status = statusTools.getStatus();
@ -95,7 +95,7 @@ class WebdavTools {
}).catch((err) => { }).catch((err) => {
reject(err); reject(err);
}); });
} }
else { else {
status.status = "error"; status.status = "error";
@ -114,24 +114,24 @@ class WebdavTools {
logger.error(status.message); logger.error(status.message);
reject("Nextcloud config not found !"); reject("Nextcloud config not found !");
} }
}); });
} }
getConf() { getConf() {
if (fs.existsSync(configPath)) { if (fs.existsSync(configPath)) {
let content = JSON.parse(fs.readFileSync(configPath)); let content = JSON.parse(fs.readFileSync(configPath));
return content; return content;
} }
else else
return null; return null;
} }
setConf(conf) { setConf(conf) {
fs.writeFileSync(configPath, JSON.stringify(conf)); fs.writeFileSync(configPath, JSON.stringify(conf));
} }
uploadFile(id, path) { uploadFile(id, path) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.client == null) { if (this.client == null) {
@ -142,10 +142,10 @@ class WebdavTools {
}) })
} }
else else
this._startUpload(id, path); this._startUpload(id, path);
}); });
} }
_startUpload(id, path) { _startUpload(id, path) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let status = statusTools.getStatus(); let status = statusTools.getStatus();
@ -163,61 +163,61 @@ class WebdavTools {
pass: this.password pass: this.password
}, },
body: fs.createReadStream('./temp/' + id + '.tar') body: fs.createReadStream('./temp/' + id + '.tar')
} }
let lastPercent = 0; let lastPercent = 0;
let req = request.put(option) let req = request.put(option)
.on('drain', () => { .on('drain', () => {
let percent = Math.floor((req.req.connection.bytesWritten / fileSize) * 100); let percent = Math.floor((req.req.connection.bytesWritten / fileSize) * 100);
if (lastPercent != percent) { if (lastPercent != percent) {
lastPercent = percent; lastPercent = percent;
status.progress = percent / 100; status.progress = percent / 100;
statusTools.setStatus(status); statusTools.setStatus(status);
} }
}).on('error', function(err) { }).on('error', function(err) {
fs.unlinkSync('./temp/' + id + '.tar'); fs.unlinkSync('./temp/' + id + '.tar');
status.status = "error";
status.error_code = 4;
status.message = "Fail to upload snapshot to nextcloud (" + err + ") !"
statusTools.setStatus(status);
logger.error(status.message);
reject(status.message);
}).on('response', (res) => {
if (res.statusCode != 201 && res.statusCode != 204) {
status.status = "error"; status.status = "error";
status.error_code = 4; status.error_code = 4;
status.message = "Fail to upload snapshot to nextcloud (" + err + ") !" status.message = "Fail to upload snapshot to nextcloud (Status code: " + res.statusCode + ") !"
statusTools.setStatus(status); statusTools.setStatus(status);
logger.error(status.message); logger.error(status.message);
fs.unlinkSync('./temp/' + id + '.tar')
reject(status.message); reject(status.message);
}
}).on('response', (res) => { else {
if (res.statusCode != 201 && res.statusCode != 204) { logger.info("...Upload finish ! (status: " + res.statusCode + ")");
status.status = "error"; status.status = "idle";
status.error_code = 4; status.progress = -1;
status.message = "Fail to upload snapshot to nextcloud (Status code: " + res.statusCode + ") !" status.message = null;
statusTools.setStatus(status); status.error_code = null;
logger.error(status.message); status.last_backup = moment().format('MMM D, YYYY HH:mm')
fs.unlinkSync('./temp/' + id + '.tar')
reject(status.message); statusTools.setStatus(status);
cleanTempFolder();
let autoCleanCloud = settingsTools.getSettings().auto_clean_backup;
if (autoCleanCloud != null && autoCleanCloud == "true") {
this.clean().catch();
} }
else { let autoCleanlocal = settingsTools.getSettings().auto_clean_local;
logger.info("...Upload finish ! (status: " + res.statusCode + ")"); if (autoCleanlocal != null && autoCleanlocal == "true") {
status.status = "idle"; hassioApiTools.clean();
status.progress = -1;
status.message = null;
status.error_code = null;
status.last_backup = moment().format('MMM D, YYYY HH:mm')
statusTools.setStatus(status);
cleanTempFolder();
let autoCleanCloud = settingsTools.getSettings().auto_clean_backup;
if (autoCleanCloud != null && autoCleanCloud == "true") {
this.clean().catch();
}
let autoCleanlocal = settingsTools.getSettings().auto_clean_local;
if (autoCleanlocal != null && autoCleanlocal == "true") {
hassioApiTools.clean();
}
resolve();
} }
}) resolve();
}
})
}); });
} }
getFolderContent(path) { getFolderContent(path) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(this.client == null){ if(this.client == null){
@ -225,18 +225,18 @@ class WebdavTools {
return; return;
} }
this.client.getDirectoryContents(path) this.client.getDirectoryContents(path)
.then((contents) => { .then((contents) => {
resolve(contents); resolve(contents);
}).catch((error) => { }).catch((error) => {
reject(error); reject(error);
}) })
}); });
} }
clean() { clean() {
let limit = settingsTools.getSettings().auto_clean_local_keep; let limit = settingsTools.getSettings().auto_clean_local_keep;
if (limit == null) if (limit == null)
limit = 5; limit = 5;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.getFolderContent(pathTools.auto).then(async (contents) => { this.getFolderContent(pathTools.auto).then(async (contents) => {
if (contents.length < limit) { if (contents.length < limit) {
@ -245,18 +245,18 @@ class WebdavTools {
} }
contents.sort((a, b) => { contents.sort((a, b) => {
if (moment(a.lastmod).isBefore(moment(b.lastmod))) if (moment(a.lastmod).isBefore(moment(b.lastmod)))
return 1; return 1;
else else
return -1; return -1;
}); });
let toDel = contents.slice(limit); let toDel = contents.slice(limit);
for (let i in toDel) { for (let i in toDel) {
await this.client.deleteFile(toDel[i].filename); await this.client.deleteFile(toDel[i].filename);
} }
logger.info('Cloud clean done.') logger.info('Cloud clean done.')
resolve(); resolve();
}).catch((error) => { }).catch((error) => {
status.status = "error"; status.status = "error";
status.error_code = 6; status.error_code = 6;
@ -266,18 +266,18 @@ class WebdavTools {
reject(status.message); reject(status.message);
}); });
}) })
} }
} }
function cleanTempFolder() { function cleanTempFolder() {
fs.readdir("./temp/", (err, files) => { fs.readdir("./temp/", (err, files) => {
if (err) throw err; if (err) throw err;
for (const file of files) { for (const file of files) {
fs.unlink(path.join("./temp/", file), err => { fs.unlink(path.join("./temp/", file), err => {
if (err) throw err; if (err) throw err;
@ -293,7 +293,7 @@ class Singleton {
Singleton.instance = new WebdavTools(); Singleton.instance = new WebdavTools();
} }
} }
getInstance() { getInstance() {
return Singleton.instance; return Singleton.instance;
} }

View File

@ -666,7 +666,7 @@
function getNextcloudSettings() { function getNextcloudSettings() {
loadingModal.open(); loadingModal.open();
$.get('./api/nextcloud-settings', (data) => { $.get('./api/nextcloud-settings', (data) => {
$('#ssl').prop('checked', ssl); $('#ssl').prop("checked", data.ssl == "true");
$('#hostname').val(data.host); $('#hostname').val(data.host);
$('#hostname + label').removeClass("active"); $('#hostname + label').removeClass("active");
$('#hostname + label').addClass("active"); $('#hostname + label').addClass("active");