Use FileReader to base64 encode attachments

* Implements #82
This commit is contained in:
Arlo Breault 2014-11-02 13:48:35 -08:00 committed by lilia
parent 6e3014895b
commit bc5dea62c3
3 changed files with 69 additions and 55 deletions

View file

@ -30,27 +30,36 @@ var Whisper = Whisper || {};
addIncomingMessage: function(decrypted) {
//TODO: The data in decrypted (from subscribeToPush) should already be cleaned up
var attachments = [];
for (var i = 0; i < decrypted.message.attachments.length; i++)
attachments[i] = "data:" + decrypted.message.attachments[i].contentType + ";base64," + btoa(getString(decrypted.message.attachments[i].decrypted));
return Promise.all(decrypted.message.attachments.map(function(a) {
return new Promise(function(resolve, reject) {
var dataView = new DataView(a.decrypted);
var blob = new Blob([dataView], { type: a.contentType });
var FR = new FileReader();
FR.onload = function(e) {
resolve(e.target.result);
};
FR.onerror = reject;
FR.readAsDataURL(blob);
});
})).then(function(base64_attachments) {
var thread = Whisper.Threads.findOrCreateForIncomingMessage(decrypted);
var timestamp = decrypted.pushMessage.timestamp.toNumber();
var m = thread.messages().add({
person: decrypted.pushMessage.source,
threadId: thread.id,
body: decrypted.message.body,
attachments: base64_attachments,
type: 'incoming',
timestamp: timestamp
});
m.save();
var thread = Whisper.Threads.findOrCreateForIncomingMessage(decrypted);
var timestamp = decrypted.pushMessage.timestamp.toNumber();
var m = thread.messages().add({
person: decrypted.pushMessage.source,
threadId: thread.id,
body: decrypted.message.body,
attachments: attachments,
type: 'incoming',
timestamp: timestamp
if (timestamp > thread.get('timestamp')) {
thread.set('timestamp', timestamp);
}
thread.save({unreadCount: thread.get('unreadCount') + 1, active: true});
return m;
});
m.save();
if (timestamp > thread.get('timestamp')) {
thread.set('timestamp', timestamp);
}
thread.save({unreadCount: thread.get('unreadCount') + 1, active: true});
return m;
}
}))();