2018-05-21 22:30:00 +02:00
|
|
|
// @flow
|
2017-12-21 22:08:54 +01:00
|
|
|
import React from 'react';
|
2018-03-26 23:32:43 +02:00
|
|
|
import Button from 'component/button';
|
|
|
|
import * as icons from 'constants/icons';
|
2018-06-20 05:55:25 +02:00
|
|
|
import ToolTip from 'component/common/tooltip';
|
2017-09-08 05:15:05 +02:00
|
|
|
|
2018-05-21 22:30:00 +02:00
|
|
|
type Props = {
|
|
|
|
uri: string,
|
|
|
|
downloading: boolean,
|
|
|
|
fileInfo: ?{
|
|
|
|
written_bytes: number,
|
|
|
|
total_bytes: number,
|
|
|
|
outpoint: number,
|
|
|
|
download_path: string,
|
2018-05-23 02:29:29 +02:00
|
|
|
completed: boolean,
|
2018-05-21 22:30:00 +02:00
|
|
|
},
|
|
|
|
loading: boolean,
|
|
|
|
costInfo: ?{},
|
|
|
|
restartDownload: (string, number) => void,
|
|
|
|
openInShell: string => void,
|
|
|
|
purchaseUri: string => void,
|
|
|
|
doPause: () => void,
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileDownloadLink extends React.PureComponent<Props> {
|
2018-05-23 02:29:29 +02:00
|
|
|
componentWillUpdate() {
|
|
|
|
const { downloading, fileInfo, uri, restartDownload } = this.props;
|
2017-09-08 05:15:05 +02:00
|
|
|
if (
|
|
|
|
!downloading &&
|
|
|
|
fileInfo &&
|
|
|
|
!fileInfo.completed &&
|
|
|
|
fileInfo.written_bytes !== false &&
|
|
|
|
fileInfo.written_bytes < fileInfo.total_bytes
|
|
|
|
) {
|
|
|
|
restartDownload(uri, fileInfo.outpoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 02:29:29 +02:00
|
|
|
uri: ?string;
|
|
|
|
|
2017-09-08 05:15:05 +02:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
fileInfo,
|
|
|
|
downloading,
|
|
|
|
uri,
|
|
|
|
openInShell,
|
2017-09-18 04:08:43 +02:00
|
|
|
purchaseUri,
|
2017-09-08 05:15:05 +02:00
|
|
|
costInfo,
|
|
|
|
loading,
|
2017-12-21 00:38:11 +01:00
|
|
|
doPause,
|
2017-09-08 05:15:05 +02:00
|
|
|
} = this.props;
|
|
|
|
|
2017-12-18 18:02:55 +01:00
|
|
|
const openFile = () => {
|
2018-05-21 22:30:00 +02:00
|
|
|
if (fileInfo) {
|
|
|
|
openInShell(fileInfo.download_path);
|
|
|
|
doPause();
|
|
|
|
}
|
2017-12-18 18:02:55 +01:00
|
|
|
};
|
|
|
|
|
2017-09-08 05:15:05 +02:00
|
|
|
if (loading || downloading) {
|
2017-11-24 15:31:05 +01:00
|
|
|
const progress =
|
2018-03-26 23:32:43 +02:00
|
|
|
fileInfo && fileInfo.written_bytes
|
2018-06-20 05:55:25 +02:00
|
|
|
? (fileInfo.written_bytes / fileInfo.total_bytes) * 100
|
2018-03-26 23:32:43 +02:00
|
|
|
: 0;
|
2018-05-21 22:30:00 +02:00
|
|
|
const label = fileInfo
|
|
|
|
? __('Downloading: ') + progress.toFixed(0) + __('% complete')
|
|
|
|
: __('Connecting...');
|
2017-09-08 05:15:05 +02:00
|
|
|
|
2018-05-21 22:30:00 +02:00
|
|
|
return <span className="file-download">{label}</span>;
|
2017-09-08 05:15:05 +02:00
|
|
|
} else if (fileInfo === null && !downloading) {
|
|
|
|
if (!costInfo) {
|
2018-03-26 23:32:43 +02:00
|
|
|
return null;
|
2017-09-08 05:15:05 +02:00
|
|
|
}
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2017-12-21 22:08:54 +01:00
|
|
|
return (
|
2018-06-20 05:55:25 +02:00
|
|
|
<ToolTip onComponent body={__('Download')}>
|
|
|
|
<Button
|
|
|
|
button="alt"
|
|
|
|
icon={icons.DOWNLOAD}
|
2018-06-25 08:28:03 +02:00
|
|
|
iconColor="green"
|
2018-06-20 05:55:25 +02:00
|
|
|
onClick={() => {
|
|
|
|
purchaseUri(uri);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</ToolTip>
|
2017-12-21 22:08:54 +01:00
|
|
|
);
|
2017-09-08 05:15:05 +02:00
|
|
|
} else if (fileInfo && fileInfo.download_path) {
|
|
|
|
return (
|
2018-06-20 05:55:25 +02:00
|
|
|
<ToolTip onComponent body={__('Open file')}>
|
2018-06-25 08:28:03 +02:00
|
|
|
<Button button="alt" iconColor="green" icon={icons.LOCAL} onClick={() => openFile()} />
|
2018-06-20 05:55:25 +02:00
|
|
|
</ToolTip>
|
2017-09-08 05:15:05 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FileDownloadLink;
|