From 34df6ea2426736b9b37094f111e850ccb06741af Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Sat, 12 Nov 2016 12:20:36 +0000 Subject: [PATCH] Complete attachment-encryption patch https://github.com/matrix-org/matrix-react-sdk/pull/533 originally landed in the wrong branch, and was reverted by https://github.com/matrix-org/matrix-react-sdk/pull/546. https://github.com/matrix-org/matrix-react-sdk/pull/548 attempted to land it on the develop branch, but omitted a small amount of the patch. This lands the final part, which got missed out. --- src/ContentMessages.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/ContentMessages.js b/src/ContentMessages.js index 8665c40ae0..28c28e875e 100644 --- a/src/ContentMessages.js +++ b/src/ContentMessages.js @@ -23,6 +23,8 @@ var MatrixClientPeg = require('./MatrixClientPeg'); var sdk = require('./index'); var Modal = require('./Modal'); +var encrypt = require("browser-encrypt-attachment"); + function infoForImageFile(imageFile) { var deferred = q.defer(); @@ -155,10 +157,26 @@ class ContentMessages { this.inprogress.push(upload); dis.dispatch({action: 'upload_started'}); + var encryptInfo = null; var error; var self = this; return def.promise.then(function() { - upload.promise = matrixClient.uploadContent(file); + if (matrixClient.isRoomEncrypted(roomId)) { + // If the room is encrypted then encrypt the file before uploading it. + // First read the file into memory. + upload.promise = readFileAsArrayBuffer(file).then(function(data) { + // Then encrypt the file. + return encrypt.encryptAttachment(data); + }).then(function(encryptResult) { + // Record the information needed to decrypt the attachment. + encryptInfo = encryptResult.info; + // Pass the encrypted data as a Blob to the uploader. + var blob = new Blob([encryptResult.data]); + return matrixClient.uploadContent(blob); + }); + } else { + upload.promise = matrixClient.uploadContent(file); + } return upload.promise; }).progress(function(ev) { if (ev) {