✏️ Add error detail on config save

This commit is contained in:
Sebastien Clement 2021-09-07 12:38:43 +02:00
parent c419ab24ea
commit c9b4b71e85
No known key found for this signature in database
GPG Key ID: 6C78A38312BBA4B6
3 changed files with 280 additions and 283 deletions

View File

@ -7,51 +7,48 @@ const default_toast_timeout = 10000;
let loadingModal; let loadingModal;
let nextcloud_setting_modal; let nextcloud_setting_modal;
let backup_setting_modal; let backup_setting_modal;
document.addEventListener('DOMContentLoaded', function () { document.addEventListener("DOMContentLoaded", function () {
$.ajaxSetup({ traditional: true }); $.ajaxSetup({ traditional: true });
updateLocalSnaps(); updateLocalSnaps();
update_status(); update_status();
loadingModal = new bootstrap.Modal(document.getElementById('loading-modal'), { loadingModal = new bootstrap.Modal(document.getElementById("loading-modal"), {
keyboard: false, keyboard: false,
backdrop: 'static' backdrop: "static",
}); });
nextcloud_setting_modal = new bootstrap.Modal(document.getElementById('modal-settings-nextcloud')); nextcloud_setting_modal = new bootstrap.Modal(document.getElementById("modal-settings-nextcloud"));
backup_setting_modal = new bootstrap.Modal(document.getElementById('modal-settings-backup')); backup_setting_modal = new bootstrap.Modal(document.getElementById("modal-settings-backup"));
setInterval(update_status, 500); setInterval(update_status, 500);
setInterval(updateLocalSnaps, 5000); setInterval(updateLocalSnaps, 5000);
listeners(); listeners();
}); });
function updateDynamicListeners() { function updateDynamicListeners() {
$('.local-snap-listener').on('click', function () { $(".local-snap-listener").on("click", function () {
let id = this.getAttribute('data-id'); let id = this.getAttribute("data-id");
console.log(id); console.log(id);
}); });
let manual_back_list = $('.manual-back-list'); let manual_back_list = $(".manual-back-list");
manual_back_list.off(); manual_back_list.off();
manual_back_list.on("click", function () { manual_back_list.on("click", function () {
let id = this.getAttribute('data-id'); let id = this.getAttribute("data-id");
let name = this.getAttribute('data-name'); let name = this.getAttribute("data-name");
manualBackup(id, name); manualBackup(id, name);
}) });
$('.restore').click(function () { $(".restore").click(function () {
let to_restore = this.getAttribute('data-id'); let to_restore = this.getAttribute("data-id");
console.log(to_restore) console.log(to_restore);
restore(to_restore) restore(to_restore);
});
})
} }
function updateLocalSnaps() { function updateLocalSnaps() {
let needUpdate = false; let needUpdate = false;
$.get('./api/formated-local-snap', (data) => { $.get("./api/formated-local-snap", (data) => {
if (JSON.stringify(data) === last_local_snap) if (JSON.stringify(data) === last_local_snap) return;
return;
last_local_snap = JSON.stringify(data); last_local_snap = JSON.stringify(data);
needUpdate = true; needUpdate = true;
let local_snaps = $('#local_snaps'); let local_snaps = $("#local_snaps");
local_snaps.empty(); local_snaps.empty();
local_snaps.html(data); local_snaps.html(data);
}).always(() => { }).always(() => {
@ -61,15 +58,13 @@ function updateLocalSnaps() {
function updateManuBackup(prevUpdate) { function updateManuBackup(prevUpdate) {
let needUpdate = false; let needUpdate = false;
$.get('./api/formated-backup-manual', (data) => { $.get("./api/formated-backup-manual", (data) => {
if (JSON.stringify(data) === last_manu_back) if (JSON.stringify(data) === last_manu_back) return;
return;
last_manu_back = JSON.stringify(data); last_manu_back = JSON.stringify(data);
needUpdate = true; needUpdate = true;
let manual_backups = $('#manual_backups') let manual_backups = $("#manual_backups");
manual_backups.empty(); manual_backups.empty();
manual_backups.html(data); manual_backups.html(data);
}).always(() => { }).always(() => {
updateAutoBackup(prevUpdate || needUpdate); updateAutoBackup(prevUpdate || needUpdate);
}); });
@ -77,157 +72,145 @@ function updateManuBackup(prevUpdate) {
function updateAutoBackup(prevUpdate) { function updateAutoBackup(prevUpdate) {
let needUpdate = false; let needUpdate = false;
$.get('./api/formated-backup-auto', (data) => { $.get("./api/formated-backup-auto", (data) => {
if (JSON.stringify(data) === last_auto_back) if (JSON.stringify(data) === last_auto_back) return;
return;
needUpdate = true; needUpdate = true;
last_auto_back = JSON.stringify(data); last_auto_back = JSON.stringify(data);
let auto_backups = $('#auto_backups') let auto_backups = $("#auto_backups");
auto_backups.empty(); auto_backups.empty();
auto_backups.html(data); auto_backups.html(data);
}).always(() => { }).always(() => {
if (prevUpdate || needUpdate) if (prevUpdate || needUpdate) updateDynamicListeners();
updateDynamicListeners();
}); });
} }
function update_status() { function update_status() {
$.get('./api/status', (data) => { $.get("./api/status", (data) => {
if (JSON.stringify(data) !== last_status) { if (JSON.stringify(data) !== last_status) {
last_status = JSON.stringify(data); last_status = JSON.stringify(data);
let buttons = $('#btn-backup-now, #btn-clean-now'); let buttons = $("#btn-backup-now, #btn-clean-now");
switch (data.status) { switch (data.status) {
case "error": case "error":
printStatus('Error', data.message); printStatus("Error", data.message);
buttons.removeClass("disabled"); buttons.removeClass("disabled");
break; break;
case "idle": case "idle":
printStatus('Idle', "Waiting for next backup."); printStatus("Idle", "Waiting for next backup.");
buttons.removeClass("disabled"); buttons.removeClass("disabled");
break; break;
case "download": case "download":
printStatusWithBar('Downloading Snapshot', data.progress); printStatusWithBar("Downloading Snapshot", data.progress);
buttons.addClass("disabled"); buttons.addClass("disabled");
break; break;
case "download-b": case "download-b":
printStatusWithBar('Downloading Backup', data.progress); printStatusWithBar("Downloading Backup", data.progress);
buttons.addClass("disabled"); buttons.addClass("disabled");
break; break;
case "upload": case "upload":
printStatusWithBar('Uploading Snapshot', data.progress); printStatusWithBar("Uploading Snapshot", data.progress);
buttons.addClass("disabled"); buttons.addClass("disabled");
break; break;
case "upload-b": case "upload-b":
printStatusWithBar('Uploading Snapshot', data.progress); printStatusWithBar("Uploading Snapshot", data.progress);
buttons.addClass("disabled"); buttons.addClass("disabled");
break; break;
case "creating": case "creating":
printStatusWithBar('Creating Snapshot', data.progress); printStatusWithBar("Creating Snapshot", data.progress);
buttons.addClass("disabled"); buttons.addClass("disabled");
break; break;
case "stopping": case "stopping":
printStatusWithBar('Stopping addons', data.progress); printStatusWithBar("Stopping addons", data.progress);
buttons.addClass("disabled"); buttons.addClass("disabled");
break; break;
case "starting": case "starting":
printStatusWithBar('Starting addons', data.progress); printStatusWithBar("Starting addons", data.progress);
buttons.addClass("disabled"); buttons.addClass("disabled");
break; break;
} }
if (data.last_backup != null) { if (data.last_backup != null) {
let last_back_status = $('#last_back_status'); let last_back_status = $("#last_back_status");
if (last_back_status.html() !== data.last_backup) if (last_back_status.html() !== data.last_backup) last_back_status.html(data.last_backup);
last_back_status.html(data.last_backup);
} }
if (data.next_backup != null) { if (data.next_backup != null) {
let next_back_status = $('#next_back_status'); let next_back_status = $("#next_back_status");
if (next_back_status.html() !== data.next_backup) if (next_back_status.html() !== data.next_backup) next_back_status.html(data.next_backup);
next_back_status.html(data.next_backup);
} }
} }
}); });
} }
function printStatus(status, secondLine) { function printStatus(status, secondLine) {
let status_jq = $('#status'); let status_jq = $("#status");
status_jq.empty(); status_jq.empty();
status_jq.html(status); status_jq.html(status);
let status_s_l_jq = $('#status-second-line'); let status_s_l_jq = $("#status-second-line");
status_s_l_jq.empty(); status_s_l_jq.empty();
status_s_l_jq.removeClass('text-center'); status_s_l_jq.removeClass("text-center");
status_s_l_jq.html(secondLine); status_s_l_jq.html(secondLine);
$('#progress').addClass("invisible"); $("#progress").addClass("invisible");
} }
function printStatusWithBar(status, progress) { function printStatusWithBar(status, progress) {
let status_jq = $('#status') let status_jq = $("#status");
status_jq.empty(); status_jq.empty();
status_jq.html(status); status_jq.html(status);
let secondLine = $('#status-second-line') let secondLine = $("#status-second-line");
secondLine.empty(); secondLine.empty();
secondLine.html(progress === -1 ? "Waiting..." : (Math.round(progress * 100) + "%")); secondLine.html(progress === -1 ? "Waiting..." : Math.round(progress * 100) + "%");
secondLine.addClass("text-center"); secondLine.addClass("text-center");
let progressDiv = $('#progress'); let progressDiv = $("#progress");
progressDiv.removeClass("invisible"); progressDiv.removeClass("invisible");
if (progress === -1) { if (progress === -1) {
progressDiv.children().css('width', "100%"); progressDiv.children().css("width", "100%");
progressDiv.children().addClass('progress-bar-striped progress-bar-animated'); progressDiv.children().addClass("progress-bar-striped progress-bar-animated");
} else { } else {
progressDiv.children().removeClass('progress-bar-striped progress-bar-animated'); progressDiv.children().removeClass("progress-bar-striped progress-bar-animated");
progressDiv.children().css('width', (progress * 100) + "%"); progressDiv.children().css("width", progress * 100 + "%");
} }
} }
function listeners() { function listeners() {
$('#btn-backup-now').on('click', backupNow); $("#btn-backup-now").on("click", backupNow);
$('#btn-clean-now').on('click',cleanNow); $("#btn-clean-now").on("click", cleanNow);
$('#trigger-backup-settings').on('click', getBackupSettings); $("#trigger-backup-settings").on("click", getBackupSettings);
$('#password_protected').on('change', function () { $("#password_protected").on("change", function () {
if (!$(this).is(':checked')) { if (!$(this).is(":checked")) {
$('#password_protect_value').parent().parent().addClass('d-none'); $("#password_protect_value").parent().parent().addClass("d-none");
} else { } else {
$('#password_protect_value').parent().parent().removeClass('d-none'); $("#password_protect_value").parent().parent().removeClass("d-none");
}
})
$('#cron-drop-settings').on('change', updateDropVisibility);
$('#save-backup-settings').click(sendBackupSettings);
$('#auto_clean_local').on('change', function () {
if (!$(this).is(':checked')) {
$('#local-snap-keep').parent().parent().addClass("d-none");
} else {
$('#local-snap-keep').parent().parent().removeClass("d-none");
} }
}); });
$('#auto_clean_backup').on('change', function () { $("#cron-drop-settings").on("change", updateDropVisibility);
if (!$(this).is(':checked')) { $("#save-backup-settings").click(sendBackupSettings);
$('#backup-snap-keep').parent().parent().addClass("d-none"); $("#auto_clean_local").on("change", function () {
if (!$(this).is(":checked")) {
$("#local-snap-keep").parent().parent().addClass("d-none");
} else { } else {
$('#backup-snap-keep').parent().parent().removeClass("d-none"); $("#local-snap-keep").parent().parent().removeClass("d-none");
}
});
$("#auto_clean_backup").on("change", function () {
if (!$(this).is(":checked")) {
$("#backup-snap-keep").parent().parent().addClass("d-none");
} else {
$("#backup-snap-keep").parent().parent().removeClass("d-none");
} }
}); });
$("#trigger-nextcloud-settings").click(getNextcloudSettings);
$('#trigger-nextcloud-settings').click(getNextcloudSettings); $("#save-nextcloud-settings").click(sendNextcloudSettings);
$('#save-nextcloud-settings').click(sendNextcloudSettings); $("#ssl").on("change", function () {
$('#ssl').on('change', function () { let div = $("#self_signed").parent().parent();
let div = $('#self_signed').parent().parent(); if ($("#ssl").is(":checked")) div.removeClass("invisible");
if ($('#ssl').is(':checked')) else div.addClass("invisible");
div.removeClass("invisible")
else
div.addClass("invisible");
}); });
} }
function restore(id) { function restore(id) {
loadingModal.show(); loadingModal.show();
$.post('./api/restore', { path: id }) $.post("./api/restore", { path: id })
.done(() => { .done(() => {
console.log("Restore cmd send !"); console.log("Restore cmd send !");
create_toast("success", "Command sent !", default_toast_timeout); create_toast("success", "Command sent !", default_toast_timeout);
@ -236,49 +219,48 @@ function restore(id) {
console.log(error); console.log(error);
create_toast("error", "Can't send command !", default_toast_timeout); create_toast("error", "Can't send command !", default_toast_timeout);
}) })
.always(() => loadingModal.hide()) .always(() => loadingModal.hide());
} }
function sendNextcloudSettings() { function sendNextcloudSettings() {
loadingModal.show(); loadingModal.show();
nextcloud_setting_modal.hide(); nextcloud_setting_modal.hide();
let ssl = $('#ssl').is(':checked') let ssl = $("#ssl").is(":checked");
let self_signed = $('#self_signed').is(':checked') let self_signed = $("#self_signed").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();
let back_dir = $('#back-dir').val(); let back_dir = $("#back-dir").val();
$.post('./api/nextcloud-settings', { $.post("./api/nextcloud-settings", {
ssl: ssl, ssl: ssl,
host: hostname, host: hostname,
username: username, username: username,
password: password, password: password,
back_dir: back_dir, back_dir: back_dir,
self_signed: self_signed self_signed: self_signed,
}) })
.done(() => { .done(() => {
console.log('Saved'); console.log("Saved");
$('#nextcloud_settings_message').parent().addClass("d-none"); $("#nextcloud_settings_message").parent().addClass("d-none");
create_toast("success", "Nextcloud settings saved !", default_toast_timeout); create_toast("success", "Nextcloud settings saved !", default_toast_timeout);
0 0;
}) })
.fail((data) => { .fail((data) => {
let nextcloud_settings_message = $('#nextcloud_settings_message') let nextcloud_settings_message = $("#nextcloud_settings_message");
if (data.status === 406) { if (data.status === 406) {
console.log(data.responseJSON.message); console.log(data.responseJSON.message);
nextcloud_settings_message.html(data.responseJSON.message); nextcloud_settings_message.html(data.responseJSON.message);
} else { } else {
nextcloud_settings_message.html("Invalid Settings."); nextcloud_settings_message.html("Invalid Settings.");
} }
nextcloud_settings_message.parent().removeClass("d-none"); nextcloud_settings_message.parent().removeClass("d-none");
nextcloud_setting_modal.show(); nextcloud_setting_modal.show();
create_toast("error", "Invalid Nextcloud settings !", default_toast_timeout); create_toast("error", "Invalid Nextcloud settings !", default_toast_timeout);
console.log('Fail'); console.log("Fail");
}).always(() => {
loadingModal.hide();
}) })
.always(() => {
loadingModal.hide();
});
} }
function manualBackup(id, name) { function manualBackup(id, name) {
@ -290,40 +272,40 @@ function manualBackup(id, name) {
.fail((error) => { .fail((error) => {
console.log(error); console.log(error);
create_toast("error", "Can't send command !", default_toast_timeout); create_toast("error", "Can't send command !", default_toast_timeout);
}) });
} }
function getNextcloudSettings() { function getNextcloudSettings() {
loadingModal.show(); loadingModal.show();
$.get('./api/nextcloud-settings', (data) => { $.get("./api/nextcloud-settings", (data) => {
$('#ssl').prop("checked", data.ssl === "true"); $("#ssl").prop("checked", data.ssl === "true");
let sef_signed_jq = $('#self_signed') let sef_signed_jq = $("#self_signed");
if (data.ssl === "true") { if (data.ssl === "true") {
let div = sef_signed_jq.parent().parent(); let div = sef_signed_jq.parent().parent();
div.removeClass("invisible"); div.removeClass("invisible");
} }
sef_signed_jq.prop('checked', data.self_signed === "true") sef_signed_jq.prop("checked", data.self_signed === "true");
$('#hostname').val(data.host); $("#hostname").val(data.host);
$('#username').val(data.username); $("#username").val(data.username);
$('#password').val(data.password); $("#password").val(data.password);
$('#back-dir').val(data.back_dir); $("#back-dir").val(data.back_dir);
nextcloud_setting_modal.show();
}).fail((error) => {
if (error.status === 404)
nextcloud_setting_modal.show(); nextcloud_setting_modal.show();
})
.fail((error) => {
if (error.status === 404) nextcloud_setting_modal.show();
else { else {
console.log(error); console.log(error);
create_toast("error", "Failed to fetch Nextcloud config !", default_toast_timeout); create_toast("error", "Failed to fetch Nextcloud config !", default_toast_timeout);
} }
}).always(() => { })
.always(() => {
loadingModal.hide(); loadingModal.hide();
}); });
} }
function backupNow() { function backupNow() {
loadingModal.show(); loadingModal.show();
$.post('./api/new-backup') $.post("./api/new-backup")
.done(() => { .done(() => {
create_toast("success", "Command sent !", default_toast_timeout); create_toast("success", "Command sent !", default_toast_timeout);
}) })
@ -333,12 +315,12 @@ function backupNow() {
}) })
.always(() => { .always(() => {
loadingModal.hide(); loadingModal.hide();
}) });
} }
function cleanNow() { function cleanNow() {
loadingModal.show(); loadingModal.show();
$.post('./api/clean-now') $.post("./api/clean-now")
.done(() => { .done(() => {
create_toast("success", "Command sent !", default_toast_timeout); create_toast("success", "Command sent !", default_toast_timeout);
}) })
@ -348,141 +330,158 @@ function cleanNow() {
}) })
.always(() => { .always(() => {
loadingModal.hide(); loadingModal.hide();
}) });
} }
function getBackupSettings() { function getBackupSettings() {
loadingModal.show(); loadingModal.show();
$.get('./api/backup-settings', (data) => { $.get("./api/backup-settings", (data) => {
if (JSON.stringify(data) === "{}") { if (JSON.stringify(data) === "{}") {
data = { data = {
settings: {
cron_base: "0", cron_base: "0",
cron_hour: "00:00", cron_hour: "00:00",
cron_weekday: "0", cron_weekday: "0",
cron_month_day: "1", cron_month_day: "1",
cron_custom: "5 4 * * *",
auto_clean_local: false, auto_clean_local: false,
auto_clean_local_keep: 5, auto_clean_local_keep: 5,
auto_clean_backup: false, auto_clean_backup: false,
auto_clean_backup_keep: 5, auto_clean_backup_keep: 5,
} }
};
}
if (data.settings.cron_custom == null || data.settings.cron_custom == "") {
data.settings.cron_custom = "5 4 * * *";
} }
$('#cron-drop-settings').val(data.settings.cron_base); $("#cron-drop-settings").val(data.settings.cron_base);
$('#name-template').val(data.settings.name_template); $("#name-template").val(data.settings.name_template);
$("#timepicker").val(data.settings.cron_hour);
$("#cron-drop-day-month-read").val(data.settings.cron_month_day);
$("#cron-drop-day-month").val(data.settings.cron_month_day);
$("#cron-drop-custom").val(data.settings.cron_custom);
$('#timepicker').val(data.settings.cron_hour); $("#auto_clean_local").prop("checked", data.settings.auto_clean_local === "true");
$('#cron-drop-day-month-read').val(data.settings.cron_month_day); let local_snap_keep = $("#local-snap-keep");
$('#cron-drop-day-month').val(data.settings.cron_month_day); if (data.settings.auto_clean_local === "true") local_snap_keep.parent().parent().removeClass("d-none");
else local_snap_keep.parent().parent().addClass("d-none");
$('#auto_clean_local').prop('checked', data.settings.auto_clean_local === "true");
let local_snap_keep = $('#local-snap-keep');
if (data.settings.auto_clean_local === "true")
local_snap_keep.parent().parent().removeClass("d-none");
else
local_snap_keep.parent().parent().addClass("d-none");
local_snap_keep.val(data.settings.auto_clean_local_keep); local_snap_keep.val(data.settings.auto_clean_local_keep);
$('#auto_clean_backup').prop('checked', data.settings.auto_clean_backup === "true"); $("#auto_clean_backup").prop("checked", data.settings.auto_clean_backup === "true");
let backup_snap_keep = $('#backup-snap-keep'); let backup_snap_keep = $("#backup-snap-keep");
if (data.settings.auto_clean_backup === "true") if (data.settings.auto_clean_backup === "true") backup_snap_keep.parent().parent().removeClass("d-none");
backup_snap_keep.parent().parent().removeClass("d-none"); else backup_snap_keep.parent().parent().addClass("d-none");
else
backup_snap_keep.parent().parent().addClass("d-none");
backup_snap_keep.val(data.settings.auto_clean_backup_keep); backup_snap_keep.val(data.settings.auto_clean_backup_keep);
$('#cron-drop-day').val(data.settings.cron_weekday); $("#cron-drop-day").val(data.settings.cron_weekday);
let folder_html = "" let folder_html = "";
for (let thisFolder of data.folders) { for (let thisFolder of data.folders) {
let exclude = data.settings.exclude_folder.includes(thisFolder.slug); let exclude = data.settings.exclude_folder.includes(thisFolder.slug);
folder_html += `<li class="list-group-item"><div class="form-check"><input class="form-check-input folders-box" type="checkbox" id="${thisFolder.slug}" ${exclude ? "" : "checked"}><label class="form-label mb-0" for="${thisFolder.slug}">${thisFolder.name}</label></div></li>` folder_html += `<li class="list-group-item"><div class="form-check"><input class="form-check-input folders-box" type="checkbox" id="${
thisFolder.slug
}" ${exclude ? "" : "checked"}><label class="form-label mb-0" for="${thisFolder.slug}">${thisFolder.name}</label></div></li>`;
} }
$("#folders-div").html(folder_html); $("#folders-div").html(folder_html);
let addons_html = "" let addons_html = "";
for (let thisAddon of data.addonList) { for (let thisAddon of data.addonList) {
let exclude = data.settings.exclude_addon.includes(thisAddon.slug); let exclude = data.settings.exclude_addon.includes(thisAddon.slug);
addons_html += `<li class="list-group-item"><div class="form-check"><input class="form-check-input addons-box" type="checkbox" id="${thisAddon.slug}" ${exclude ? "" : "checked"}><label class="form-label mb-0" for="${thisAddon.slug}">${thisAddon.name}</label></div></li>` addons_html += `<li class="list-group-item"><div class="form-check"><input class="form-check-input addons-box" type="checkbox" id="${
thisAddon.slug
}" ${exclude ? "" : "checked"}><label class="form-label mb-0" for="${thisAddon.slug}">${thisAddon.name}</label></div></li>`;
} }
$("#addons-div").html(addons_html); $("#addons-div").html(addons_html);
let addons_stop_html = "" let addons_stop_html = "";
for (let thisAddon of data.addonList) { for (let thisAddon of data.addonList) {
if (thisAddon.slug !== "229cc4d7_nextcloud_backup"){ if (thisAddon.slug !== "229cc4d7_nextcloud_backup") {
let on = data.settings.auto_stop_addon.includes(thisAddon.slug); let on = data.settings.auto_stop_addon.includes(thisAddon.slug);
addons_stop_html += `<li class="list-group-item"><div class="form-check"><input class="form-check-input stop-addons-box" type="checkbox" id="${thisAddon.slug}" ${on ? "checked" : ""}><label class="form-label mb-0" for="${thisAddon.slug}">${thisAddon.name}</label></div></li>` addons_stop_html += `<li class="list-group-item"><div class="form-check"><input class="form-check-input stop-addons-box" type="checkbox" id="${
thisAddon.slug
}" ${on ? "checked" : ""}><label class="form-label mb-0" for="${thisAddon.slug}">${thisAddon.name}</label></div></li>`;
} }
} }
$("#auto-stop-addons-div").html(addons_stop_html); $("#auto-stop-addons-div").html(addons_stop_html);
updateDropVisibility(); updateDropVisibility();
backup_setting_modal.show(); backup_setting_modal.show();
})
.fail((error) => {
}).fail((error) => {
console.log(error); console.log(error);
create_toast("error", "Failed to fetch Nextcloud config !", default_toast_timeout); create_toast("error", "Failed to fetch Nextcloud config !", default_toast_timeout);
}).always(() => { })
.always(() => {
loadingModal.hide(); loadingModal.hide();
}); });
} }
function updateDropVisibility() { function updateDropVisibility() {
let cronBase = $("#cron-drop-settings").val(); let cronBase = $("#cron-drop-settings").val();
let timepicker = $('#timepicker'); let timepicker = $("#timepicker");
let cron_drop_day = $('#cron-drop-day'); let cron_drop_day = $("#cron-drop-day");
let cron_drop_day_mount = $('#cron-drop-day-month'); let cron_drop_day_mount = $("#cron-drop-day-month");
let cron_drop_custom = $("#cron-drop-custom");
switch (cronBase) { switch (cronBase) {
case "4":
timepicker.parent().parent().addClass("d-none");
cron_drop_day.parent().parent().addClass("d-none");
cron_drop_day_mount.parent().parent().addClass("d-none");
cron_drop_custom.parent().parent().removeClass("d-none");
break;
case "3": case "3":
timepicker.parent().parent().removeClass("d-none"); timepicker.parent().parent().removeClass("d-none");
cron_drop_day.parent().parent().addClass("d-none"); cron_drop_day.parent().parent().addClass("d-none");
cron_drop_day_mount.parent().parent().removeClass("d-none"); cron_drop_day_mount.parent().parent().removeClass("d-none");
cron_drop_custom.parent().parent().addClass("d-none");
break; break;
case "2": case "2":
timepicker.parent().parent().removeClass("d-none"); timepicker.parent().parent().removeClass("d-none");
cron_drop_day.parent().parent().removeClass("d-none"); cron_drop_day.parent().parent().removeClass("d-none");
cron_drop_day_mount.parent().parent().addClass("d-none"); cron_drop_day_mount.parent().parent().addClass("d-none");
cron_drop_custom.parent().parent().addClass("d-none");
break; break;
case "1": case "1":
timepicker.parent().parent().removeClass("d-none"); timepicker.parent().parent().removeClass("d-none");
cron_drop_day.parent().parent().addClass("d-none"); cron_drop_day.parent().parent().addClass("d-none");
cron_drop_day_mount.parent().parent().addClass("d-none"); cron_drop_day_mount.parent().parent().addClass("d-none");
cron_drop_custom.parent().parent().addClass("d-none");
break; break;
case "0": case "0":
timepicker.parent().parent().addClass("d-none"); timepicker.parent().parent().addClass("d-none");
cron_drop_day.parent().parent().addClass("d-none"); cron_drop_day.parent().parent().addClass("d-none");
cron_drop_day_mount.parent().parent().addClass("d-none"); cron_drop_day_mount.parent().parent().addClass("d-none");
cron_drop_custom.parent().parent().addClass("d-none");
break; break;
} }
} }
function sendBackupSettings() { function sendBackupSettings() {
let cron_month_day = $('#cron-drop-day-month').val(); let cron_month_day = $("#cron-drop-day-month").val();
let cron_weekday = $('#cron-drop-day').val(); let cron_weekday = $("#cron-drop-day").val();
let cron_hour = $('#timepicker').val(); let cron_hour = $("#timepicker").val();
let cron_base = $('#cron-drop-settings').val(); let cron_custom = $("#cron-drop-custom").val();
let auto_clean_local = $("#auto_clean_local").is(':checked'); let cron_base = $("#cron-drop-settings").val();
let auto_clean_backup = $("#auto_clean_backup").is(':checked'); let auto_clean_local = $("#auto_clean_local").is(":checked");
let auto_clean_backup = $("#auto_clean_backup").is(":checked");
let auto_clean_local_keep = $("#local-snap-keep").val(); let auto_clean_local_keep = $("#local-snap-keep").val();
let auto_clean_backup_keep = $("#backup-snap-keep").val(); let auto_clean_backup_keep = $("#backup-snap-keep").val();
let name_template = $('#name-template').val(); let name_template = $("#name-template").val();
let excluded_folders_nodes = document.querySelectorAll('.folders-box:not(:checked)'); let excluded_folders_nodes = document.querySelectorAll(".folders-box:not(:checked)");
let exclude_folder = [""]; let exclude_folder = [""];
let password_protected = $('#password_protected').is(':checked'); let password_protected = $("#password_protected").is(":checked");
let password_protect_value = $('#password_protect_value').val(); let password_protect_value = $("#password_protect_value").val();
for (let i of excluded_folders_nodes) { for (let i of excluded_folders_nodes) {
exclude_folder.push(i.id); exclude_folder.push(i.id);
} }
let excluded_addons_nodes = document.querySelectorAll('.addons-box:not(:checked)'); let excluded_addons_nodes = document.querySelectorAll(".addons-box:not(:checked)");
let exclude_addon = [""]; let exclude_addon = [""];
for (let i of excluded_addons_nodes) { for (let i of excluded_addons_nodes) {
exclude_addon.push(i.id); exclude_addon.push(i.id);
} }
let stop_addons_nodes = document.querySelectorAll('.stop-addons-box:checked'); let stop_addons_nodes = document.querySelectorAll(".stop-addons-box:checked");
let stop_addon = [""]; let stop_addon = [""];
for (let i of stop_addons_nodes) { for (let i of stop_addons_nodes) {
stop_addon.push(i.id); stop_addon.push(i.id);
@ -490,13 +489,13 @@ function sendBackupSettings() {
loadingModal.show(); loadingModal.show();
backup_setting_modal.hide(); backup_setting_modal.hide();
$.post('./api/backup-settings', $.post("./api/backup-settings", {
{
name_template: name_template, name_template: name_template,
cron_base: cron_base, cron_base: cron_base,
cron_hour: cron_hour, cron_hour: cron_hour,
cron_weekday: cron_weekday, cron_weekday: cron_weekday,
cron_month_day: cron_month_day, cron_month_day: cron_month_day,
cron_custom: cron_custom,
auto_clean_local: auto_clean_local, auto_clean_local: auto_clean_local,
auto_clean_local_keep: auto_clean_local_keep, auto_clean_local_keep: auto_clean_local_keep,
auto_clean_backup: auto_clean_backup, auto_clean_backup: auto_clean_backup,
@ -505,14 +504,14 @@ function sendBackupSettings() {
exclude_folder: exclude_folder, exclude_folder: exclude_folder,
auto_stop_addon: stop_addon, auto_stop_addon: stop_addon,
password_protected: password_protected, password_protected: password_protected,
password_protect_value: password_protect_value password_protect_value: password_protect_value,
}) })
.done(() => { .done(() => {
create_toast("success", "Backup settings saved !", default_toast_timeout); create_toast("success", "Backup settings saved !", default_toast_timeout);
}) })
.fail(() => { .fail((data) => {
create_toast("error", "Can't save backup settings !", default_toast_timeout); debugger
create_toast("error", `Can't save backup settings ! <br> Error: ${data.responseText}`, default_toast_timeout);
backup_setting_modal.show(); backup_setting_modal.show();
}) })
.always(() => { .always(() => {

View File

@ -188,13 +188,14 @@ router.get("/backup-settings", function (req, res, next) {
}); });
router.post("/backup-settings", function (req, res, next) { router.post("/backup-settings", function (req, res, next) {
if (settingsTools.check(req.body)) { let [result, message] = settingsTools.check(req.body)
if (result) {
settingsTools.setSettings(req.body); settingsTools.setSettings(req.body);
cronTools.startCron(); cronTools.startCron();
res.send(); res.send();
} else { } else {
res.status(400); res.status(400);
res.send(); res.send(message);
} }
}); });

View File

@ -1,6 +1,6 @@
const fs = require("fs"); const fs = require("fs");
const logger = require("../config/winston"); const logger = require("../config/winston");
const moment = require('moment') const moment = require("moment");
const settingsPath = "/data/backup_conf.json"; const settingsPath = "/data/backup_conf.json";
@ -26,143 +26,140 @@ function check_cron(conf) {
return false; return false;
} }
function check(conf, fallback = false) { function check(conf, fallback = false) {
let needSave = false; let needSave = false;
if (!check_cron(conf)) { if (!check_cron(conf)) {
if (fallback) { if (fallback) {
logger.warn("Bad value for cron settings, fallback to default ") logger.warn("Bad value for cron settings, fallback to default ");
conf.cron_base = "0"; conf.cron_base = "0";
conf.cron_hour = "00:00", conf.cron_hour = "00:00";
conf.cron_weekday = "0", conf.cron_weekday = "0";
conf.cron_month_day = "1" conf.cron_month_day = "1";
} else { } else {
logger.error("Bad value for cron settings") logger.error("Bad value for cron settings");
return false; return [false, "Bad cron settings"];
} }
} }
if (conf.name_template == null) { if (conf.name_template == null) {
if (fallback) { if (fallback) {
logger.warn("Bad value for 'name_template', fallback to default ") logger.warn("Bad value for 'name_template', fallback to default ");
conf.name_template = "{type}-{ha_version}-{date}_{hour}" conf.name_template = "{type}-{ha_version}-{date}_{hour}";
} else { } else {
logger.error("Bad value for 'name_template'") logger.error("Bad value for 'name_template'");
return false; return [false, "Bad value for 'name_template'"];
} }
} }
if (conf.auto_clean_local_keep == null || !/^\d+$/.test(conf.auto_clean_local_keep)) { if (conf.auto_clean_local_keep == null || !/^\d+$/.test(conf.auto_clean_local_keep)) {
if (fallback) { if (fallback) {
logger.warn("Bad value for 'auto_clean_local_keep', fallback to 5 ") logger.warn("Bad value for 'auto_clean_local_keep', fallback to 5 ");
conf.auto_clean_local_keep = "5" conf.auto_clean_local_keep = "5";
} else { } else {
logger.error("Bad value for 'auto_clean_local_keep'") logger.error("Bad value for 'auto_clean_local_keep'");
return false; return [false, "Bad value for 'auto_clean_local_keep'"];
} }
} }
if (conf.auto_clean_backup_keep == null || !/^\d+$/.test(conf.auto_clean_backup_keep)) { if (conf.auto_clean_backup_keep == null || !/^\d+$/.test(conf.auto_clean_backup_keep)) {
if (fallback) { if (fallback) {
logger.warn("Bad value for 'auto_clean_backup_keep', fallback to 5 ") logger.warn("Bad value for 'auto_clean_backup_keep', fallback to 5 ");
conf.auto_clean_backup_keep = "5" conf.auto_clean_backup_keep = "5";
} else { } else {
logger.error("Bad value for 'auto_clean_backup_keep'") logger.error("Bad value for 'auto_clean_backup_keep'");
return false; return [false, "Bad value for 'auto_clean_backup_keep'"];
} }
} }
if (conf.auto_clean_local == null) { if (conf.auto_clean_local == null) {
if (fallback) { if (fallback) {
logger.warn("Bad value for 'auto_clean_local', fallback to false ") logger.warn("Bad value for 'auto_clean_local', fallback to false ");
conf.auto_clean_local = "false" conf.auto_clean_local = "false";
} else { } else {
logger.error("Bad value for 'auto_clean_local'") logger.error("Bad value for 'auto_clean_local'");
return false; return [false, "Bad value for 'auto_clean_local'"];
} }
} }
if (conf.auto_clean_backup == null) { if (conf.auto_clean_backup == null) {
if (fallback) { if (fallback) {
logger.warn("Bad value for 'auto_clean_backup', fallback to false ") logger.warn("Bad value for 'auto_clean_backup', fallback to false ");
conf.auto_clean_backup = "false" conf.auto_clean_backup = "false";
} else { } else {
logger.error("Bad value for 'auto_clean_backup'") logger.error("Bad value for 'auto_clean_backup'");
return false; return [false, "Bad value for 'auto_clean_backup'"];
} }
} }
if (conf.exclude_addon == null) { if (conf.exclude_addon == null) {
if (fallback) { if (fallback) {
logger.warn("Bad value for 'exclude_addon', fallback to [] ") logger.warn("Bad value for 'exclude_addon', fallback to [] ");
conf.exclude_addon = [] conf.exclude_addon = [];
} else { } else {
logger.error("Bad value for 'exclude_addon'") logger.error("Bad value for 'exclude_addon'");
return false; return [false, "Bad value for 'exclude_addon'"];
} }
} }
if (conf.exclude_folder == null) { if (conf.exclude_folder == null) {
if (fallback) { if (fallback) {
logger.warn("Bad value for 'exclude_folder', fallback to [] ") logger.warn("Bad value for 'exclude_folder', fallback to [] ");
conf.exclude_folder = [] conf.exclude_folder = [];
} else { } else {
logger.error("Bad value for 'exclude_folder'") logger.error("Bad value for 'exclude_folder'");
return false; return [false, "Bad value for 'exclude_folder'"];
} }
} }
if (conf.auto_stop_addon == null) { if (conf.auto_stop_addon == null) {
if (fallback) { if (fallback) {
logger.warn("Bad value for 'auto_stop_addon', fallback to [] ") logger.warn("Bad value for 'auto_stop_addon', fallback to [] ");
conf.auto_stop_addon = [] conf.auto_stop_addon = [];
} else { } else {
logger.error("Bad value for 'auto_stop_addon'") logger.error("Bad value for 'auto_stop_addon'");
return false; return [false, "Bad value for 'auto_stop_addon'"];
} }
} }
if (!Array.isArray(conf.exclude_folder)) { if (!Array.isArray(conf.exclude_folder)) {
logger.debug("exclude_folder is not array (Empty value), reset..."); logger.debug("exclude_folder is not array (Empty value), reset...");
conf.exclude_folder = [] conf.exclude_folder = [];
needSave = true; needSave = true;
} }
if (!Array.isArray(conf.exclude_addon)) { if (!Array.isArray(conf.exclude_addon)) {
logger.debug("exclude_addon is not array (Empty value), reset..."); logger.debug("exclude_addon is not array (Empty value), reset...");
conf.exclude_addon = [] conf.exclude_addon = [];
needSave = true; needSave = true;
} }
if(conf.password_protected == null){ if (conf.password_protected == null) {
if (fallback) { if (fallback) {
logger.warn("Bad value for 'password_protected', fallback to false "); logger.warn("Bad value for 'password_protected', fallback to false ");
conf.password_protected = 'false'; conf.password_protected = "false";
} else { } else {
logger.error("Bad value for 'password_protect_value'"); logger.error("Bad value for 'password_protect_value'");
return false; return [false, "Bad value for 'password_protect_value'"];
} }
} }
if(conf.password_protect_value == null){ if (conf.password_protect_value == null) {
if (fallback) { if (fallback) {
logger.warn("Bad value for 'password_protect_value', fallback to '' "); logger.warn("Bad value for 'password_protect_value', fallback to '' ");
conf.password_protect_value = ''; conf.password_protect_value = "";
} else { } else {
logger.error("Bad value for 'password_protect_value'"); logger.error("Bad value for 'password_protect_value'");
return false; return [false, "Bad value for 'password_protect_value'"];
} }
} }
if (fallback || needSave) { if (fallback || needSave) {
setSettings(conf); setSettings(conf);
} }
return true return [true, null];
} }
function getFormatedName(is_manual, ha_version) { function getFormatedName(is_manual, ha_version) {
let setting = getSettings(); let setting = getSettings();
let template = setting.name_template; let template = setting.name_template;
template = template.replace('{type_low}', is_manual ? 'manual' : 'auto'); template = template.replace("{type_low}", is_manual ? "manual" : "auto");
template = template.replace('{type}', is_manual ? 'Manual' : 'Auto'); template = template.replace("{type}", is_manual ? "Manual" : "Auto");
template = template.replace('{ha_version}', ha_version); template = template.replace("{ha_version}", ha_version);
let mmt = moment() let mmt = moment();
template = template.replace('{hour_12}', mmt.format('hhmmA')); template = template.replace("{hour_12}", mmt.format("hhmmA"));
template = template.replace('{hour}', mmt.format('HHmm')); template = template.replace("{hour}", mmt.format("HHmm"));
template = template.replace('{date}', mmt.format('YYYY-MM-DD')); template = template.replace("{date}", mmt.format("YYYY-MM-DD"));
return template return template;
} }
function getSettings() { function getSettings() {