Move decryptFile into a utility function so that it can be shared between different components
This commit is contained in:
parent
12fc70c671
commit
b6653dd0ed
2 changed files with 50 additions and 27 deletions
|
@ -19,16 +19,13 @@ limitations under the License.
|
|||
var React = require('react');
|
||||
var filesize = require('filesize');
|
||||
|
||||
// Pull in the encryption lib so that we can decrypt attachments.
|
||||
var encrypt = require("browser-encrypt-attachment");
|
||||
// Pull in a fetch polyfill so we can download encrypted attachments.
|
||||
require("isomorphic-fetch");
|
||||
|
||||
var MatrixClientPeg = require('../../../MatrixClientPeg');
|
||||
var ImageUtils = require('../../../ImageUtils');
|
||||
var Modal = require('../../../Modal');
|
||||
var sdk = require('../../../index');
|
||||
var dis = require("../../../dispatcher");
|
||||
var DecryptFile = require('../../../utils/DecryptFile');
|
||||
|
||||
|
||||
module.exports = React.createClass({
|
||||
|
@ -116,7 +113,13 @@ module.exports = React.createClass({
|
|||
var self = this;
|
||||
if (content.file !== undefined && this.state.decryptedUrl === null) {
|
||||
// TODO: hook up an error handler to the promise.
|
||||
this.decryptFile(content.file).catch(function (err) {
|
||||
DecryptFile.decryptFile(content.file).then(function(blob) {
|
||||
if (!self._unmounted) {
|
||||
self.setState({
|
||||
decryptedUrl: window.URL.createObjectURL(blob),
|
||||
});
|
||||
}
|
||||
}).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";
|
||||
|
@ -124,28 +127,6 @@ module.exports = React.createClass({
|
|||
}
|
||||
},
|
||||
|
||||
decryptFile: function(file) {
|
||||
var url = MatrixClientPeg.get().mxcUrlToHttp(file.url);
|
||||
var self = this;
|
||||
// Download the encrypted file as an array buffer.
|
||||
return fetch(url).then(function (response) {
|
||||
return response.arrayBuffer();
|
||||
}).then(function (responseData) {
|
||||
// Decrypt the array buffer using the information taken from
|
||||
// the event content.
|
||||
return encrypt.decryptAttachment(responseData, file);
|
||||
}).then(function(dataArray) {
|
||||
// Turn the array into a Blob and use createObjectUrl to make
|
||||
// a url that we can use as an img src.
|
||||
var blob = new Blob([dataArray], {type: file.mimetype});
|
||||
if (!self._unmounted) {
|
||||
self.setState({
|
||||
decryptedUrl: window.URL.createObjectURL(blob),
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
componentWillUnmount: function() {
|
||||
dis.unregister(this.dispatcherRef);
|
||||
this._unmounted = true;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue