Use blob urls to display attachments

Converting attachment data to base64-encoded data uris takes O(n) and
there's no need! URL.createObjectURL returns a magic link that can be
set as the `src` attribute to `img`, `video`, and `audio` tags to load
blob data directly without copying.

https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL
This commit is contained in:
lilia 2015-03-03 12:46:53 -08:00
parent 4f5d3f0080
commit df06499a19

View file

@ -52,17 +52,6 @@
Whisper.AttachmentView = Backbone.View.extend({
tagName: 'span',
className: 'attachment',
encodeAsDataUrl: function () {
return new Promise(function(resolve, reject) {
var blob = new Blob([this.model.data], { type: this.model.contentType });
var FR = new FileReader();
FR.onload = function(e) {
resolve(e.target.result);
};
FR.onerror = reject;
FR.readAsDataURL(blob);
}.bind(this));
},
render: function() {
var view;
switch(this.model.contentType.split('/')[0]) {
@ -72,11 +61,9 @@
default:
throw 'Unsupported attachment type';
}
this.encodeAsDataUrl().then(function(base64) {
view.render(base64, this.model.contentType);
view.$el.appendTo(this.$el);
this.$el.trigger('update');
}.bind(this));
var blob = new Blob([this.model.data], {type: this.model.contentType});
view.render(window.URL.createObjectURL(blob), this.model.contentType);
view.$el.appendTo(this.$el);
return this;
}
});