|
| 1 | +/* |
| 2 | +Copyright 2021 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { MatrixEvent } from "matrix-js-sdk/src"; |
| 18 | +import { MediaEventHelper } from "../../../utils/MediaEventHelper"; |
| 19 | +import React, { createRef } from "react"; |
| 20 | +import { RovingAccessibleTooltipButton } from "../../../accessibility/RovingTabIndex"; |
| 21 | +import Spinner from "../elements/Spinner"; |
| 22 | +import classNames from "classnames"; |
| 23 | +import { _t } from "../../../languageHandler"; |
| 24 | +import { replaceableComponent } from "../../../utils/replaceableComponent"; |
| 25 | + |
| 26 | +interface IProps { |
| 27 | + mxEvent: MatrixEvent; |
| 28 | + |
| 29 | + // XXX: It can take a cycle or two for the MessageActionBar to have all the props/setup |
| 30 | + // required to get us a MediaEventHelper, so we use a getter function instead to prod for |
| 31 | + // one. |
| 32 | + mediaEventHelperGet: () => MediaEventHelper; |
| 33 | +} |
| 34 | + |
| 35 | +interface IState { |
| 36 | + loading: boolean; |
| 37 | + blob?: Blob; |
| 38 | +} |
| 39 | + |
| 40 | +@replaceableComponent("views.messages.DownloadActionButton") |
| 41 | +export default class DownloadActionButton extends React.PureComponent<IProps, IState> { |
| 42 | + private iframe: React.RefObject<HTMLIFrameElement> = createRef(); |
| 43 | + |
| 44 | + public constructor(props: IProps) { |
| 45 | + super(props); |
| 46 | + |
| 47 | + this.state = { |
| 48 | + loading: false, |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + private onDownloadClick = async () => { |
| 53 | + if (this.state.loading) return; |
| 54 | + |
| 55 | + this.setState({ loading: true }); |
| 56 | + |
| 57 | + if (this.state.blob) { |
| 58 | + // Cheat and trigger a download, again. |
| 59 | + return this.onFrameLoad(); |
| 60 | + } |
| 61 | + |
| 62 | + const blob = await this.props.mediaEventHelperGet().sourceBlob.value; |
| 63 | + this.setState({ blob }); |
| 64 | + }; |
| 65 | + |
| 66 | + private onFrameLoad = () => { |
| 67 | + this.setState({ loading: false }); |
| 68 | + |
| 69 | + // we aren't showing the iframe, so we can send over the bare minimum styles and such. |
| 70 | + this.iframe.current.contentWindow.postMessage({ |
| 71 | + imgSrc: "", // no image |
| 72 | + imgStyle: null, |
| 73 | + style: "", |
| 74 | + blob: this.state.blob, |
| 75 | + download: this.props.mediaEventHelperGet().fileName, |
| 76 | + textContent: "", |
| 77 | + auto: true, // autodownload |
| 78 | + }, '*'); |
| 79 | + }; |
| 80 | + |
| 81 | + public render() { |
| 82 | + let spinner: JSX.Element; |
| 83 | + if (this.state.loading) { |
| 84 | + spinner = <Spinner w={18} h={18} />; |
| 85 | + } |
| 86 | + |
| 87 | + const classes = classNames({ |
| 88 | + 'mx_MessageActionBar_maskButton': true, |
| 89 | + 'mx_MessageActionBar_downloadButton': true, |
| 90 | + 'mx_MessageActionBar_downloadSpinnerButton': !!spinner, |
| 91 | + }); |
| 92 | + |
| 93 | + return <RovingAccessibleTooltipButton |
| 94 | + className={classes} |
| 95 | + title={spinner ? _t("Downloading") : _t("Download")} |
| 96 | + onClick={this.onDownloadClick} |
| 97 | + disabled={!!spinner} |
| 98 | + > |
| 99 | + { spinner } |
| 100 | + { this.state.blob && <iframe |
| 101 | + src="usercontent/" // XXX: Like MFileBody, this should come from the skin |
| 102 | + ref={this.iframe} |
| 103 | + onLoad={this.onFrameLoad} |
| 104 | + sandbox="allow-scripts allow-downloads allow-downloads-without-user-activation" |
| 105 | + style={{ display: "none" }} |
| 106 | + /> } |
| 107 | + </RovingAccessibleTooltipButton>; |
| 108 | + } |
| 109 | +} |
0 commit comments