fix(backup-display): Fix parsing of rsync target in system-backup.html, fixes #2206

This commit is contained in:
Hugh Secker-Walker 2022-11-21 18:22:35 +00:00 committed by Hugh Secker-Walker
parent 3314c4f7de
commit 97e1f39aec
1 changed files with 32 additions and 6 deletions

View File

@ -259,12 +259,11 @@ function show_custom_backup() {
} else if (r.target == "off") { } else if (r.target == "off") {
$("#backup-target-type").val("off"); $("#backup-target-type").val("off");
} else if (r.target.substring(0, 8) == "rsync://") { } else if (r.target.substring(0, 8) == "rsync://") {
$("#backup-target-type").val("rsync"); const spec = url_split(r.target);
var path = r.target.substring(8).split('//'); $("#backup-target-type").val(spec.scheme);
var host_parts = path.shift().split('@'); $("#backup-target-rsync-user").val(spec.user);
$("#backup-target-rsync-user").val(host_parts[0]); $("#backup-target-rsync-host").val(spec.host);
$("#backup-target-rsync-host").val(host_parts[1]); $("#backup-target-rsync-path").val(spec.path);
$("#backup-target-rsync-path").val('/'+path[0]);
} else if (r.target.substring(0, 5) == "s3://") { } else if (r.target.substring(0, 5) == "s3://") {
$("#backup-target-type").val("s3"); $("#backup-target-type").val("s3");
var hostpath = r.target.substring(5).split('/'); var hostpath = r.target.substring(5).split('/');
@ -344,4 +343,31 @@ function init_inputs(target_type) {
set_host($('#backup-target-s3-host-select').val()); set_host($('#backup-target-s3-host-select').val());
} }
} }
// Return a two-element array of the substring preceding and the substring following
// the first occurence of separator in string. Return [undefined, string] if the
// separator does not appear in string.
const split1_rest = (string, separator) => {
const index = string.indexOf(separator);
return (index >= 0) ? [string.substring(0, index), string.substring(index + separator.length)] : [undefined, string];
};
// Note: The manifest JS URL class does not work in some security-conscious
// settings, e.g. Brave browser, so we roll our own that handles only what we need.
//
// Use greedy separator parsing to get parts of a MIAB backup target url.
// Note: path will not include a leading forward slash '/'
const url_split = url => {
const [ scheme, scheme_rest ] = split1_rest(url, '://');
const [ user, user_rest ] = split1_rest(scheme_rest, '@');
const [ host, path ] = split1_rest(user_rest, '/');
return {
scheme,
user,
host,
path,
}
};
</script> </script>