hterm: support mime types with inline image display
Chrome can auto-detect png/jpeg/gif images fine, but has trouble with
svg files. Allow the remote to specify the mime type explicitly, and
try to auto-guess it for svg files to fix display.
Change-Id: If948c10b1976743bfc3806ff19efc921c63aace7
Reviewed-on: https://chromium-review.googlesource.com/c/1436159
Reviewed-by: Vitaliy Shipitsyn <vsh@google.com>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/hterm/js/hterm_terminal.js b/hterm/js/hterm_terminal.js
index 276cb77..3062b30 100644
--- a/hterm/js/hterm_terminal.js
+++ b/hterm/js/hterm_terminal.js
@@ -3087,6 +3087,7 @@
* @param {string=} options.uri The source URI for the image.
* @param {ArrayBuffer=} options.buffer The ArrayBuffer image data.
* @param {Blob=} options.blob The Blob image data.
+ * @param {string=} options.type The MIME type of the image data.
* @param {function=} onLoad Callback when loading finishes.
* @param {function(Event)=} onError Callback when loading fails.
*/
@@ -3100,6 +3101,20 @@
if (!options.name)
options.name = '';
+ // See if the mime type is available. If not, guess from the filename.
+ // We don't list all possible mime types because the browser can usually
+ // guess it correctly. So list the ones that need a bit more help.
+ if (!options.type) {
+ const ary = options.name.split('.');
+ const ext = ary[ary.length - 1].trim();
+ switch (ext) {
+ case 'svg':
+ case 'svgz':
+ options.type = 'image/svg+xml';
+ break;
+ }
+ }
+
// Has the user approved image display yet?
if (this.allowImagesInline !== true) {
this.newLine();
@@ -3167,9 +3182,10 @@
if (options.uri !== undefined) {
img.src = options.uri;
} else if (options.buffer !== undefined) {
- const blob = new Blob([options.buffer]);
+ const blob = new Blob([options.buffer], {type: options.type});
img.src = URL.createObjectURL(blob);
} else {
+ const blob = new Blob([options.blob], {type: options.type});
img.src = URL.createObjectURL(options.blob);
}
img.title = img.alt = options.name;