2016-04-13 02:00:24 +03:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import MFileBody from './MFileBody';
|
|
|
|
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from '../../../MatrixClientPeg';
|
2018-04-29 05:07:31 +03:00
|
|
|
import { decryptFile } from '../../../utils/DecryptFile';
|
2017-05-30 17:09:57 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2016-04-13 02:00:24 +03:00
|
|
|
|
|
|
|
export default class MAudioBody extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2016-11-04 16:45:00 +03:00
|
|
|
playing: false,
|
|
|
|
decryptedUrl: null,
|
2016-12-02 17:21:07 +03:00
|
|
|
decryptedBlob: null,
|
2016-11-18 23:08:26 +03:00
|
|
|
error: null,
|
2017-01-20 17:22:27 +03:00
|
|
|
};
|
2016-04-13 02:00:24 +03:00
|
|
|
}
|
|
|
|
onPlayToggle() {
|
|
|
|
this.setState({
|
2017-10-11 19:56:17 +03:00
|
|
|
playing: !this.state.playing,
|
2016-04-13 02:00:24 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-04 16:45:00 +03:00
|
|
|
_getContentUrl() {
|
2016-11-08 14:42:20 +03:00
|
|
|
const content = this.props.mxEvent.getContent();
|
2016-11-04 16:45:00 +03:00
|
|
|
if (content.file !== undefined) {
|
|
|
|
return this.state.decryptedUrl;
|
|
|
|
} else {
|
|
|
|
return MatrixClientPeg.get().mxcUrlToHttp(content.url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2017-10-11 19:56:17 +03:00
|
|
|
const content = this.props.mxEvent.getContent();
|
2016-11-04 16:45:00 +03:00
|
|
|
if (content.file !== undefined && this.state.decryptedUrl === null) {
|
2017-10-11 19:56:17 +03:00
|
|
|
let decryptedBlob;
|
2016-12-02 17:21:07 +03:00
|
|
|
decryptFile(content.file).then(function(blob) {
|
|
|
|
decryptedBlob = blob;
|
2018-04-29 05:07:31 +03:00
|
|
|
return URL.createObjectURL(decryptedBlob);
|
2019-11-18 13:03:05 +03:00
|
|
|
}).then((url) => {
|
2016-11-04 18:39:39 +03:00
|
|
|
this.setState({
|
2016-11-18 23:08:26 +03:00
|
|
|
decryptedUrl: url,
|
2016-12-02 17:21:07 +03:00
|
|
|
decryptedBlob: decryptedBlob,
|
2016-11-04 18:39:39 +03:00
|
|
|
});
|
2016-11-08 14:42:20 +03:00
|
|
|
}, (err) => {
|
2016-11-18 23:08:26 +03:00
|
|
|
console.warn("Unable to decrypt attachment: ", err);
|
|
|
|
this.setState({
|
|
|
|
error: err,
|
|
|
|
});
|
2016-11-04 16:45:00 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-29 05:17:55 +03:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.state.decryptedUrl) {
|
|
|
|
URL.revokeObjectURL(this.state.decryptedUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-13 02:00:24 +03:00
|
|
|
render() {
|
2016-11-08 14:42:20 +03:00
|
|
|
const content = this.props.mxEvent.getContent();
|
2016-11-04 16:45:00 +03:00
|
|
|
|
2016-11-18 23:08:26 +03:00
|
|
|
if (this.state.error !== null) {
|
|
|
|
return (
|
2019-12-08 15:12:06 +03:00
|
|
|
<span className="mx_MAudioBody">
|
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 audio") }
|
2016-11-18 23:08:26 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-04 16:45:00 +03:00
|
|
|
if (content.file !== undefined && this.state.decryptedUrl === null) {
|
|
|
|
// Need to decrypt the attachment
|
|
|
|
// The attachment is decrypted in componentDidMount.
|
2016-11-18 23:08:26 +03:00
|
|
|
// For now add an img tag with a 16x16 spinner.
|
|
|
|
// Not sure how tall the audio player is so not sure how tall it should actually be.
|
2016-11-04 16:45:00 +03:00
|
|
|
return (
|
|
|
|
<span className="mx_MAudioBody">
|
2019-01-11 04:37:28 +03:00
|
|
|
<img src={require("../../../../res/img/spinner.gif")} alt={content.body} width="16" height="16" />
|
2016-11-04 16:45:00 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-08 14:42:20 +03:00
|
|
|
const contentUrl = this._getContentUrl();
|
2016-04-13 02:00:24 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<span className="mx_MAudioBody">
|
2016-11-04 16:45:00 +03:00
|
|
|
<audio src={contentUrl} controls />
|
2016-12-02 17:21:07 +03:00
|
|
|
<MFileBody {...this.props} decryptedBlob={this.state.decryptedBlob} />
|
2016-04-13 02:00:24 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|