From 95e8889857d0be1ecf062ee6c2159a164f60e7b7 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Fri, 4 Nov 2016 13:05:34 +0000 Subject: [PATCH] Decypt m.video events --- src/components/views/messages/MImageBody.js | 1 - src/components/views/messages/MVideoBody.js | 100 ++++++++++++++++++-- 2 files changed, 94 insertions(+), 7 deletions(-) diff --git a/src/components/views/messages/MImageBody.js b/src/components/views/messages/MImageBody.js index 54fea699d2..8d5f3ed756 100644 --- a/src/components/views/messages/MImageBody.js +++ b/src/components/views/messages/MImageBody.js @@ -162,7 +162,6 @@ module.exports = React.createClass({ render: function() { var TintableSvg = sdk.getComponent("elements.TintableSvg"); var content = this.props.mxEvent.getContent(); - var cli = MatrixClientPeg.get(); if (content.file !== undefined && this.state.decryptedUrl === null) { diff --git a/src/components/views/messages/MVideoBody.js b/src/components/views/messages/MVideoBody.js index d29a3ea53e..df707ddcb6 100644 --- a/src/components/views/messages/MVideoBody.js +++ b/src/components/views/messages/MVideoBody.js @@ -19,13 +19,22 @@ limitations under the License. var React = require('react'); var filesize = require('filesize'); + var MatrixClientPeg = require('../../../MatrixClientPeg'); var Modal = require('../../../Modal'); var sdk = require('../../../index'); +var DecryptFile = require("../../../utils/DecryptFile") module.exports = React.createClass({ displayName: 'MVideoBody', + getInitialState: function() { + return { + decryptedUrl: null, + decryptedThumbnailUrl: null, + }; + }, + thumbScale: function(fullWidth, fullHeight, thumbWidth, thumbHeight) { if (!fullWidth || !fullHeight) { // Cannot calculate thumbnail height for image: missing w/h in metadata. We can't even @@ -48,9 +57,88 @@ module.exports = React.createClass({ } }, + _getContentUrl: function() { + var content = this.props.mxEvent.getContent(); + if (content.file !== undefined) { + return this.state.decryptedUrl; + } else { + return MatrixClientPeg.get().mxcUrlToHttp(content.url); + } + }, + + _getThumbUrl: function() { + var content = this.props.mxEvent.getContent(); + if (content.file !== undefined) { + return this.state.decryptedThumbnailUrl; + } else { + return MatrixClientPeg.get().mxcUrlToHttp(content.url, 800, 600); + } + }, + + componentDidMount: function() { + var content = this.props.mxEvent.getContent(); + var self = this; + + if (content.file !== undefined && this.state.decryptedUrl === null) { + var thumbnailPromise = Promise.resolve(null); + if (content.info.thumbnail_file) { + thumbnailPromise = DecryptFile.decryptFile( + content.info.thumbnail_file + ); + } + thumbnailPromise.then(function(thumbnailBlob) { + DecryptFile.decryptFile( + content.file + ).then(function(contentBlob) { + if (self._unmounted) { + return; + } + var contentUrl = window.URL.createObjectURL(contentBlob); + var thumbUrl = null; + if (thumbnailBlob) { + thumbUrl = window.URL.createObjectURL(thumbnailBlob); + } + self.setState({ + decryptedUrl: contentUrl, + decryptedThumbnailUrl: thumbUrl, + }); + }); + }).catch(function (err) { + console.warn("Unable to decrypt attachment: ", err) + // Set a placeholder image when we can't decrypt the image. + self.refs.image.src = "img/warning.svg"; + }); + } + }, + + componentWillUnmount: function() { + this._unmounted = true; + if (this.state.decryptedUrl) { + window.URL.revokeObjectURL(this.state.decryptedUrl); + } + if (this.state.decryptedThumbnailUrl) { + window.URL.revokeObjectURL(this.state.decryptedThumbnailUrl); + } + }, + + render: function() { var content = this.props.mxEvent.getContent(); - var cli = MatrixClientPeg.get(); + + if (content.file !== undefined && this.state.decryptedUrl === null) { + // Need to decrypt the attachment + // The attachment is decrypted in componentDidMount. + // For now add an img tag with a spinner. + return ( + + {content.body} + + ); + } + + var contentUrl = this._getContentUrl(); + var thumbUrl = this._getThumbUrl(); var height = null; var width = null; @@ -63,8 +151,8 @@ module.exports = React.createClass({ height = Math.floor(content.info.h * scale); } - if (content.info.thumbnail_url) { - poster = cli.mxcUrlToHttp(content.info.thumbnail_url); + if (thumbUrl) { + poster = thumbUrl; preload = "none"; } } @@ -73,7 +161,7 @@ module.exports = React.createClass({ if (this.props.tileShape === "file_grid") { download = (
- + {content.body}
@@ -86,7 +174,7 @@ module.exports = React.createClass({ var TintableSvg = sdk.getComponent("elements.TintableSvg"); download = (
- + Download {content.body} ({ content.info && content.info.size ? filesize(content.info.size) : "Unknown size" }) @@ -96,7 +184,7 @@ module.exports = React.createClass({ return ( -