2015-11-27 18:02:32 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-11-27 18:02:32 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2016-11-08 14:42:20 +03:00
|
|
|
import React from 'react';
|
2016-11-04 21:20:20 +03:00
|
|
|
import MFileBody from './MFileBody';
|
2016-11-08 14:42:20 +03:00
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
|
|
|
import ImageUtils from '../../../ImageUtils';
|
|
|
|
import Modal from '../../../Modal';
|
|
|
|
import sdk from '../../../index';
|
|
|
|
import dis from '../../../dispatcher';
|
2016-12-02 17:21:07 +03:00
|
|
|
import { decryptFile, readBlobAsDataUri } from '../../../utils/DecryptFile';
|
2017-07-12 15:58:14 +03:00
|
|
|
import Promise from 'bluebird';
|
2017-05-30 17:09:57 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2017-10-29 10:43:52 +03:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2016-11-02 19:26:10 +03:00
|
|
|
|
2015-11-27 18:02:32 +03:00
|
|
|
module.exports = React.createClass({
|
2015-11-30 18:19:43 +03:00
|
|
|
displayName: 'MImageBody',
|
2015-11-27 18:02:32 +03:00
|
|
|
|
2016-02-22 20:19:04 +03:00
|
|
|
propTypes: {
|
|
|
|
/* the MatrixEvent to show */
|
|
|
|
mxEvent: React.PropTypes.object.isRequired,
|
2016-12-02 14:11:35 +03:00
|
|
|
|
|
|
|
/* called when the image has loaded */
|
|
|
|
onWidgetLoad: React.PropTypes.func.isRequired,
|
2015-11-27 18:02:32 +03:00
|
|
|
},
|
|
|
|
|
2016-11-04 14:52:47 +03:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
decryptedUrl: null,
|
2016-11-15 14:22:39 +03:00
|
|
|
decryptedThumbnailUrl: null,
|
2016-12-02 17:21:07 +03:00
|
|
|
decryptedBlob: null,
|
2017-02-28 01:16:56 +03:00
|
|
|
error: null,
|
2016-11-04 14:52:47 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2015-12-08 22:49:16 +03:00
|
|
|
onClick: function onClick(ev) {
|
2015-11-27 18:02:32 +03:00
|
|
|
if (ev.button == 0 && !ev.metaKey) {
|
|
|
|
ev.preventDefault();
|
2016-11-08 15:57:24 +03:00
|
|
|
const content = this.props.mxEvent.getContent();
|
|
|
|
const httpUrl = this._getContentUrl();
|
|
|
|
const ImageView = sdk.getComponent("elements.ImageView");
|
|
|
|
const params = {
|
2015-11-27 18:02:32 +03:00
|
|
|
src: httpUrl,
|
2017-05-30 17:09:57 +03:00
|
|
|
name: content.body && content.body.length > 0 ? content.body : _t('Attachment'),
|
2017-02-28 01:16:56 +03:00
|
|
|
mxEvent: this.props.mxEvent,
|
2015-12-08 22:49:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
if (content.info) {
|
|
|
|
params.width = content.info.w;
|
|
|
|
params.height = content.info.h;
|
2016-04-07 20:58:50 +03:00
|
|
|
params.fileSize = content.info.size;
|
2015-12-08 22:49:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Modal.createDialog(ImageView, params, "mx_Dialog_lightbox");
|
2015-11-27 18:02:32 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_isGif: function() {
|
2016-11-08 15:57:24 +03:00
|
|
|
const content = this.props.mxEvent.getContent();
|
2017-02-28 01:16:56 +03:00
|
|
|
return (
|
|
|
|
content &&
|
|
|
|
content.info &&
|
2017-03-02 16:39:56 +03:00
|
|
|
content.info.mimetype === "image/gif"
|
|
|
|
);
|
2015-11-27 18:02:32 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onImageEnter: function(e) {
|
2017-10-29 10:43:52 +03:00
|
|
|
if (!this._isGif() || SettingsStore.getValue("autoplayGifsAndVideos")) {
|
2015-11-27 18:02:32 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-02-28 01:16:56 +03:00
|
|
|
const imgElement = e.target;
|
2016-11-04 21:40:57 +03:00
|
|
|
imgElement.src = this._getContentUrl();
|
2015-11-27 18:02:32 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onImageLeave: function(e) {
|
2017-10-29 10:43:52 +03:00
|
|
|
if (!this._isGif() || SettingsStore.getValue("autoplayGifsAndVideos")) {
|
2015-11-27 18:02:32 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-02-28 01:16:56 +03:00
|
|
|
const imgElement = e.target;
|
2015-11-27 18:02:32 +03:00
|
|
|
imgElement.src = this._getThumbUrl();
|
|
|
|
},
|
|
|
|
|
2016-11-04 14:52:47 +03:00
|
|
|
_getContentUrl: function() {
|
2016-11-08 14:42:20 +03:00
|
|
|
const content = this.props.mxEvent.getContent();
|
2016-11-04 14:52:47 +03:00
|
|
|
if (content.file !== undefined) {
|
|
|
|
return this.state.decryptedUrl;
|
|
|
|
} else {
|
|
|
|
return MatrixClientPeg.get().mxcUrlToHttp(content.url);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-11-27 18:02:32 +03:00
|
|
|
_getThumbUrl: function() {
|
2016-11-08 14:42:20 +03:00
|
|
|
const content = this.props.mxEvent.getContent();
|
2016-11-04 14:52:47 +03:00
|
|
|
if (content.file !== undefined) {
|
2017-03-02 16:39:56 +03:00
|
|
|
// Don't use the thumbnail for clients wishing to autoplay gifs.
|
2016-11-15 14:22:39 +03:00
|
|
|
if (this.state.decryptedThumbnailUrl) {
|
|
|
|
return this.state.decryptedThumbnailUrl;
|
|
|
|
}
|
2016-11-04 14:52:47 +03:00
|
|
|
return this.state.decryptedUrl;
|
|
|
|
} else {
|
|
|
|
return MatrixClientPeg.get().mxcUrlToHttp(content.url, 800, 600);
|
|
|
|
}
|
2015-11-27 18:02:32 +03:00
|
|
|
},
|
|
|
|
|
2016-03-24 03:13:32 +03:00
|
|
|
componentDidMount: function() {
|
|
|
|
this.dispatcherRef = dis.register(this.onAction);
|
|
|
|
this.fixupHeight();
|
2016-11-08 14:42:20 +03:00
|
|
|
const content = this.props.mxEvent.getContent();
|
2016-11-04 14:52:47 +03:00
|
|
|
if (content.file !== undefined && this.state.decryptedUrl === null) {
|
2017-07-12 16:02:00 +03:00
|
|
|
let thumbnailPromise = Promise.resolve(null);
|
2016-11-15 14:22:39 +03:00
|
|
|
if (content.info.thumbnail_file) {
|
|
|
|
thumbnailPromise = decryptFile(
|
2017-02-28 01:16:56 +03:00
|
|
|
content.info.thumbnail_file,
|
2017-01-20 17:22:27 +03:00
|
|
|
).then(function(blob) {
|
2016-12-02 17:21:07 +03:00
|
|
|
return readBlobAsDataUri(blob);
|
|
|
|
});
|
2016-11-15 14:22:39 +03:00
|
|
|
}
|
2017-02-28 01:16:56 +03:00
|
|
|
let decryptedBlob;
|
2016-11-15 14:22:39 +03:00
|
|
|
thumbnailPromise.then((thumbnailUrl) => {
|
2016-12-02 17:21:07 +03:00
|
|
|
return decryptFile(content.file).then(function(blob) {
|
|
|
|
decryptedBlob = blob;
|
|
|
|
return readBlobAsDataUri(blob);
|
|
|
|
}).then((contentUrl) => {
|
2016-11-15 14:22:39 +03:00
|
|
|
this.setState({
|
|
|
|
decryptedUrl: contentUrl,
|
|
|
|
decryptedThumbnailUrl: thumbnailUrl,
|
2016-12-02 17:21:07 +03:00
|
|
|
decryptedBlob: decryptedBlob,
|
2016-11-15 14:22:39 +03:00
|
|
|
});
|
2016-12-02 14:11:35 +03:00
|
|
|
this.props.onWidgetLoad();
|
2016-11-04 18:39:39 +03:00
|
|
|
});
|
2016-11-15 14:22:39 +03:00
|
|
|
}).catch((err) => {
|
2016-11-18 23:08:26 +03:00
|
|
|
console.warn("Unable to decrypt attachment: ", err);
|
2016-11-04 14:52:47 +03:00
|
|
|
// Set a placeholder image when we can't decrypt the image.
|
2016-11-18 23:08:26 +03:00
|
|
|
this.setState({
|
|
|
|
error: err,
|
|
|
|
});
|
2016-11-15 14:22:39 +03:00
|
|
|
}).done();
|
2016-11-04 14:52:47 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-24 03:13:32 +03:00
|
|
|
componentWillUnmount: function() {
|
|
|
|
dis.unregister(this.dispatcherRef);
|
|
|
|
},
|
|
|
|
|
|
|
|
onAction: function(payload) {
|
|
|
|
if (payload.action === "timeline_resize") {
|
|
|
|
this.fixupHeight();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
fixupHeight: function() {
|
2016-03-24 19:58:27 +03:00
|
|
|
if (!this.refs.image) {
|
2016-03-29 02:46:51 +03:00
|
|
|
console.warn("Refusing to fix up height on MImageBody with no image element");
|
2016-03-24 19:58:27 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-08 15:57:24 +03:00
|
|
|
const content = this.props.mxEvent.getContent();
|
|
|
|
const timelineWidth = this.refs.body.offsetWidth;
|
|
|
|
const maxHeight = 600; // let images take up as much width as they can so long as the height doesn't exceed 600px.
|
2016-03-24 04:12:51 +03:00
|
|
|
// the alternative here would be 600*timelineWidth/800; to scale them down to fit inside a 4:3 bounding box
|
2016-03-24 03:13:32 +03:00
|
|
|
|
2016-03-24 19:58:27 +03:00
|
|
|
//console.log("trying to fit image into timelineWidth of " + this.refs.body.offsetWidth + " or " + this.refs.body.clientWidth);
|
2017-02-28 01:16:56 +03:00
|
|
|
let thumbHeight = null;
|
2016-04-02 02:36:19 +03:00
|
|
|
if (content.info) {
|
|
|
|
thumbHeight = ImageUtils.thumbHeight(content.info.w, content.info.h, timelineWidth, maxHeight);
|
|
|
|
}
|
2016-03-24 19:58:27 +03:00
|
|
|
this.refs.image.style.height = thumbHeight + "px";
|
2016-03-29 02:46:51 +03:00
|
|
|
// console.log("Image height now", thumbHeight);
|
2016-03-24 03:13:32 +03:00
|
|
|
},
|
2015-11-27 18:02:32 +03:00
|
|
|
|
2016-03-24 03:13:32 +03:00
|
|
|
render: function() {
|
2016-11-08 15:57:24 +03:00
|
|
|
const TintableSvg = sdk.getComponent("elements.TintableSvg");
|
|
|
|
const content = this.props.mxEvent.getContent();
|
2016-11-11 14:59:08 +03:00
|
|
|
|
2016-11-18 23:08:26 +03:00
|
|
|
if (this.state.error !== null) {
|
|
|
|
return (
|
|
|
|
<span className="mx_MImageBody" ref="body">
|
2017-10-11 19:56:17 +03:00
|
|
|
<img src="img/warning.svg" width="16" height="16" />
|
|
|
|
{ _t("Error decrypting image") }
|
2016-11-18 23:08:26 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-04 14:52:47 +03:00
|
|
|
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 (
|
|
|
|
<span className="mx_MImageBody" ref="body">
|
2016-11-18 23:08:26 +03:00
|
|
|
<div className="mx_MImageBody_thumbnail" ref="image" style={{
|
|
|
|
"display": "flex",
|
2016-12-02 14:11:35 +03:00
|
|
|
"alignItems": "center",
|
2016-11-18 23:08:26 +03:00
|
|
|
"width": "100%",
|
|
|
|
}}>
|
2016-12-02 14:11:35 +03:00
|
|
|
<img src="img/spinner.gif" alt={content.body} width="32" height="32" style={{
|
|
|
|
"margin": "auto",
|
2017-10-11 19:56:17 +03:00
|
|
|
}} />
|
2016-11-18 23:08:26 +03:00
|
|
|
</div>
|
2016-11-04 14:52:47 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-08 15:57:24 +03:00
|
|
|
const contentUrl = this._getContentUrl();
|
2017-02-28 01:16:56 +03:00
|
|
|
let thumbUrl;
|
2017-10-29 10:43:52 +03:00
|
|
|
if (this._isGif() && SettingsStore.getValue("autoplayGifsAndVideos")) {
|
2017-02-28 01:16:56 +03:00
|
|
|
thumbUrl = contentUrl;
|
|
|
|
} else {
|
|
|
|
thumbUrl = this._getThumbUrl();
|
|
|
|
}
|
2016-11-04 14:52:47 +03:00
|
|
|
|
2016-11-04 14:52:47 +03:00
|
|
|
if (thumbUrl) {
|
2015-11-27 18:02:32 +03:00
|
|
|
return (
|
2016-03-24 19:58:27 +03:00
|
|
|
<span className="mx_MImageBody" ref="body">
|
2017-10-11 19:56:17 +03:00
|
|
|
<a href={contentUrl} onClick={this.onClick}>
|
2016-03-24 19:58:27 +03:00
|
|
|
<img className="mx_MImageBody_thumbnail" src={thumbUrl} ref="image"
|
2016-03-24 03:13:32 +03:00
|
|
|
alt={content.body}
|
2017-12-01 17:18:13 +03:00
|
|
|
onLoad={this.props.onWidgetLoad}
|
2015-11-27 18:02:32 +03:00
|
|
|
onMouseEnter={this.onImageEnter}
|
2016-04-02 02:36:19 +03:00
|
|
|
onMouseLeave={this.onImageLeave} />
|
2015-11-27 18:02:32 +03:00
|
|
|
</a>
|
2016-12-02 17:21:07 +03:00
|
|
|
<MFileBody {...this.props} decryptedBlob={this.state.decryptedBlob} />
|
2015-11-27 18:02:32 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
} else if (content.body) {
|
|
|
|
return (
|
2016-01-03 03:11:11 +03:00
|
|
|
<span className="mx_MImageBody">
|
2017-10-11 19:56:17 +03:00
|
|
|
{ _t("Image '%(Body)s' cannot be displayed.", {Body: content.body}) }
|
2015-11-27 18:02:32 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
2016-01-03 03:11:11 +03:00
|
|
|
<span className="mx_MImageBody">
|
2017-10-11 19:56:17 +03:00
|
|
|
{ _t("This image cannot be displayed.") }
|
2015-11-27 18:02:32 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|