Merge pull request #2404 from swagger-api/issue-2132

Add support for binary downloads, inline image rendering
This commit is contained in:
Tony Tam
2016-09-16 10:02:07 -07:00
committed by GitHub
4 changed files with 641 additions and 404 deletions

942
dist/swagger-ui.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -60,6 +60,6 @@
"phantomjs": "1.9.19", "phantomjs": "1.9.19",
"selenium-webdriver": "^2.45.0", "selenium-webdriver": "^2.45.0",
"sinon-chai": "2.8.0", "sinon-chai": "2.8.0",
"swagger-client": "2.1.18" "swagger-client": "2.1.20"
} }
} }

View File

@@ -651,6 +651,7 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
// puts the response data in UI // puts the response data in UI
showStatus: function(response) { showStatus: function(response) {
console.log(response.headers);
var url, content; var url, content;
if (response.content === undefined) { if (response.content === undefined) {
content = response.data; content = response.data;
@@ -660,7 +661,9 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
url = response.request.url; url = response.request.url;
} }
var headers = response.headers; var headers = response.headers;
content = jQuery.trim(content); if(typeof content === 'string') {
content = jQuery.trim(content);
}
// if server is nice, and sends content-type back, we can use it // if server is nice, and sends content-type back, we can use it
var contentType = null; var contentType = null;
@@ -685,6 +688,36 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
pre = $('<pre class="json" />').append(code); pre = $('<pre class="json" />').append(code);
// JSON // JSON
} else if (headers['Content-Disposition'] && (/attachment/).test(headers['Content-Disposition']) ||
headers['content-disposition'] && (/attachment/).test(headers['content-disposition']) ||
headers['Content-Description'] && (/File Transfer/).test(headers['Content-Description']) ||
headers['content-description'] && (/File Transfer/).test(headers['content-description'])) {
if ('Blob' in window) {
var type = contentType || 'text/html';
var a = document.createElement('a');
var href = window.URL.createObjectURL(content);
var fileName = response.url.substr(response.url.lastIndexOf('/') + 1);
var download = [type, fileName, href].join(':');
// Use filename from response header
var disposition = headers['content-disposition'] || headers['Content-Disposition'];
if(typeof disposition !== 'undefined') {
var responseFilename = /filename=([^;]*);?/.exec(disposition);
if(responseFilename !== null && responseFilename.length > 1) {
download = responseFilename[1];
}
}
a.setAttribute('href', href);
a.setAttribute('download', download);
a.innerText = 'Download ' + fileName;
pre = $('<div/>').append(a);
} else {
pre = $('<pre class="json" />').append('Download headers detected but your browser does not support downloading binary via XHR (Blob).');
}
} else if (contentType === 'application/json' || /\+json$/.test(contentType)) { } else if (contentType === 'application/json' || /\+json$/.test(contentType)) {
var json = null; var json = null;
try { try {
@@ -710,49 +743,17 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
code = $('<code />').text(content); code = $('<code />').text(content);
pre = $('<pre class="plain" />').append(code); pre = $('<pre class="plain" />').append(code);
// Image // Image
} else if (/^image\//.test(contentType)) { } else if (/^image\//.test(contentType)) {
pre = $('<img>').attr('src', url); var urlCreator = window.URL || window.webkitURL;
var imageUrl = urlCreator.createObjectURL(content);
pre = $('<img>').attr( 'src', imageUrl);
// Audio // Audio
} else if (/^audio\//.test(contentType) && supportsAudioPlayback(contentType)) { } else if (/^audio\//.test(contentType) && supportsAudioPlayback(contentType)) {
pre = $('<audio controls>').append($('<source>').attr('src', url).attr('type', contentType)); pre = $('<audio controls>').append($('<source>').attr('src', url).attr('type', contentType));
// Download
} else if (headers['Content-Disposition'] && (/attachment/).test(headers['Content-Disposition']) ||
headers['content-disposition'] && (/attachment/).test(headers['content-disposition']) ||
headers['Content-Description'] && (/File Transfer/).test(headers['Content-Description']) ||
headers['content-description'] && (/File Transfer/).test(headers['content-description'])) {
if ('Blob' in window) {
var type = contentType || 'text/html';
var blob = new Blob([content], {type: type});
var a = document.createElement('a');
var href = window.URL.createObjectURL(blob);
var fileName = response.url.substr(response.url.lastIndexOf('/') + 1);
var download = [type, fileName, href].join(':');
// Use filename from response header
var disposition = headers['content-disposition'] || headers['Content-Disposition'];
if(typeof disposition !== 'undefined') {
var responseFilename = /filename=([^;]*);?/.exec(disposition);
if(responseFilename !== null && responseFilename.length > 1) {
download = responseFilename[1];
}
}
a.setAttribute('href', href);
a.setAttribute('download', download);
a.innerText = 'Download ' + fileName;
pre = $('<div/>').append(a);
} else {
pre = $('<pre class="json" />').append('Download headers detected but your browser does not support downloading binary via XHR (Blob).');
}
// Location header based redirect download
} else if(headers.location || headers.Location) { } else if(headers.location || headers.Location) {
// Location header based redirect download
window.location = response.url; window.location = response.url;
// Anything else (CORS) // Anything else (CORS)