From 59313b5177df6725ef018918185393704f710483 Mon Sep 17 00:00:00 2001 From: lilia Date: Sun, 13 Sep 2015 19:42:49 -0700 Subject: [PATCH] Let message models manage blob urls for attachments // FREEBIE --- js/models/messages.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/js/models/messages.js b/js/models/messages.js index a971757fe260..dead3309767b 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -8,6 +8,10 @@ var Message = window.Whisper.Message = Backbone.Model.extend({ database : Whisper.Database, storeName : 'messages', + initialize: function() { + this.on('change:attachments', this.updateImageUrl); + this.on('destroy', this.revokeImageUrl); + }, defaults : function() { return { timestamp: new Date().getTime(), @@ -57,6 +61,30 @@ return this.get('body'); }, + updateImageUrl: function() { + this.revokeImageUrl(); + var attachment = message.get('attachments')[0]; + if (attachment) { + var blob = new Blob([attachment.data], { + type: attachment.contentType + }); + this.imageUrl = URL.createObjectURL(blob); + } else { + this.imageUrl = null; + } + }, + revokeImageUrl: function() { + if (this.imageUrl) { + URL.revokeObjectURL(this.imageUrl); + this.imageUrl = null; + } + }, + getImageUrl: function() { + if (this.imageUrl === undefined) { + this.updateImageUrl(); + } + return this.imageUrl; + }, getContact: function() { if (this.collection) { return this.collection.conversation.contactCollection.get(this.get('source'));