2015-07-08 18:25:27 +03:00
|
|
|
/*
|
2021-07-14 03:35:56 +03:00
|
|
|
Copyright 2015 - 2021 The Matrix.org Foundation C.I.C.
|
2015-07-08 18:25:27 +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-06-29 15:11:58 +03:00
|
|
|
import React, { createRef } from 'react';
|
2016-11-08 15:57:24 +03:00
|
|
|
import filesize from 'filesize';
|
2017-05-25 13:39:08 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2016-11-18 23:08:26 +03:00
|
|
|
import Modal from '../../../Modal';
|
2020-02-08 01:07:29 +03:00
|
|
|
import AccessibleButton from "../elements/AccessibleButton";
|
2021-06-29 15:11:58 +03:00
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
|
|
|
import { mediaFromContent } from "../../../customisations/Media";
|
2021-03-09 22:54:20 +03:00
|
|
|
import ErrorDialog from "../dialogs/ErrorDialog";
|
2021-07-14 03:35:56 +03:00
|
|
|
import { TileShape } from "../rooms/EventTile";
|
2021-07-19 19:04:33 +03:00
|
|
|
import { presentableTextForFile } from "../../../utils/FileUtils";
|
2021-07-16 01:48:21 +03:00
|
|
|
import { IMediaEventContent } from "../../../customisations/models/IMediaEventContent";
|
2021-07-16 19:57:14 +03:00
|
|
|
import { IBodyProps } from "./IBodyProps";
|
2021-07-30 00:36:50 +03:00
|
|
|
import { FileDownloader } from "../../../utils/FileDownloader";
|
2021-07-30 00:49:23 +03:00
|
|
|
import TextWithTooltip from "../elements/TextWithTooltip";
|
2016-11-18 23:08:26 +03:00
|
|
|
|
2021-07-17 00:55:07 +03:00
|
|
|
export let DOWNLOAD_ICON_URL; // cached copy of the download.svg asset for the sandboxed iframe later on
|
2016-11-16 17:16:51 +03:00
|
|
|
|
2021-03-09 22:54:20 +03:00
|
|
|
async function cacheDownloadIcon() {
|
2021-07-17 00:55:07 +03:00
|
|
|
if (DOWNLOAD_ICON_URL) return; // cached already
|
2021-07-17 01:11:27 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
2021-03-09 22:54:20 +03:00
|
|
|
const svg = await fetch(require("../../../../res/img/download.svg")).then(r => r.text());
|
2021-07-17 00:55:07 +03:00
|
|
|
DOWNLOAD_ICON_URL = "data:image/svg+xml;base64," + window.btoa(svg);
|
2016-11-16 17:16:51 +03:00
|
|
|
}
|
|
|
|
|
2021-03-09 22:54:20 +03:00
|
|
|
// Cache the asset immediately
|
2021-07-16 01:48:21 +03:00
|
|
|
// noinspection JSIgnoredPromiseFromCall
|
2021-03-09 22:54:20 +03:00
|
|
|
cacheDownloadIcon();
|
2016-11-04 18:39:39 +03:00
|
|
|
|
2016-12-02 17:21:07 +03:00
|
|
|
// User supplied content can contain scripts, we have to be careful that
|
|
|
|
// we don't accidentally run those script within the same origin as the
|
|
|
|
// client. Otherwise those scripts written by remote users can read
|
|
|
|
// the access token and end-to-end keys that are in local storage.
|
|
|
|
//
|
|
|
|
// For attachments downloaded directly from the homeserver we can use
|
|
|
|
// Content-Security-Policy headers to disable script execution.
|
|
|
|
//
|
|
|
|
// But attachments with end-to-end encryption are more difficult to handle.
|
|
|
|
// We need to decrypt the attachment on the client and then display it.
|
|
|
|
// To display the attachment we need to turn the decrypted bytes into a URL.
|
|
|
|
//
|
|
|
|
// There are two ways to turn bytes into URLs, data URL and blob URLs.
|
|
|
|
// Data URLs aren't suitable for downloading a file because Chrome has a
|
|
|
|
// 2MB limit on the size of URLs that can be viewed in the browser or
|
|
|
|
// downloaded. This limit does not seem to apply when the url is used as
|
|
|
|
// the source attribute of an image tag.
|
|
|
|
//
|
2018-02-15 23:20:19 +03:00
|
|
|
// Blob URLs are generated using window.URL.createObjectURL and unfortunately
|
2016-12-02 17:21:07 +03:00
|
|
|
// for our purposes they inherit the origin of the page that created them.
|
|
|
|
// This means that any scripts that run when the URL is viewed will be able
|
|
|
|
// to access local storage.
|
|
|
|
//
|
|
|
|
// The easiest solution is to host the code that generates the blob URL on
|
|
|
|
// a different domain to the client.
|
|
|
|
// Another possibility is to generate the blob URL within a sandboxed iframe.
|
|
|
|
// The downside of using a second domain is that it complicates hosting,
|
|
|
|
// the downside of using a sandboxed iframe is that the browers are overly
|
|
|
|
// restrictive in what you are allowed to do with the generated URL.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current CSS style for a DOMElement.
|
|
|
|
* @param {HTMLElement} element The element to get the current style of.
|
|
|
|
* @return {string} The CSS style encoded as a string.
|
|
|
|
*/
|
2021-07-17 00:55:07 +03:00
|
|
|
export function computedStyle(element: HTMLElement) {
|
2016-12-02 17:21:07 +03:00
|
|
|
if (!element) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
const style = window.getComputedStyle(element, null);
|
2017-10-11 19:56:17 +03:00
|
|
|
let cssText = style.cssText;
|
2021-03-09 22:54:20 +03:00
|
|
|
// noinspection EqualityComparisonWithCoercionJS
|
2016-12-02 17:21:07 +03:00
|
|
|
if (cssText == "") {
|
|
|
|
// Firefox doesn't implement ".cssText" for computed styles.
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=137687
|
2017-10-11 19:56:17 +03:00
|
|
|
for (let i = 0; i < style.length; i++) {
|
2016-12-02 17:21:07 +03:00
|
|
|
cssText += style[i] + ":";
|
|
|
|
cssText += style.getPropertyValue(style[i]) + ";";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cssText;
|
|
|
|
}
|
|
|
|
|
2021-07-16 19:57:14 +03:00
|
|
|
interface IProps extends IBodyProps {
|
2021-07-16 01:48:21 +03:00
|
|
|
/* whether or not to show the default placeholder for the file. Defaults to true. */
|
|
|
|
showGenericPlaceholder: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
decryptedBlob?: Blob;
|
|
|
|
}
|
2021-03-05 06:07:48 +03:00
|
|
|
|
2021-07-16 01:48:21 +03:00
|
|
|
@replaceableComponent("views.messages.MFileBody")
|
|
|
|
export default class MFileBody extends React.Component<IProps, IState> {
|
2021-03-05 06:07:48 +03:00
|
|
|
static defaultProps = {
|
|
|
|
showGenericPlaceholder: true,
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
|
|
|
|
2021-07-16 01:48:21 +03:00
|
|
|
private iframe: React.RefObject<HTMLIFrameElement> = createRef();
|
|
|
|
private dummyLink: React.RefObject<HTMLAnchorElement> = createRef();
|
|
|
|
private userDidClick = false;
|
2021-07-30 01:01:26 +03:00
|
|
|
private fileDownloader: FileDownloader = new FileDownloader(() => this.iframe.current);
|
2020-08-29 14:14:16 +03:00
|
|
|
|
2021-07-16 01:48:21 +03:00
|
|
|
public constructor(props: IProps) {
|
|
|
|
super(props);
|
2020-08-29 14:14:16 +03:00
|
|
|
|
2021-07-16 01:48:21 +03:00
|
|
|
this.state = {};
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-03-07 19:00:10 +03:00
|
|
|
|
2021-07-30 00:36:50 +03:00
|
|
|
private get content(): IMediaEventContent {
|
|
|
|
return this.props.mxEvent.getContent<IMediaEventContent>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private get fileName(): string {
|
|
|
|
return this.content.body && this.content.body.length > 0 ? this.content.body : _t("Attachment");
|
|
|
|
}
|
|
|
|
|
|
|
|
private get linkText(): string {
|
|
|
|
return presentableTextForFile(this.content);
|
|
|
|
}
|
|
|
|
|
|
|
|
private downloadFile(fileName: string, text: string) {
|
|
|
|
this.fileDownloader.download({
|
|
|
|
blob: this.state.decryptedBlob,
|
|
|
|
name: fileName,
|
|
|
|
autoDownload: this.userDidClick,
|
|
|
|
opts: {
|
|
|
|
imgSrc: DOWNLOAD_ICON_URL,
|
|
|
|
imgStyle: null,
|
|
|
|
style: computedStyle(this.dummyLink.current),
|
2021-07-30 01:01:26 +03:00
|
|
|
textContent: _t("Download %(text)s", { text }),
|
2021-07-30 00:36:50 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-16 01:48:21 +03:00
|
|
|
private getContentUrl(): string {
|
2021-03-04 05:06:46 +03:00
|
|
|
const media = mediaFromContent(this.props.mxEvent.getContent());
|
|
|
|
return media.srcHttp;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-12-08 15:16:17 +03:00
|
|
|
|
2021-07-16 01:48:21 +03:00
|
|
|
public componentDidUpdate(prevProps, prevState) {
|
2019-03-07 19:00:10 +03:00
|
|
|
if (this.props.onHeightChanged && !prevState.decryptedBlob && this.state.decryptedBlob) {
|
|
|
|
this.props.onHeightChanged();
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-03-07 19:00:10 +03:00
|
|
|
|
2021-07-30 00:36:50 +03:00
|
|
|
private decryptFile = async (): Promise<void> => {
|
|
|
|
if (this.state.decryptedBlob) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
this.userDidClick = true;
|
|
|
|
this.setState({
|
|
|
|
decryptedBlob: await this.props.mediaEventHelper.sourceBlob.value,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
console.warn("Unable to decrypt attachment: ", err);
|
|
|
|
Modal.createTrackedDialog('Error decrypting attachment', '', ErrorDialog, {
|
|
|
|
title: _t("Error"),
|
|
|
|
description: _t("Error decrypting attachment"),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private onPlaceholderClick = async () => {
|
|
|
|
const mediaHelper = this.props.mediaEventHelper;
|
2021-08-16 16:30:48 +03:00
|
|
|
if (mediaHelper?.media.isEncrypted) {
|
2021-07-30 00:36:50 +03:00
|
|
|
await this.decryptFile();
|
|
|
|
this.downloadFile(this.fileName, this.linkText);
|
|
|
|
} else {
|
|
|
|
// As a button we're missing the `download` attribute for styling reasons, so
|
|
|
|
// download with the file downloader.
|
|
|
|
this.fileDownloader.download({
|
|
|
|
blob: await mediaHelper.sourceBlob.value,
|
|
|
|
name: this.fileName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-16 01:48:21 +03:00
|
|
|
public render() {
|
2021-08-16 16:30:48 +03:00
|
|
|
const isEncrypted = this.props.mediaEventHelper?.media.isEncrypted;
|
2021-07-16 01:48:21 +03:00
|
|
|
const contentUrl = this.getContentUrl();
|
2021-07-30 00:57:54 +03:00
|
|
|
const fileSize = this.content.info ? this.content.info.size : null;
|
|
|
|
const fileType = this.content.info ? this.content.info.mimetype : "application/octet-stream";
|
2015-11-27 18:02:32 +03:00
|
|
|
|
2021-07-30 00:36:50 +03:00
|
|
|
let placeholder: React.ReactNode = null;
|
2021-03-05 06:07:48 +03:00
|
|
|
if (this.props.showGenericPlaceholder) {
|
|
|
|
placeholder = (
|
2021-07-30 00:36:50 +03:00
|
|
|
<AccessibleButton className="mx_MediaBody mx_MFileBody_info" onClick={this.onPlaceholderClick}>
|
2021-03-05 06:07:48 +03:00
|
|
|
<span className="mx_MFileBody_info_icon" />
|
2021-07-30 17:56:55 +03:00
|
|
|
<TextWithTooltip tooltip={presentableTextForFile(this.content, _t("Attachment"), true)}>
|
2021-07-30 00:55:45 +03:00
|
|
|
<span className="mx_MFileBody_info_filename">
|
2021-07-30 00:57:54 +03:00
|
|
|
{ presentableTextForFile(this.content, _t("Attachment"), true, true) }
|
2021-07-30 00:55:45 +03:00
|
|
|
</span>
|
2021-07-30 00:49:23 +03:00
|
|
|
</TextWithTooltip>
|
2021-07-30 00:36:50 +03:00
|
|
|
</AccessibleButton>
|
2021-03-05 06:07:48 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-16 20:05:04 +03:00
|
|
|
const showDownloadLink = this.props.tileShape || !this.props.showGenericPlaceholder;
|
|
|
|
|
2016-12-02 17:21:07 +03:00
|
|
|
if (isEncrypted) {
|
2021-07-16 01:48:21 +03:00
|
|
|
if (!this.state.decryptedBlob) {
|
2016-12-02 17:21:07 +03:00
|
|
|
// Need to decrypt the attachment
|
|
|
|
// Wait for the user to click on the link before downloading
|
|
|
|
// and decrypting the attachment.
|
|
|
|
|
2020-03-03 14:03:40 +03:00
|
|
|
// This button should actually Download because usercontent/ will try to click itself
|
|
|
|
// but it is not guaranteed between various browsers' settings.
|
2016-12-02 17:21:07 +03:00
|
|
|
return (
|
2019-12-08 15:12:06 +03:00
|
|
|
<span className="mx_MFileBody">
|
2021-07-16 20:05:04 +03:00
|
|
|
{ placeholder }
|
|
|
|
{ showDownloadLink && <div className="mx_MFileBody_download">
|
2021-07-30 00:36:50 +03:00
|
|
|
<AccessibleButton onClick={this.decryptFile}>
|
|
|
|
{ _t("Decrypt %(text)s", { text: this.linkText }) }
|
2020-02-08 01:07:29 +03:00
|
|
|
</AccessibleButton>
|
2021-07-16 20:05:04 +03:00
|
|
|
</div> }
|
2016-12-02 17:21:07 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-13 19:53:45 +03:00
|
|
|
const url = "usercontent/"; // XXX: this path should probably be passed from the skin
|
|
|
|
|
2020-02-08 01:07:29 +03:00
|
|
|
// If the attachment is encrypted then put the link inside an iframe.
|
2016-11-04 17:00:26 +03:00
|
|
|
return (
|
2016-12-02 17:21:07 +03:00
|
|
|
<span className="mx_MFileBody">
|
2021-07-16 20:05:04 +03:00
|
|
|
{ placeholder }
|
|
|
|
{ showDownloadLink && <div className="mx_MFileBody_download">
|
2021-06-29 15:11:58 +03:00
|
|
|
<div style={{ display: "none" }}>
|
2017-10-11 19:56:17 +03:00
|
|
|
{ /*
|
2016-12-02 17:21:07 +03:00
|
|
|
* Add dummy copy of the "a" tag
|
|
|
|
* We'll use it to learn how the download link
|
|
|
|
* would have been styled if it was rendered inline.
|
2017-10-11 19:56:17 +03:00
|
|
|
*/ }
|
2021-07-16 01:48:21 +03:00
|
|
|
<a ref={this.dummyLink} />
|
2016-12-02 17:21:07 +03:00
|
|
|
</div>
|
2021-07-30 00:36:50 +03:00
|
|
|
{ /*
|
|
|
|
TODO: Move iframe (and dummy link) into FileDownloader.
|
|
|
|
We currently have it set up this way because of styles applied to the iframe
|
|
|
|
itself which cannot be easily handled/overridden by the FileDownloader. In
|
|
|
|
future, the download link may disappear entirely at which point it could also
|
|
|
|
be suitable to just remove this bit of code.
|
|
|
|
*/ }
|
2020-02-13 19:53:45 +03:00
|
|
|
<iframe
|
2021-02-16 21:03:12 +03:00
|
|
|
src={url}
|
2021-07-30 00:36:50 +03:00
|
|
|
onLoad={() => this.downloadFile(this.fileName, this.linkText)}
|
2021-07-16 01:48:21 +03:00
|
|
|
ref={this.iframe}
|
2020-03-03 14:03:40 +03:00
|
|
|
sandbox="allow-scripts allow-downloads allow-downloads-without-user-activation" />
|
2021-07-16 20:05:04 +03:00
|
|
|
</div> }
|
2016-11-04 17:00:26 +03:00
|
|
|
</span>
|
|
|
|
);
|
2016-12-02 17:21:07 +03:00
|
|
|
} else if (contentUrl) {
|
2019-03-29 23:12:48 +03:00
|
|
|
const downloadProps = {
|
|
|
|
target: "_blank",
|
2020-02-24 01:14:29 +03:00
|
|
|
rel: "noreferrer noopener",
|
2019-03-29 23:12:48 +03:00
|
|
|
|
|
|
|
// We set the href regardless of whether or not we intercept the download
|
|
|
|
// because we don't really want to convert the file to a blob eagerly, and
|
|
|
|
// still want "open in new tab" and "save link as" to work.
|
|
|
|
href: contentUrl,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Blobs can only have up to 500mb, so if the file reports as being too large then
|
|
|
|
// we won't try and convert it. Likewise, if the file size is unknown then we'll assume
|
|
|
|
// it is too big. There is the risk of the reported file size and the actual file size
|
|
|
|
// being different, however the user shouldn't normally run into this problem.
|
|
|
|
const fileTooBig = typeof(fileSize) === 'number' ? fileSize > 524288000 : true;
|
|
|
|
|
|
|
|
if (["application/pdf"].includes(fileType) && !fileTooBig) {
|
|
|
|
// We want to force a download on this type, so use an onClick handler.
|
|
|
|
downloadProps["onClick"] = (e) => {
|
|
|
|
console.log(`Downloading ${fileType} as blob (unencrypted)`);
|
|
|
|
|
|
|
|
// Avoid letting the <a> do its thing
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
// Start a fetch for the download
|
|
|
|
// Based upon https://stackoverflow.com/a/49500465
|
2021-07-16 01:48:21 +03:00
|
|
|
this.props.mediaEventHelper.sourceBlob.value.then((blob) => {
|
2019-03-29 23:12:48 +03:00
|
|
|
const blobUrl = URL.createObjectURL(blob);
|
|
|
|
|
|
|
|
// We have to create an anchor to download the file
|
|
|
|
const tempAnchor = document.createElement('a');
|
2021-07-30 00:36:50 +03:00
|
|
|
tempAnchor.download = this.fileName;
|
2019-03-29 23:12:48 +03:00
|
|
|
tempAnchor.href = blobUrl;
|
|
|
|
document.body.appendChild(tempAnchor); // for firefox: https://stackoverflow.com/a/32226068
|
|
|
|
tempAnchor.click();
|
|
|
|
tempAnchor.remove();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// Else we are hoping the browser will do the right thing
|
2021-07-30 00:36:50 +03:00
|
|
|
downloadProps["download"] = this.fileName;
|
2019-03-29 23:12:48 +03:00
|
|
|
}
|
|
|
|
|
2021-07-16 20:05:04 +03:00
|
|
|
return (
|
|
|
|
<span className="mx_MFileBody">
|
|
|
|
{ placeholder }
|
|
|
|
{ showDownloadLink && <div className="mx_MFileBody_download">
|
|
|
|
<a {...downloadProps}>
|
|
|
|
<span className="mx_MFileBody_download_icon" />
|
2021-07-30 00:36:50 +03:00
|
|
|
{ _t("Download %(text)s", { text: this.linkText }) }
|
2021-07-16 20:05:04 +03:00
|
|
|
</a>
|
|
|
|
{ this.props.tileShape === TileShape.FileGrid && <div className="mx_MImageBody_size">
|
2021-07-30 00:57:54 +03:00
|
|
|
{ this.content.info && this.content.info.size ? filesize(this.content.info.size) : "" }
|
2021-07-20 18:03:26 +03:00
|
|
|
</div> }
|
2021-07-16 20:05:04 +03:00
|
|
|
</div> }
|
|
|
|
</span>
|
|
|
|
);
|
2015-11-27 18:02:32 +03:00
|
|
|
} else {
|
2021-07-30 00:36:50 +03:00
|
|
|
const extra = this.linkText ? (': ' + this.linkText) : '';
|
2016-01-03 03:11:11 +03:00
|
|
|
return <span className="mx_MFileBody">
|
2021-07-16 20:05:04 +03:00
|
|
|
{ placeholder }
|
2017-05-23 17:16:31 +03:00
|
|
|
{ _t("Invalid file%(extra)s", { extra: extra }) }
|
2017-01-20 17:22:27 +03:00
|
|
|
</span>;
|
2015-11-27 18:02:32 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
}
|