treat blobs as binary, render images w/o image url

This commit is contained in:
Tony Tam
2016-09-15 01:07:08 -07:00
parent 15f76ad355
commit 4bdaeba797
3 changed files with 622 additions and 396 deletions

840
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

@@ -651,7 +651,8 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
// puts the response data in UI // puts the response data in UI
showStatus: function(response) { showStatus: function(response) {
var url, content; console.log(response.headers);
var url, content, blob;
if (response.content === undefined) { if (response.content === undefined) {
content = response.data; content = response.data;
url = response.url; url = response.url;
@@ -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;
if(typeof content === 'string') {
content = jQuery.trim(content); 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,37 @@ 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';
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).');
}
} 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 +744,23 @@ 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); //pre = $('<img>').attr('src', url);
var binary = '';
var bytes = new Uint8Array( content );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
pre = $('<img>').attr( 'src', 'data:' + contentType + ';base64,' + btoa(binary));
// 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)