From a8f4699816ac7fbacef8f014149fbe6a12923e06 Mon Sep 17 00:00:00 2001 From: Rahul Somasundaram Date: Mon, 23 Aug 2021 15:47:03 +0530 Subject: [PATCH] [frontend] certificate download changes --- frontend/js/app/api.js | 57 +++++++++++++++++++ .../js/app/nginx/certificates/list/item.ejs | 1 + .../js/app/nginx/certificates/list/item.js | 7 ++- frontend/js/i18n/messages.json | 1 + 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/frontend/js/app/api.js b/frontend/js/app/api.js index 9d11d268..2e0bbcae 100644 --- a/frontend/js/app/api.js +++ b/frontend/js/app/api.js @@ -152,6 +152,54 @@ function FileUpload(path, fd) { }); } +//ref : https://codepen.io/chrisdpratt/pen/RKxJNo +function DownloadFile(verb, path, filename) { + return new Promise(function (resolve, reject) { + let api_url = '/api/'; + let url = api_url + path; + let token = Tokens.getTopToken(); + + $.ajax({ + url: url, + type: verb, + crossDomain: true, + xhrFields: { + withCredentials: true, + responseType: 'blob' + }, + + beforeSend: function (xhr) { + xhr.setRequestHeader('Authorization', 'Bearer ' + (token ? token.t : null)); + }, + + success: function (data) { + console.log(data) + console.log(textStatus) + console.log(response) + var a = document.createElement('a'); + var url = window.URL.createObjectURL(data); + a.href = url; + a.download = filename; + document.body.append(a); + a.click(); + a.remove(); + window.URL.revokeObjectURL(url); + }, + + error: function (xhr, status, error_thrown) { + let code = 400; + + if (typeof xhr.responseJSON !== 'undefined' && typeof xhr.responseJSON.error !== 'undefined' && typeof xhr.responseJSON.error.message !== 'undefined') { + error_thrown = xhr.responseJSON.error.message; + code = xhr.responseJSON.error.code || 500; + } + + reject(new ApiError(error_thrown, xhr.responseText, code)); + } + }); + }); +} + module.exports = { status: function () { return fetch('get', ''); @@ -638,6 +686,15 @@ module.exports = { */ renew: function (id, timeout = 180000) { return fetch('post', 'nginx/certificates/' + id + '/renew', undefined, {timeout}); + }, + + /** + * @param {Number} id + * @returns {Promise} + */ + download: function (id) { + console.log("downloading") + return DownloadFile('get', "nginx/certificates/" + id + "/download", "certificate.zip") } } }, diff --git a/frontend/js/app/nginx/certificates/list/item.ejs b/frontend/js/app/nginx/certificates/list/item.ejs index 87930dce..85b56eb0 100644 --- a/frontend/js/app/nginx/certificates/list/item.ejs +++ b/frontend/js/app/nginx/certificates/list/item.ejs @@ -41,6 +41,7 @@ <%- i18n('audit-log', 'certificate') %> #<%- id %> <% if (provider === 'letsencrypt') { %> <%- i18n('certificates', 'force-renew') %> + <%- i18n('certificates', 'download') %> <% } %> <%- i18n('str', 'delete') %> diff --git a/frontend/js/app/nginx/certificates/list/item.js b/frontend/js/app/nginx/certificates/list/item.js index c967fdb8..7c2530dd 100644 --- a/frontend/js/app/nginx/certificates/list/item.js +++ b/frontend/js/app/nginx/certificates/list/item.js @@ -11,7 +11,8 @@ module.exports = Mn.View.extend({ ui: { host_link: '.host-link', renew: 'a.renew', - delete: 'a.delete' + delete: 'a.delete', + download: 'a.download' }, events: { @@ -29,6 +30,10 @@ module.exports = Mn.View.extend({ e.preventDefault(); let win = window.open($(e.currentTarget).attr('rel'), '_blank'); win.focus(); + }, + 'click @ui.download': function (e) { + e.preventDefault(); + App.Api.Nginx.Certificates.download(this.model.get('id')) } }, diff --git a/frontend/js/i18n/messages.json b/frontend/js/i18n/messages.json index 6962a4db..4b486818 100644 --- a/frontend/js/i18n/messages.json +++ b/frontend/js/i18n/messages.json @@ -188,6 +188,7 @@ "other-certificate-key": "Certificate Key", "other-intermediate-certificate": "Intermediate Certificate", "force-renew": "Renew Now", + "download":"Download", "renew-title": "Renew Let'sEncrypt Certificate" }, "access-lists": {