2015-11-27 18:02:32 +03:00
|
|
|
/*
|
2021-07-16 01:37:48 +03:00
|
|
|
Copyright 2015 - 2021 The Matrix.org Foundation C.I.C.
|
2019-08-09 16:51:07 +03:00
|
|
|
Copyright 2018, 2019 Michael Telatynski <7t3chguy@gmail.com>
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-07-13 09:17:51 +03:00
|
|
|
import React, { ComponentProps, createRef } from 'react';
|
2021-07-07 22:00:31 +03:00
|
|
|
import { Blurhash } from "react-blurhash";
|
2018-02-06 20:50:53 +03:00
|
|
|
|
2016-11-04 21:20:20 +03:00
|
|
|
import MFileBody from './MFileBody';
|
2016-11-08 14:42:20 +03:00
|
|
|
import Modal from '../../../Modal';
|
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";
|
2019-12-17 20:26:12 +03:00
|
|
|
import MatrixClientContext from "../../../contexts/MatrixClientContext";
|
2020-04-25 00:14:41 +03:00
|
|
|
import InlineSpinner from '../elements/InlineSpinner';
|
2021-06-29 15:11:58 +03:00
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
|
|
|
import { mediaFromContent } from "../../../customisations/Media";
|
2021-07-01 22:48:34 +03:00
|
|
|
import { BLURHASH_FIELD } from "../../../ContentMessages";
|
2021-07-02 14:46:42 +03:00
|
|
|
import { IMediaEventContent } from '../../../customisations/models/IMediaEventContent';
|
2021-07-13 09:17:51 +03:00
|
|
|
import ImageView from '../elements/ImageView';
|
2021-07-13 10:16:01 +03:00
|
|
|
import { SyncState } from 'matrix-js-sdk/src/sync.api';
|
2021-07-16 19:57:14 +03:00
|
|
|
import { IBodyProps } from "./IBodyProps";
|
2016-11-02 19:26:10 +03:00
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
interface IState {
|
2021-07-02 15:01:30 +03:00
|
|
|
decryptedUrl?: string;
|
|
|
|
decryptedThumbnailUrl?: string;
|
|
|
|
decryptedBlob?: Blob;
|
|
|
|
error;
|
|
|
|
imgError: boolean;
|
|
|
|
imgLoaded: boolean;
|
2021-07-02 14:46:42 +03:00
|
|
|
loadedImageDimensions?: {
|
|
|
|
naturalWidth: number;
|
|
|
|
naturalHeight: number;
|
2021-07-02 15:01:30 +03:00
|
|
|
};
|
|
|
|
hover: boolean;
|
|
|
|
showImage: boolean;
|
2021-07-02 14:46:42 +03:00
|
|
|
}
|
2015-11-27 18:02:32 +03:00
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
@replaceableComponent("views.messages.MImageBody")
|
2021-07-16 19:57:14 +03:00
|
|
|
export default class MImageBody extends React.Component<IBodyProps, IState> {
|
2019-12-17 20:26:12 +03:00
|
|
|
static contextType = MatrixClientContext;
|
2021-07-02 14:46:42 +03:00
|
|
|
private unmounted = true;
|
|
|
|
private image = createRef<HTMLImageElement>();
|
2018-02-06 20:50:53 +03:00
|
|
|
|
2021-07-16 19:57:14 +03:00
|
|
|
constructor(props: IBodyProps) {
|
2018-02-26 17:01:33 +03:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2016-11-04 14:52:47 +03:00
|
|
|
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,
|
2018-02-06 20:50:53 +03:00
|
|
|
imgError: false,
|
2018-05-21 18:59:13 +03:00
|
|
|
imgLoaded: false,
|
2018-06-14 17:53:49 +03:00
|
|
|
loadedImageDimensions: null,
|
2018-05-21 18:59:13 +03:00
|
|
|
hover: false,
|
2019-09-28 06:08:31 +03:00
|
|
|
showImage: SettingsStore.getValue("showImages"),
|
2016-11-04 14:52:47 +03:00
|
|
|
};
|
2018-02-26 17:01:33 +03:00
|
|
|
}
|
2016-11-04 14:52:47 +03:00
|
|
|
|
2021-03-10 00:16:15 +03:00
|
|
|
// FIXME: factor this out and apply it to MVideoBody and MAudioBody too!
|
2021-07-13 10:16:01 +03:00
|
|
|
private onClientSync = (syncState: SyncState, prevState: SyncState): void => {
|
2018-02-06 20:50:53 +03:00
|
|
|
if (this.unmounted) return;
|
|
|
|
// Consider the client reconnected if there is no error with syncing.
|
2018-08-30 16:39:48 +03:00
|
|
|
// This means the state could be RECONNECTING, SYNCING, PREPARED or CATCHUP.
|
2018-02-06 20:50:53 +03:00
|
|
|
const reconnected = syncState !== "ERROR" && prevState !== syncState;
|
|
|
|
if (reconnected && this.state.imgError) {
|
|
|
|
// Load the image again
|
|
|
|
this.setState({
|
|
|
|
imgError: false,
|
|
|
|
});
|
|
|
|
}
|
2021-07-02 14:46:42 +03:00
|
|
|
};
|
2016-11-04 14:52:47 +03:00
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
protected showImage(): void {
|
2019-09-28 06:08:31 +03:00
|
|
|
localStorage.setItem("mx_ShowImage_" + this.props.mxEvent.getId(), "true");
|
2021-06-29 15:11:58 +03:00
|
|
|
this.setState({ showImage: true });
|
2021-07-02 14:46:42 +03:00
|
|
|
this.downloadImage();
|
2019-09-28 06:08:31 +03:00
|
|
|
}
|
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
protected onClick = (ev: React.MouseEvent): void => {
|
2018-06-19 10:37:11 +03:00
|
|
|
if (ev.button === 0 && !ev.metaKey) {
|
2015-11-27 18:02:32 +03:00
|
|
|
ev.preventDefault();
|
2019-09-28 06:08:31 +03:00
|
|
|
if (!this.state.showImage) {
|
|
|
|
this.showImage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-13 10:16:53 +03:00
|
|
|
const content = this.props.mxEvent.getContent<IMediaEventContent>();
|
2021-07-02 14:46:42 +03:00
|
|
|
const httpUrl = this.getContentUrl();
|
2021-07-14 10:54:33 +03:00
|
|
|
const params: Omit<ComponentProps<typeof ImageView>, "onFinished"> = {
|
2015-11-27 18:02:32 +03:00
|
|
|
src: httpUrl,
|
2021-07-02 14:46:42 +03:00
|
|
|
name: content.body?.length > 0 ? content.body : _t('Attachment'),
|
2017-02-28 01:16:56 +03:00
|
|
|
mxEvent: this.props.mxEvent,
|
2021-02-24 21:11:08 +03:00
|
|
|
permalinkCreator: this.props.permalinkCreator,
|
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
|
|
|
}
|
|
|
|
|
2021-04-13 09:07:17 +03:00
|
|
|
Modal.createDialog(ImageView, params, "mx_Dialog_lightbox", null, true);
|
2015-11-27 18:02:32 +03:00
|
|
|
}
|
2021-07-02 14:46:42 +03:00
|
|
|
};
|
2015-11-27 18:02:32 +03:00
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
private isGif = (): boolean => {
|
2016-11-08 15:57:24 +03:00
|
|
|
const content = this.props.mxEvent.getContent();
|
2021-07-13 10:17:18 +03:00
|
|
|
return content.info?.mimetype === "image/gif";
|
2021-07-02 14:46:42 +03:00
|
|
|
};
|
2015-11-27 18:02:32 +03:00
|
|
|
|
2021-07-13 10:18:05 +03:00
|
|
|
private onImageEnter = (e: React.MouseEvent<HTMLImageElement>): void => {
|
2018-05-21 18:59:13 +03:00
|
|
|
this.setState({ hover: true });
|
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
if (!this.state.showImage || !this.isGif() || SettingsStore.getValue("autoplayGifsAndVideos")) {
|
2015-11-27 18:02:32 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-07-13 10:18:34 +03:00
|
|
|
const imgElement = e.currentTarget;
|
2021-07-02 14:46:42 +03:00
|
|
|
imgElement.src = this.getContentUrl();
|
|
|
|
};
|
2015-11-27 18:02:32 +03:00
|
|
|
|
2021-07-13 10:19:19 +03:00
|
|
|
private onImageLeave = (e: React.MouseEvent<HTMLImageElement>): void => {
|
2018-05-21 18:59:13 +03:00
|
|
|
this.setState({ hover: false });
|
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
if (!this.state.showImage || !this.isGif() || SettingsStore.getValue("autoplayGifsAndVideos")) {
|
2015-11-27 18:02:32 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-07-13 10:19:45 +03:00
|
|
|
const imgElement = e.currentTarget;
|
2021-07-02 14:46:42 +03:00
|
|
|
imgElement.src = this.getThumbUrl();
|
|
|
|
};
|
2015-11-27 18:02:32 +03:00
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
private onImageError = (): void => {
|
2018-02-06 20:50:53 +03:00
|
|
|
this.setState({
|
|
|
|
imgError: true,
|
|
|
|
});
|
2021-07-02 14:46:42 +03:00
|
|
|
};
|
2018-02-06 20:50:53 +03:00
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
private onImageLoad = (): void => {
|
2019-03-06 14:27:16 +03:00
|
|
|
this.props.onHeightChanged();
|
2018-06-14 17:44:55 +03:00
|
|
|
|
|
|
|
let loadedImageDimensions;
|
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
if (this.image.current) {
|
|
|
|
const { naturalWidth, naturalHeight } = this.image.current;
|
2019-04-08 15:57:39 +03:00
|
|
|
// this is only used as a fallback in case content.info.w/h is missing
|
2018-06-14 17:44:55 +03:00
|
|
|
loadedImageDimensions = { naturalWidth, naturalHeight };
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ imgLoaded: true, loadedImageDimensions });
|
2021-07-02 14:46:42 +03:00
|
|
|
};
|
2018-04-29 06:28:15 +03:00
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
protected getContentUrl(): string {
|
2021-03-04 05:06:46 +03:00
|
|
|
const media = mediaFromContent(this.props.mxEvent.getContent());
|
|
|
|
if (media.isEncrypted) {
|
2016-11-04 14:52:47 +03:00
|
|
|
return this.state.decryptedUrl;
|
|
|
|
} else {
|
2021-03-04 05:06:46 +03:00
|
|
|
return media.srcHttp;
|
2016-11-04 14:52:47 +03:00
|
|
|
}
|
2018-02-26 17:01:33 +03:00
|
|
|
}
|
2016-11-04 14:52:47 +03:00
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
protected getThumbUrl(): string {
|
2021-03-04 05:06:46 +03:00
|
|
|
// FIXME: we let images grow as wide as you like, rather than capped to 800x600.
|
2019-01-20 18:35:16 +03:00
|
|
|
// So either we need to support custom timeline widths here, or reimpose the cap, otherwise the
|
|
|
|
// thumbnail resolution will be unnecessarily reduced.
|
|
|
|
// custom timeline widths seems preferable.
|
2021-04-26 20:25:49 +03:00
|
|
|
const thumbWidth = 800;
|
|
|
|
const thumbHeight = 600;
|
2019-01-20 18:35:16 +03:00
|
|
|
|
2021-07-13 10:20:13 +03:00
|
|
|
const content = this.props.mxEvent.getContent<IMediaEventContent>();
|
2021-03-04 05:06:46 +03:00
|
|
|
const media = mediaFromContent(content);
|
|
|
|
|
|
|
|
if (media.isEncrypted) {
|
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;
|
2021-03-04 05:06:46 +03:00
|
|
|
} else if (content.info && content.info.mimetype === "image/svg+xml" && media.hasThumbnail) {
|
2019-01-20 18:35:16 +03:00
|
|
|
// special case to return clientside sender-generated thumbnails for SVGs, if any,
|
2018-04-29 05:58:17 +03:00
|
|
|
// given we deliberately don't thumbnail them serverside to prevent
|
|
|
|
// billion lol attacks and similar
|
2021-03-04 05:06:46 +03:00
|
|
|
return media.getThumbnailHttp(thumbWidth, thumbHeight, 'scale');
|
2018-04-29 06:31:30 +03:00
|
|
|
} else {
|
2019-04-08 15:57:39 +03:00
|
|
|
// we try to download the correct resolution
|
|
|
|
// for hi-res images (like retina screenshots).
|
|
|
|
// synapse only supports 800x600 thumbnails for now though,
|
|
|
|
// so we'll need to download the original image for this to work
|
|
|
|
// well for now. First, let's try a few cases that let us avoid
|
2019-05-14 15:55:38 +03:00
|
|
|
// downloading the original, including:
|
|
|
|
// - When displaying a GIF, we always want to thumbnail so that we can
|
|
|
|
// properly respect the user's GIF autoplay setting (which relies on
|
|
|
|
// thumbnailing to produce the static preview image)
|
|
|
|
// - On a low DPI device, always thumbnail to save bandwidth
|
|
|
|
// - If there's no sizing info in the event, default to thumbnail
|
2019-05-13 20:00:52 +03:00
|
|
|
const info = content.info;
|
|
|
|
if (
|
2021-07-02 14:46:42 +03:00
|
|
|
this.isGif() ||
|
2021-04-27 11:56:28 +03:00
|
|
|
window.devicePixelRatio === 1.0 ||
|
2019-05-13 20:00:52 +03:00
|
|
|
(!info || !info.w || !info.h || !info.size)
|
|
|
|
) {
|
2021-03-04 05:06:46 +03:00
|
|
|
return media.getThumbnailOfSourceHttp(thumbWidth, thumbHeight);
|
2019-04-08 15:57:39 +03:00
|
|
|
} else {
|
2019-01-20 18:35:16 +03:00
|
|
|
// we should only request thumbnails if the image is bigger than 800x600
|
|
|
|
// (or 1600x1200 on retina) otherwise the image in the timeline will just
|
|
|
|
// end up resampled and de-retina'd for no good reason.
|
|
|
|
// Ideally the server would pregen 1600x1200 thumbnails in order to provide retina
|
|
|
|
// thumbnails, but we don't do this currently in synapse for fear of disk space.
|
|
|
|
// As a compromise, let's switch to non-retina thumbnails only if the original
|
|
|
|
// image is both physically too large and going to be massive to load in the
|
|
|
|
// timeline (e.g. >1MB).
|
|
|
|
|
2019-04-08 15:57:39 +03:00
|
|
|
const isLargerThanThumbnail = (
|
2019-05-13 20:00:52 +03:00
|
|
|
info.w > thumbWidth ||
|
|
|
|
info.h > thumbHeight
|
2019-04-08 15:57:39 +03:00
|
|
|
);
|
2021-03-04 05:06:46 +03:00
|
|
|
const isLargeFileSize = info.size > 1*1024*1024; // 1mb
|
2019-04-08 15:57:39 +03:00
|
|
|
|
|
|
|
if (isLargeFileSize && isLargerThanThumbnail) {
|
2019-01-20 18:35:16 +03:00
|
|
|
// image is too large physically and bytewise to clutter our timeline so
|
|
|
|
// we ask for a thumbnail, despite knowing that it will be max 800x600
|
|
|
|
// despite us being retina (as synapse doesn't do 1600x1200 thumbs yet).
|
2021-03-04 05:06:46 +03:00
|
|
|
return media.getThumbnailOfSourceHttp(thumbWidth, thumbHeight);
|
2019-04-08 15:57:39 +03:00
|
|
|
} else {
|
|
|
|
// download the original image otherwise, so we can scale it client side
|
|
|
|
// to take pixelRatio into account.
|
2021-03-04 05:06:46 +03:00
|
|
|
return media.srcHttp;
|
2019-01-20 18:35:16 +03:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 14:52:47 +03:00
|
|
|
}
|
2018-02-26 17:01:33 +03:00
|
|
|
}
|
2015-11-27 18:02:32 +03:00
|
|
|
|
2021-07-16 01:37:48 +03:00
|
|
|
private async downloadImage() {
|
|
|
|
if (this.props.mediaEventHelper.media.isEncrypted && this.state.decryptedUrl === null) {
|
|
|
|
try {
|
|
|
|
const thumbnailUrl = await this.props.mediaEventHelper.thumbnailUrl.value;
|
|
|
|
this.setState({
|
|
|
|
decryptedUrl: await this.props.mediaEventHelper.sourceUrl.value,
|
|
|
|
decryptedThumbnailUrl: thumbnailUrl,
|
|
|
|
decryptedBlob: await this.props.mediaEventHelper.sourceBlob.value,
|
2016-11-04 18:39:39 +03:00
|
|
|
});
|
2021-07-16 01:37:48 +03:00
|
|
|
} catch (err) {
|
2019-08-09 16:51:07 +03:00
|
|
|
if (this.unmounted) return;
|
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,
|
|
|
|
});
|
2021-07-16 01:37:48 +03:00
|
|
|
}
|
2016-11-04 14:52:47 +03:00
|
|
|
}
|
2020-10-23 19:16:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.unmounted = false;
|
|
|
|
this.context.on('sync', this.onClientSync);
|
|
|
|
|
|
|
|
const showImage = this.state.showImage ||
|
|
|
|
localStorage.getItem("mx_ShowImage_" + this.props.mxEvent.getId()) === "true";
|
2019-09-28 06:08:31 +03:00
|
|
|
|
2020-10-23 19:16:09 +03:00
|
|
|
if (showImage) {
|
2021-07-16 01:37:48 +03:00
|
|
|
// noinspection JSIgnoredPromiseFromCall
|
2021-07-02 14:46:42 +03:00
|
|
|
this.downloadImage();
|
2021-06-29 15:11:58 +03:00
|
|
|
this.setState({ showImage: true });
|
2021-07-16 01:37:48 +03:00
|
|
|
} // else don't download anything because we don't want to display anything.
|
2018-02-26 17:01:33 +03:00
|
|
|
}
|
2016-11-04 14:52:47 +03:00
|
|
|
|
2018-02-26 17:01:33 +03:00
|
|
|
componentWillUnmount() {
|
2018-02-06 20:50:53 +03:00
|
|
|
this.unmounted = true;
|
2019-12-17 20:26:12 +03:00
|
|
|
this.context.removeListener('sync', this.onClientSync);
|
2018-03-10 00:12:56 +03:00
|
|
|
}
|
|
|
|
|
2021-07-13 12:49:49 +03:00
|
|
|
protected messageContent(
|
|
|
|
contentUrl: string,
|
|
|
|
thumbUrl: string,
|
|
|
|
content: IMediaEventContent,
|
|
|
|
forcedHeight?: number,
|
|
|
|
): JSX.Element {
|
2018-06-14 17:44:55 +03:00
|
|
|
let infoWidth;
|
|
|
|
let infoHeight;
|
2018-06-14 16:19:30 +03:00
|
|
|
|
2018-06-14 17:44:55 +03:00
|
|
|
if (content && content.info && content.info.w && content.info.h) {
|
2018-06-14 16:19:30 +03:00
|
|
|
infoWidth = content.info.w;
|
2018-06-14 17:44:55 +03:00
|
|
|
infoHeight = content.info.h;
|
|
|
|
} else {
|
2020-08-11 07:06:30 +03:00
|
|
|
// Whilst the image loads, display nothing. We also don't display a blurhash image
|
|
|
|
// because we don't really know what size of image we'll end up with.
|
2018-06-14 17:44:55 +03:00
|
|
|
//
|
|
|
|
// Once loaded, use the loaded image dimensions stored in `loadedImageDimensions`.
|
|
|
|
//
|
|
|
|
// By doing this, the image "pops" into the timeline, but is still restricted
|
|
|
|
// by the same width and height logic below.
|
|
|
|
if (!this.state.loadedImageDimensions) {
|
2019-10-02 21:46:21 +03:00
|
|
|
let imageElement;
|
2019-09-28 06:08:31 +03:00
|
|
|
if (!this.state.showImage) {
|
2019-10-02 02:00:01 +03:00
|
|
|
imageElement = <HiddenImagePlaceholder />;
|
2019-10-02 21:46:21 +03:00
|
|
|
} else {
|
|
|
|
imageElement = (
|
2021-07-23 12:23:45 +03:00
|
|
|
<img
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
src={thumbUrl}
|
|
|
|
ref={this.image}
|
2021-04-27 18:23:27 +03:00
|
|
|
alt={content.body}
|
|
|
|
onError={this.onImageError}
|
|
|
|
onLoad={this.onImageLoad}
|
2019-10-02 21:46:21 +03:00
|
|
|
/>
|
|
|
|
);
|
2019-09-28 06:08:31 +03:00
|
|
|
}
|
|
|
|
return this.wrapImage(contentUrl, imageElement);
|
2018-06-14 17:44:55 +03:00
|
|
|
}
|
|
|
|
infoWidth = this.state.loadedImageDimensions.naturalWidth;
|
|
|
|
infoHeight = this.state.loadedImageDimensions.naturalHeight;
|
2018-06-14 16:19:30 +03:00
|
|
|
}
|
|
|
|
|
2018-05-18 12:27:22 +03:00
|
|
|
// The maximum height of the thumbnail as it is rendered as an <img>
|
2021-07-13 12:49:49 +03:00
|
|
|
const maxHeight = forcedHeight || Math.min((this.props.maxImageHeight || 600), infoHeight);
|
2018-05-22 18:09:54 +03:00
|
|
|
// The maximum width of the thumbnail, as dictated by its natural
|
2018-05-18 12:27:22 +03:00
|
|
|
// maximum height.
|
2018-06-14 16:19:30 +03:00
|
|
|
const maxWidth = infoWidth * maxHeight / infoHeight;
|
2018-05-18 11:47:49 +03:00
|
|
|
|
|
|
|
let img = null;
|
2018-05-21 18:59:13 +03:00
|
|
|
let placeholder = null;
|
2018-10-24 21:06:48 +03:00
|
|
|
let gifLabel = null;
|
2018-05-21 18:59:13 +03:00
|
|
|
|
2021-05-21 23:39:38 +03:00
|
|
|
if (!this.state.imgLoaded) {
|
2020-08-11 07:06:30 +03:00
|
|
|
placeholder = this.getPlaceholder(maxWidth, maxHeight);
|
2018-05-21 18:59:13 +03:00
|
|
|
}
|
|
|
|
|
2019-10-02 02:00:01 +03:00
|
|
|
let showPlaceholder = Boolean(placeholder);
|
2018-05-21 18:59:13 +03:00
|
|
|
|
|
|
|
if (thumbUrl && !this.state.imgError) {
|
2018-05-18 12:27:22 +03:00
|
|
|
// Restrict the width of the thumbnail here, otherwise it will fill the container
|
|
|
|
// which has the same width as the timeline
|
2018-10-22 23:03:39 +03:00
|
|
|
// mx_MImageBody_thumbnail resizes img to exactly container size
|
2019-09-28 06:08:31 +03:00
|
|
|
img = (
|
2021-07-23 12:23:45 +03:00
|
|
|
<img
|
|
|
|
className="mx_MImageBody_thumbnail"
|
|
|
|
src={thumbUrl}
|
|
|
|
ref={this.image}
|
2021-08-10 17:14:04 +03:00
|
|
|
// Force the image to be the full size of the container, even if the
|
|
|
|
// pixel size is smaller. The problem here is that we don't know what
|
|
|
|
// thumbnail size the HS is going to give us, but we have to commit to
|
|
|
|
// a container size immediately and not change it when the image loads
|
|
|
|
// or we'll get a scroll jump (or have to leave blank space).
|
|
|
|
// This will obviously result in an upscaled image which will be a bit
|
|
|
|
// blurry. The best fix would be for the HS to advertise what size thumbnails
|
|
|
|
// it guarantees to produce.
|
|
|
|
style={{ height: '100%' }}
|
2021-04-27 18:23:27 +03:00
|
|
|
alt={content.body}
|
|
|
|
onError={this.onImageError}
|
|
|
|
onLoad={this.onImageLoad}
|
|
|
|
onMouseEnter={this.onImageEnter}
|
2021-08-10 17:14:04 +03:00
|
|
|
onMouseLeave={this.onImageLeave}
|
|
|
|
/>
|
2019-09-28 06:08:31 +03:00
|
|
|
);
|
2019-10-02 02:00:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.state.showImage) {
|
2021-07-02 14:46:42 +03:00
|
|
|
img = <HiddenImagePlaceholder maxWidth={maxWidth} />;
|
2020-08-11 07:06:30 +03:00
|
|
|
showPlaceholder = false; // because we're hiding the image, so don't show the placeholder.
|
2018-05-18 11:47:49 +03:00
|
|
|
}
|
2018-05-17 20:15:34 +03:00
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
if (this.isGif() && !SettingsStore.getValue("autoplayGifsAndVideos") && !this.state.hover) {
|
2018-10-24 21:06:48 +03:00
|
|
|
gifLabel = <p className="mx_MImageBody_gifLabel">GIF</p>;
|
2018-10-22 22:57:58 +03:00
|
|
|
}
|
|
|
|
|
2018-05-21 18:59:13 +03:00
|
|
|
const thumbnail = (
|
2021-07-30 12:53:29 +03:00
|
|
|
<div className="mx_MImageBody_thumbnail_container" style={{ maxHeight: maxHeight, maxWidth: maxWidth, aspectRatio: `${infoWidth}/${infoHeight}` }}>
|
2018-05-22 12:43:16 +03:00
|
|
|
{ showPlaceholder &&
|
2021-07-23 12:23:45 +03:00
|
|
|
<div
|
|
|
|
className="mx_MImageBody_thumbnail"
|
|
|
|
style={{
|
|
|
|
// Constrain width here so that spinner appears central to the loaded thumbnail
|
2021-07-23 12:42:46 +03:00
|
|
|
maxWidth: `min(100%, ${infoWidth}px)`,
|
2021-07-23 12:23:45 +03:00
|
|
|
}}
|
|
|
|
>
|
2021-05-21 23:39:38 +03:00
|
|
|
{ placeholder }
|
2018-05-21 18:59:13 +03:00
|
|
|
</div>
|
2018-05-22 12:43:16 +03:00
|
|
|
}
|
2018-05-21 18:59:13 +03:00
|
|
|
|
2021-08-10 17:14:04 +03:00
|
|
|
<div style={{
|
|
|
|
display: !showPlaceholder ? undefined : 'none',
|
|
|
|
height: '100%', // Also force to size of a parent to prevent scroll-jumps (see above)
|
|
|
|
}}>
|
2018-05-18 11:47:49 +03:00
|
|
|
{ img }
|
2018-10-24 21:06:48 +03:00
|
|
|
{ gifLabel }
|
2018-05-17 20:15:34 +03:00
|
|
|
</div>
|
2018-04-29 05:58:17 +03:00
|
|
|
|
2018-05-21 18:59:13 +03:00
|
|
|
{ this.state.hover && this.getTooltip() }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
return this.wrapImage(contentUrl, thumbnail);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overidden by MStickerBody
|
2021-07-02 14:46:42 +03:00
|
|
|
protected wrapImage(contentUrl: string, children: JSX.Element): JSX.Element {
|
2018-05-21 18:59:13 +03:00
|
|
|
return <a href={contentUrl} onClick={this.onClick}>
|
2021-07-20 00:43:11 +03:00
|
|
|
{ children }
|
2018-05-21 18:59:13 +03:00
|
|
|
</a>;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overidden by MStickerBody
|
2021-07-13 10:20:40 +03:00
|
|
|
protected getPlaceholder(width: number, height: number): JSX.Element {
|
2021-05-21 23:19:41 +03:00
|
|
|
const blurhash = this.props.mxEvent.getContent().info[BLURHASH_FIELD];
|
2021-07-07 22:00:31 +03:00
|
|
|
if (blurhash) return <Blurhash hash={blurhash} width={width} height={height} />;
|
2021-07-20 09:54:32 +03:00
|
|
|
return (
|
2021-05-21 23:39:38 +03:00
|
|
|
<InlineSpinner w={32} h={32} />
|
2021-07-20 09:54:32 +03:00
|
|
|
);
|
2018-05-21 18:59:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overidden by MStickerBody
|
2021-07-13 10:21:05 +03:00
|
|
|
protected getTooltip(): JSX.Element {
|
2018-05-21 18:59:13 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overidden by MStickerBody
|
2021-07-19 19:04:33 +03:00
|
|
|
protected getFileBody(): string | JSX.Element {
|
2021-07-16 20:05:04 +03:00
|
|
|
// We only ever need the download bar if we're appearing outside of the timeline
|
|
|
|
if (this.props.tileShape) {
|
|
|
|
return <MFileBody {...this.props} showGenericPlaceholder={false} />;
|
|
|
|
}
|
2018-02-26 17:01:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2021-07-13 10:21:33 +03:00
|
|
|
const content = this.props.mxEvent.getContent<IMediaEventContent>();
|
2016-11-11 14:59:08 +03:00
|
|
|
|
2016-11-18 23:08:26 +03:00
|
|
|
if (this.state.error !== null) {
|
|
|
|
return (
|
2021-07-23 18:22:35 +03:00
|
|
|
<div className="mx_MImageBody">
|
2019-01-11 04:37:28 +03:00
|
|
|
<img src={require("../../../../res/img/warning.svg")} width="16" height="16" />
|
2017-10-11 19:56:17 +03:00
|
|
|
{ _t("Error decrypting image") }
|
2021-07-23 18:22:35 +03:00
|
|
|
</div>
|
2016-11-18 23:08:26 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
const contentUrl = this.getContentUrl();
|
2017-02-28 01:16:56 +03:00
|
|
|
let thumbUrl;
|
2021-07-02 14:46:42 +03:00
|
|
|
if (this.isGif() && SettingsStore.getValue("autoplayGifsAndVideos")) {
|
2021-04-27 18:23:27 +03:00
|
|
|
thumbUrl = contentUrl;
|
2017-02-28 01:16:56 +03:00
|
|
|
} else {
|
2021-07-02 14:46:42 +03:00
|
|
|
thumbUrl = this.getThumbUrl();
|
2017-02-28 01:16:56 +03:00
|
|
|
}
|
2016-11-04 14:52:47 +03:00
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
const thumbnail = this.messageContent(contentUrl, thumbUrl, content);
|
2018-05-21 18:59:13 +03:00
|
|
|
const fileBody = this.getFileBody();
|
|
|
|
|
2021-07-23 18:22:35 +03:00
|
|
|
return <div className="mx_MImageBody">
|
2018-05-21 18:59:13 +03:00
|
|
|
{ thumbnail }
|
|
|
|
{ fileBody }
|
2021-07-23 18:22:35 +03:00
|
|
|
</div>;
|
2018-02-26 17:01:33 +03:00
|
|
|
}
|
|
|
|
}
|
2019-10-02 02:00:01 +03:00
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
interface PlaceholderIProps {
|
|
|
|
hover?: boolean;
|
|
|
|
maxWidth?: number;
|
|
|
|
}
|
2019-10-02 02:00:01 +03:00
|
|
|
|
2021-07-02 14:46:42 +03:00
|
|
|
export class HiddenImagePlaceholder extends React.PureComponent<PlaceholderIProps> {
|
2019-10-02 02:00:01 +03:00
|
|
|
render() {
|
2021-07-02 14:46:42 +03:00
|
|
|
const maxWidth = this.props.maxWidth ? this.props.maxWidth + "px" : null;
|
2019-10-02 02:00:01 +03:00
|
|
|
let className = 'mx_HiddenImagePlaceholder';
|
|
|
|
if (this.props.hover) className += ' mx_HiddenImagePlaceholder_hover';
|
|
|
|
return (
|
2021-07-22 17:05:09 +03:00
|
|
|
<div className={className} style={{ maxWidth: `min(100%, ${maxWidth}px)` }}>
|
2019-10-02 02:00:01 +03:00
|
|
|
<div className='mx_HiddenImagePlaceholder_button'>
|
2019-10-02 21:52:07 +03:00
|
|
|
<span className='mx_HiddenImagePlaceholder_eye' />
|
2021-07-20 00:43:11 +03:00
|
|
|
<span>{ _t("Show image") }</span>
|
2019-10-02 02:00:01 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|