Skip to content

Commit

Permalink
Merge pull request #6677 from rabbitmq/mergify/bp/v3.11.x/pr-6674
Browse files Browse the repository at this point in the history
Use more modern method to download definitions (backport #6674)
  • Loading branch information
michaelklishin committed Dec 14, 2022
2 parents c66a185 + e2c724b commit ba619a6
Showing 1 changed file with 40 additions and 16 deletions.
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

0 comments on commit ba619a6

Please sign in to comment.