Skip to content

Commit efc1194

Browse files
authored
improve(response-rendering): give Content-Disposition priority (#4140)
* Remove produces/consumes setter from OperationContainer * Store consumes/produces information in `meta` key * Migrate produces value state usage to `meta` key * use meta consumes data for isXml check * Fix failing tests * normalize action name casing * restore correct produces fallback value logic * default to first server if current server is invalid * improve(response-rendering): give Content-Disposition render priority
1 parent bb84da4 commit efc1194

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

src/core/components/response-body.jsx

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,41 @@ export default class ResponseBody extends React.Component {
2020
let body, bodyEl
2121
url = url || ""
2222

23-
// JSON
24-
if (/json/i.test(contentType)) {
23+
if (
24+
/^application\/octet-stream/i.test(contentType) ||
25+
(headers["Content-Disposition"] && (/attachment/i).test(headers["Content-Disposition"])) ||
26+
(headers["content-disposition"] && (/attachment/i).test(headers["content-disposition"])) ||
27+
(headers["Content-Description"] && (/File Transfer/i).test(headers["Content-Description"])) ||
28+
(headers["content-description"] && (/File Transfer/i).test(headers["content-description"]))) {
29+
// Download
30+
31+
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
32+
33+
if (!isSafari && "Blob" in window) {
34+
let type = contentType || "text/html"
35+
let blob = (content instanceof Blob) ? content : new Blob([content], {type: type})
36+
let href = window.URL.createObjectURL(blob)
37+
let fileName = url.substr(url.lastIndexOf("/") + 1)
38+
let download = [type, fileName, href].join(":")
39+
40+
// Use filename from response header,
41+
// First check if filename is quoted (e.g. contains space), if no, fallback to not quoted check
42+
let disposition = headers["content-disposition"] || headers["Content-Disposition"]
43+
if (typeof disposition !== "undefined") {
44+
let responseFilename = extractFileNameFromContentDispositionHeader(disposition)
45+
if (responseFilename !== null) {
46+
download = responseFilename
47+
}
48+
}
49+
50+
bodyEl = <div><a href={ href } download={ download }>{ "Download file" }</a></div>
51+
} else {
52+
bodyEl = <pre>Download headers detected but your browser does not support downloading binary via XHR (Blob).</pre>
53+
}
54+
55+
// Anything else (CORS)
56+
} else if (/json/i.test(contentType)) {
57+
// JSON
2558
try {
2659
body = JSON.stringify(JSON.parse(content), null, " ")
2760
} catch (error) {
@@ -53,40 +86,6 @@ export default class ResponseBody extends React.Component {
5386
// Audio
5487
} else if (/^audio\//i.test(contentType)) {
5588
bodyEl = <pre><audio controls><source src={ url } type={ contentType } /></audio></pre>
56-
57-
// Download
58-
} else if (
59-
/^application\/octet-stream/i.test(contentType) ||
60-
(headers["Content-Disposition"] && (/attachment/i).test(headers["Content-Disposition"])) ||
61-
(headers["content-disposition"] && (/attachment/i).test(headers["content-disposition"])) ||
62-
(headers["Content-Description"] && (/File Transfer/i).test(headers["Content-Description"])) ||
63-
(headers["content-description"] && (/File Transfer/i).test(headers["content-description"]))) {
64-
65-
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
66-
67-
if (!isSafari && "Blob" in window) {
68-
let type = contentType || "text/html"
69-
let blob = (content instanceof Blob) ? content : new Blob([content], {type: type})
70-
let href = window.URL.createObjectURL(blob)
71-
let fileName = url.substr(url.lastIndexOf("/") + 1)
72-
let download = [type, fileName, href].join(":")
73-
74-
// Use filename from response header,
75-
// First check if filename is quoted (e.g. contains space), if no, fallback to not quoted check
76-
let disposition = headers["content-disposition"] || headers["Content-Disposition"]
77-
if (typeof disposition !== "undefined") {
78-
let responseFilename = extractFileNameFromContentDispositionHeader(disposition)
79-
if (responseFilename !== null) {
80-
download = responseFilename
81-
}
82-
}
83-
84-
bodyEl = <div><a href={ href } download={ download }>{ "Download file" }</a></div>
85-
} else {
86-
bodyEl = <pre>Download headers detected but your browser does not support downloading binary via XHR (Blob).</pre>
87-
}
88-
89-
// Anything else (CORS)
9089
} else if (typeof content === "string") {
9190
bodyEl = <HighlightCode value={ content } />
9291
} else if ( content.size > 0 ) {

0 commit comments

Comments
 (0)