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.
pull/21833/head
Richard van der Hoff 2016-11-12 12:20:36 +00:00
parent 062cb7a52e
commit 34df6ea242
1 changed files with 19 additions and 1 deletions

View File

@ -23,6 +23,8 @@ var MatrixClientPeg = require('./MatrixClientPeg');
var sdk = require('./index'); var sdk = require('./index');
var Modal = require('./Modal'); var Modal = require('./Modal');
var encrypt = require("browser-encrypt-attachment");
function infoForImageFile(imageFile) { function infoForImageFile(imageFile) {
var deferred = q.defer(); var deferred = q.defer();
@ -155,10 +157,26 @@ class ContentMessages {
this.inprogress.push(upload); this.inprogress.push(upload);
dis.dispatch({action: 'upload_started'}); dis.dispatch({action: 'upload_started'});
var encryptInfo = null;
var error; var error;
var self = this; var self = this;
return def.promise.then(function() { 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; return upload.promise;
}).progress(function(ev) { }).progress(function(ev) {
if (ev) { if (ev) {