Add nextcloud settings panel

This commit is contained in:
Sebastien Clement 2019-12-19 17:09:05 +01:00
parent 240fcbbf2e
commit a0840e2581
4 changed files with 164 additions and 22 deletions

View File

@ -4,12 +4,14 @@ const moment = require('moment');
const statusTools = require('../tools/status'); const statusTools = require('../tools/status');
const WebdavTools = require('../tools/webdavTools') const WebdavTools = require('../tools/webdavTools')
const webdav = new WebdavTools().getInstance(); const webdav = new WebdavTools().getInstance();
const hassioApiTools = require('../tools/hassioApiTools'); const hassioApiTools = require('../tools/hassioApiTools');
const settingsTools = require('../tools/settingsTools'); const settingsTools = require('../tools/settingsTools');
router.get('/status', (req, res, next) => { router.get('/status', (req, res, next) => {
let status = statusTools.getStatus(); let status = statusTools.getStatus();
res.json(status); res.json(status);
@ -44,12 +46,17 @@ router.get('/formated-remote-manual', function(req, res, next) {
router.post('/nextcloud-settings', function(req, res, next){ router.post('/nextcloud-settings', function(req, res, next){
console.log("ok");
let settings = req.body; let settings = req.body;
if(settings.host !== null && settings.host !== "" && settings.username !== null && settings.password !== null){ if(settings.ssl != null && settings.host != null && settings.host != "" && settings.username != null && settings.password != null){
settingsTools.setSettings(settings); webdav.setConf(settings);
webdav.confIsValid().then(()=>{
res.status(201); res.status(201);
res.send(); res.send();
}).catch((err)=>{
res.status(406);
res.json({message: err});
});
} }
else{ else{
res.status(400); res.status(400);
@ -57,5 +64,16 @@ router.post('/nextcloud-settings', function(req, res, next){
} }
}); });
router.get('/nextcloud-settings', function(req, res, next){
let conf = webdav.getConf();
if(conf == null){
res.status(404);
res.send();
}
else{
res.json(conf);
}
});
module.exports = router; module.exports = router;

View File

@ -3,7 +3,7 @@ const statusTools = require('./status');
// !!! FOR DEV PURPOSE ONLY !!! // !!! FOR DEV PURPOSE ONLY !!!
//put token here for dev (ssh port tunelling 'sudo ssh -L 80:hassio:80 root@`hassoi_ip`' + put 127.0.0.1 hassio into host) //put token here for dev (ssh port tunelling 'sudo ssh -L 80:hassio:80 root@`hassoi_ip`' + put 127.0.0.1 hassio into host)
const fallbackToken = "75c7a712290f47a7513ee75a24072f2a5f44745d9b9c4e1f9fe6d44e55da2715e7c4341de239ec1c79a5f7178dd4376e27a98ebb7b4b029a" const fallbackToken = "3afd4f8440830816e32fd490bd4f98b2423c4d9dff1432a0d57f581c43ec2bf1d1fa9468fc162732f8e95ae524c59ceed0f8e2b8a948d170"
function getSnapshots() { function getSnapshots() {

View File

@ -11,6 +11,7 @@ class WebdavTools {
init(ssl, host, username, password) { init(ssl, host, username, password) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let status = statusTools.getStatus();
console.log("Initilizing and checking webdav client...") console.log("Initilizing and checking webdav client...")
let url = (ssl ? "https" : "http") + "://" + host + endpoint; let url = (ssl ? "https" : "http") + "://" + host + endpoint;
try { try {
@ -22,21 +23,24 @@ class WebdavTools {
status.error_code = null; status.error_code = null;
statusTools.setStatus(status); statusTools.setStatus(status);
} }
console.log("Nextcloud connection: \x1b[32mSuccess !\x1b[0m");
resolve(); resolve();
}).catch((error) => { }).catch((error) => {
status.status = "Error"; status.status = "error";
status.error_code = 3; status.error_code = 3;
status.message = "Can't connect to Nextcloud (" + error + ") !" status.message = "Can't connect to Nextcloud (" + error + ") !"
statusTools.setStatus(status); statusTools.setStatus(status);
this.client = null; this.client = null;
console.error("Can't connect to Nextcloud (" + error + ") !");
reject("Can't connect to Nextcloud (" + error + ") !"); reject("Can't connect to Nextcloud (" + error + ") !");
}); });
} catch (err) { } catch (err) {
status.status = "Error"; status.status = "error";
status.error_code = 3; status.error_code = 3;
status.message = "Can't connect to Nextcloud (" + err + ") !" status.message = "Can't connect to Nextcloud (" + err + ") !"
statusTools.setStatus(status); statusTools.setStatus(status);
this.client = null; this.client = null;
console.error("Can't connect to Nextcloud (" + err + ") !");
reject("Can't connect to Nextcloud (" + err + ") !"); reject("Can't connect to Nextcloud (" + err + ") !");
} }
@ -46,20 +50,24 @@ class WebdavTools {
confIsValid() { confIsValid() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let status = statusTools.getStatus(); let status = statusTools.getStatus();
let conf = this.loadConf(); let conf = this.getConf();
if (conf !== null) { if (conf !== null) {
if (conf.ssl !== null && conf.host !== null && conf !== null && conf !== null) { if (conf.ssl !== null && conf.host !== null && conf.username !== null && conf.password !== null) {
if (status.error_code == 2) { if (status.error_code == 2) {
status.status = "idle"; status.status = "idle";
status.message = null; status.message = null;
status.error_code = null; status.error_code = null;
statusTools.setStatus(status); statusTools.setStatus(status);
} }
//TODO init connection this.init(conf.ssl, conf.host, conf.username, conf.password).then(() => {
resolve(); resolve();
}).catch((err) => {
reject(err);
});
} }
else { else {
status.status = "Error"; status.status = "error";
status.error_code = 2; status.error_code = 2;
status.message = "Nextcloud config invalid !" status.message = "Nextcloud config invalid !"
statusTools.setStatus(status); statusTools.setStatus(status);
@ -67,7 +75,7 @@ class WebdavTools {
} }
} }
else { else {
status.status = "Error"; status.status = "error";
status.error_code = 2; status.error_code = 2;
status.message = "Nextcloud config not found !" status.message = "Nextcloud config not found !"
statusTools.setStatus(status); statusTools.setStatus(status);
@ -77,7 +85,7 @@ class WebdavTools {
}); });
} }
loadConf() { 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;
@ -85,6 +93,11 @@ class WebdavTools {
else else
return null; return null;
} }
setConf(conf){
fs.writeFileSync(configPath, JSON.stringify(conf));
}
} }

View File

@ -86,7 +86,8 @@
</div> </div>
</nav> </nav>
<ul id="dropdown-settings" class="dropdown-content blue-grey darken-4"> <ul id="dropdown-settings" class="dropdown-content blue-grey darken-4">
<li><a href="#modal-settings-nextcloud" class="modal-trigger center">Nextcloud</a></li> <li><a href="#modal-settings-nextcloud" id="trigger-nextcloud-settings" class="modal-trigger center">Nextcloud</a>
</li>
<li><a href="#modal-settings-backup" class="modal-trigger center">Backup</a></li> <li><a href="#modal-settings-backup" class="modal-trigger center">Backup</a></li>
</ul> </ul>
<div class="container "> <div class="container ">
@ -156,6 +157,23 @@
<div class="col s12 center divider"> <div class="col s12 center divider">
</div> </div>
</div> </div>
<div class="row hide">
<div class="col s12 center red-text" id="nextcloud_settings_message">
</div>
</div>
<div class="row" style="margin-bottom: 10px;">
<div class="col s12" style="margin-bottom: 10px;">
<div style="color: #9e9e9e; display: inline;">SSL</div>
<div class="switch" style="display: inline;">
<label>
<input id="ssl" type="checkbox">
<span class="lever"></span>
</label>
</div>
</div>
</div>
<div class="row"> <div class="row">
<div class="input-field col s12"> <div class="input-field col s12">
<input id="hostname" type="text" class="white-text"> <input id="hostname" type="text" class="white-text">
@ -187,7 +205,64 @@
<div id="modal-loading" class="modal blue-grey darken-4 white-text"> <div id="modal-loading" class="modal blue-grey darken-4 white-text">
<div class="modal-content " > <div class="modal-content " >
<div class="row">
<div class="col s12 center">
<h4>Loading</h4>
</div>
</div>
<div class="row valign-wrapper" style="height: 150px;">
<div class="col s12 center">
<div class="preloader-wrapper big active">
<div class="spinner-layer spinner-blue">
<div class="circle-clipper left">
<div class="circle"></div>
</div>
<div class="gap-patch">
<div class="circle"></div>
</div>
<div class="circle-clipper right">
<div class="circle"></div>
</div>
</div>
<div class="spinner-layer spinner-red">
<div class="circle-clipper left">
<div class="circle"></div>
</div>
<div class="gap-patch">
<div class="circle"></div>
</div>
<div class="circle-clipper right">
<div class="circle"></div>
</div>
</div>
<div class="spinner-layer spinner-yellow">
<div class="circle-clipper left">
<div class="circle"></div>
</div>
<div class="gap-patch">
<div class="circle"></div>
</div>
<div class="circle-clipper right">
<div class="circle"></div>
</div>
</div>
<div class="spinner-layer spinner-green">
<div class="circle-clipper left">
<div class="circle"></div>
</div>
<div class="gap-patch">
<div class="circle"></div>
</div>
<div class="circle-clipper right">
<div class="circle"></div>
</div>
</div>
</div>
</div>
</div>
</div> </div>
</div> </div>
@ -225,7 +300,6 @@
M.Modal.init(loadingModals, { dismissible: false }); M.Modal.init(loadingModals, { dismissible: false });
loadingModal = M.Modal.getInstance(document.querySelector('#modal-loading')); loadingModal = M.Modal.getInstance(document.querySelector('#modal-loading'));
debugger;
$('.local-snap-listener').click(function() { $('.local-snap-listener').click(function() {
let id = this.getAttribute('data-id'); let id = this.getAttribute('data-id');
console.log(id); console.log(id);
@ -262,23 +336,60 @@
function listeners() { function listeners() {
$('#save-nextcloud-settings').click(sendNextcloudSettings); $('#save-nextcloud-settings').click(sendNextcloudSettings);
$('#trigger-nextcloud-settings').click(getNextcloudSettings)
} }
function sendNextcloudSettings() { function sendNextcloudSettings() {
loadingModal.open(); loadingModal.open();
let ssl = $('#ssl').is(':checked')
let hostname = $('#hostname').val(); let hostname = $('#hostname').val();
let username = $('#username').val(); let username = $('#username').val();
let password = $('#password').val(); let password = $('#password').val();
$.post('./api/nextcloud-settings', { hostname: hostname, username: username, password: password }) $.post('./api/nextcloud-settings', { ssl: ssl, host: hostname, username: username, password: password })
.done((data) => { .done((data) => {
console.log('Saved'); console.log('Saved');
$('#nextcloud_settings_message').parent().addClass("hide");
M.toast({ html: '<i class="material-icons" style="margin-right:10px">check_box</i> Nextcloud settings saved !', classes: "green" });
M.Modal.getInstance(document.querySelector('#modal-settings-nextcloud')).close();
}).fail((data) => { }).fail((data) => {
debugger;
if (data.status == 406) {
console.log(data.responseJSON.message);
$('#nextcloud_settings_message').html(data.responseJSON.message);
}
else {
$('#nextcloud_settings_message').html("Invalid Settings.");
}
$('#nextcloud_settings_message').parent().removeClass("hide");
M.toast({ html: '<i class="material-icons" style="margin-right:10px">warning</i> Invalid Nextcloud settings !', classes: "red" });
console.log('Fail'); console.log('Fail');
}).always(() => { }).always(() => {
loadingModal.close(); loadingModal.close();
}) })
} }
function getNextcloudSettings() {
loadingModal.open();
$.get('./api/nextcloud-settings', (data) => {
$('#ssl').prop('checked', ssl);
$('#hostname').val(data.host);
$('#hostname + label').removeClass("active");
$('#hostname + label').addClass("active");
$('#username').val(data.username);
$('#username + label').removeClass("active");
$('#username + label').addClass("active");
$('#password').val(data.password);
$('#password + label').removeClass("active");
$('#password + label').addClass("active");
loadingModal.close();
});
}
</script> </script>
</html> </html>