Skip to content

Commit 17864c4

Browse files
author
Bruno Tremblay
authored
Dual purpose download/show button
Added recognized types to download button behavior Slightly modify behavior for image/audio/video to display the same with the label "SHOW" instead and open the content in a new window.
1 parent bf3ece0 commit 17864c4

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/components/api-request.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ export default class ApiRequest extends LitElement {
677677
${this.responseIsBlob
678678
? html`
679679
<div class="tab-content col" style="flex:1; display:${this.activeResponseTab === 'response' ? 'flex' : 'none'};">
680-
<button class="m-btn" @click="${this.downloadResponseBlob}">DOWNLOAD</button>
680+
<button class="m-btn" @click="${this.downloadResponseBlob}">${this.responseBlobType.toUpperCase()}</button>
681681
</div>`
682682
: html`
683683
<div class="tab-content col m-markdown" style="flex:1;display:${this.activeResponseTab === 'response' ? 'flex' : 'none'};" >
@@ -1003,18 +1003,24 @@ export default class ApiRequest extends LitElement {
10031003
resp.json().then((respObj) => {
10041004
me.responseText = JSON.stringify(respObj, null, 2);
10051005
});
1006-
} else if (contentType.includes('octet-stream')) {
1006+
} else if (RegExp('7z|octet-stream|pdf|tar|zip').test(contentType)) {
10071007
me.responseIsBlob = true;
1008+
me.responseBlobType = 'download';
10081009
const contentDisposition = resp.headers.get('content-disposition');
10091010
me.respContentDisposition = contentDisposition ? contentDisposition.split('filename=')[1] : 'filename';
1010-
resp.blob().then((respBlob) => {
1011-
me.responseBlobUrl = URL.createObjectURL(respBlob);
1012-
});
1011+
} else if (RegExp('^audio|^image|^video').test(contentType)) {
1012+
me.responseIsBlob = true;
1013+
me.responseBlobType = 'show';
10131014
} else {
10141015
resp.text().then((respText) => {
10151016
me.responseText = respText;
10161017
});
10171018
}
1019+
if (me.responseIsBlob) {
1020+
resp.blob().then((respBlob) => {
1021+
me.responseBlobUrl = URL.createObjectURL(respBlob);
1022+
});
1023+
}
10181024
} else {
10191025
resp.text().then((respText) => {
10201026
me.responseText = respText;
@@ -1071,7 +1077,11 @@ export default class ApiRequest extends LitElement {
10711077
document.body.appendChild(a);
10721078
a.style = 'display: none';
10731079
a.href = this.responseBlobUrl;
1074-
a.download = this.respContentDisposition;
1080+
if (this.responseBlobType === 'show') {
1081+
a.target = '_blank';
1082+
} else {
1083+
a.download = this.respContentDisposition;
1084+
}
10751085
a.click();
10761086
a.remove();
10771087
}
@@ -1084,6 +1094,7 @@ export default class ApiRequest extends LitElement {
10841094
this.responseStatus = 'success';
10851095
this.responseMessage = '';
10861096
this.responseIsBlob = false;
1097+
this.responseBlobType = '';
10871098
this.respContentDisposition = '';
10881099
if (this.responseBlobUrl) {
10891100
URL.revokeObjectURL(this.responseBlobUrl);

0 commit comments

Comments
 (0)