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:
parent
4f5d3f0080
commit
df06499a19
1 changed files with 3 additions and 16 deletions
|
@ -52,17 +52,6 @@
|
||||||
Whisper.AttachmentView = Backbone.View.extend({
|
Whisper.AttachmentView = Backbone.View.extend({
|
||||||
tagName: 'span',
|
tagName: 'span',
|
||||||
className: 'attachment',
|
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() {
|
render: function() {
|
||||||
var view;
|
var view;
|
||||||
switch(this.model.contentType.split('/')[0]) {
|
switch(this.model.contentType.split('/')[0]) {
|
||||||
|
@ -72,11 +61,9 @@
|
||||||
default:
|
default:
|
||||||
throw 'Unsupported attachment type';
|
throw 'Unsupported attachment type';
|
||||||
}
|
}
|
||||||
this.encodeAsDataUrl().then(function(base64) {
|
var blob = new Blob([this.model.data], {type: this.model.contentType});
|
||||||
view.render(base64, this.model.contentType);
|
view.render(window.URL.createObjectURL(blob), this.model.contentType);
|
||||||
view.$el.appendTo(this.$el);
|
view.$el.appendTo(this.$el);
|
||||||
this.$el.trigger('update');
|
|
||||||
}.bind(this));
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue