Skip to content

Commit a8f4699

Browse files
committed
[frontend] certificate download changes
1 parent ac3df6d commit a8f4699

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

frontend/js/app/api.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,54 @@ function FileUpload(path, fd) {
152152
});
153153
}
154154

155+
//ref : https://codepen.io/chrisdpratt/pen/RKxJNo
156+
function DownloadFile(verb, path, filename) {
157+
return new Promise(function (resolve, reject) {
158+
let api_url = '/api/';
159+
let url = api_url + path;
160+
let token = Tokens.getTopToken();
161+
162+
$.ajax({
163+
url: url,
164+
type: verb,
165+
crossDomain: true,
166+
xhrFields: {
167+
withCredentials: true,
168+
responseType: 'blob'
169+
},
170+
171+
beforeSend: function (xhr) {
172+
xhr.setRequestHeader('Authorization', 'Bearer ' + (token ? token.t : null));
173+
},
174+
175+
success: function (data) {
176+
console.log(data)
177+
console.log(textStatus)
178+
console.log(response)
179+
var a = document.createElement('a');
180+
var url = window.URL.createObjectURL(data);
181+
a.href = url;
182+
a.download = filename;
183+
document.body.append(a);
184+
a.click();
185+
a.remove();
186+
window.URL.revokeObjectURL(url);
187+
},
188+
189+
error: function (xhr, status, error_thrown) {
190+
let code = 400;
191+
192+
if (typeof xhr.responseJSON !== 'undefined' && typeof xhr.responseJSON.error !== 'undefined' && typeof xhr.responseJSON.error.message !== 'undefined') {
193+
error_thrown = xhr.responseJSON.error.message;
194+
code = xhr.responseJSON.error.code || 500;
195+
}
196+
197+
reject(new ApiError(error_thrown, xhr.responseText, code));
198+
}
199+
});
200+
});
201+
}
202+
155203
module.exports = {
156204
status: function () {
157205
return fetch('get', '');
@@ -638,6 +686,15 @@ module.exports = {
638686
*/
639687
renew: function (id, timeout = 180000) {
640688
return fetch('post', 'nginx/certificates/' + id + '/renew', undefined, {timeout});
689+
},
690+
691+
/**
692+
* @param {Number} id
693+
* @returns {Promise}
694+
*/
695+
download: function (id) {
696+
console.log("downloading")
697+
return DownloadFile('get', "nginx/certificates/" + id + "/download", "certificate.zip")
641698
}
642699
}
643700
},

frontend/js/app/nginx/certificates/list/item.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<span class="dropdown-header"><%- i18n('audit-log', 'certificate') %> #<%- id %></span>
4242
<% if (provider === 'letsencrypt') { %>
4343
<a href="#" class="renew dropdown-item"><i class="dropdown-icon fe fe-refresh-cw"></i> <%- i18n('certificates', 'force-renew') %></a>
44+
<a href="#" class="download dropdown-item"><i class="dropdown-icon fe fe-download"></i><%- i18n('certificates', 'download') %></a>
4445
<div class="dropdown-divider"></div>
4546
<% } %>
4647
<a href="#" class="delete dropdown-item"><i class="dropdown-icon fe fe-trash-2"></i> <%- i18n('str', 'delete') %></a>

frontend/js/app/nginx/certificates/list/item.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ module.exports = Mn.View.extend({
1111
ui: {
1212
host_link: '.host-link',
1313
renew: 'a.renew',
14-
delete: 'a.delete'
14+
delete: 'a.delete',
15+
download: 'a.download'
1516
},
1617

1718
events: {
@@ -29,6 +30,10 @@ module.exports = Mn.View.extend({
2930
e.preventDefault();
3031
let win = window.open($(e.currentTarget).attr('rel'), '_blank');
3132
win.focus();
33+
},
34+
'click @ui.download': function (e) {
35+
e.preventDefault();
36+
App.Api.Nginx.Certificates.download(this.model.get('id'))
3237
}
3338
},
3439

frontend/js/i18n/messages.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
"other-certificate-key": "Certificate Key",
189189
"other-intermediate-certificate": "Intermediate Certificate",
190190
"force-renew": "Renew Now",
191+
"download":"Download",
191192
"renew-title": "Renew Let'sEncrypt Certificate"
192193
},
193194
"access-lists": {

0 commit comments

Comments
 (0)