Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use more modern method to download definitions #6674

Merged
merged 1 commit into from
Dec 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 40 additions & 16 deletions deps/rabbitmq_management/priv/www/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,21 +657,47 @@ function postprocess() {
});

$('#download-definitions').on('click', function() {
var idx = $("select[name='vhost-download'] option:selected").index();
var vhost = ((idx <=0 ) ? "" : "/" + esc($("select[name='vhost-download'] option:selected").val()));
if (oauth.enabled) {
var path = 'api/definitions' + vhost + '?download=' +
esc($('#download-filename').val()) +
'&token=' + oauth.access_token;
// https://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post/23797348
// https://gist.github.com/zynick/12bae6dbc76f6aacedf0/
var idx = $("select[name='vhost-download'] option:selected").index();
var vhost = ((idx <=0 ) ? "" : "/" + esc($("select[name='vhost-download'] option:selected").val()));
var download_filename = esc($('#download-filename').val());
var path = 'api/definitions' + vhost + '?download=' + download_filename;
var req = xmlHttpRequest();
req.open('GET', path, true);
req.setRequestHeader('authorization', auth_header());
req.responseType = 'blob';
req.onload = function (_event) {
if (this.status >= 200 && this.status <= 299) {
var type = req.getResponseHeader('Content-Type');
var blob = new Blob([this.response], { type: type });
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(blob, download_filename);
} else {
var URL = window.URL || window.webkitURL;
var downloadUrl = URL.createObjectURL(blob);
var a = document.createElement("a");
if (typeof a.download === 'undefined') {
window.location = downloadUrl;
} else {
a.href = downloadUrl;
a.download = download_filename;
document.body.appendChild(a);
a.click();
}
var cleanup = function () {
URL.revokeObjectURL(downloadUrl);
document.body.removeChild(a);
};
setTimeout(cleanup, 1000);
}
} else {
var path = 'api/definitions' + vhost + '?download=' +
esc($('#download-filename').val()) +
'&auth=' + get_cookie_value('auth');
};
window.location = path;
setTimeout('app.run()');
return false;
});
// Unsuccessful status
show_popup('warn', 'Error downloading definitions');
}
};
req.send();
});

$('.update-manual').on('click', function() {
update_manual($(this).attr('for'), $(this).attr('query'));
Expand Down Expand Up @@ -1296,8 +1322,6 @@ function sync_req(type, params0, path_template, options) {
}
}



try {
if (type == 'GET')
req.send(null);
Expand Down