2017-12-21 22:08:54 +01:00
|
|
|
import React from 'react';
|
2018-01-04 23:05:29 +01:00
|
|
|
import { BusyMessage } from 'component/common';
|
2018-01-09 02:15:44 +01:00
|
|
|
import Icon from 'component/icon';
|
2017-12-21 22:08:54 +01:00
|
|
|
import Link from 'component/link';
|
2017-09-08 05:15:05 +02:00
|
|
|
|
|
|
|
class FileDownloadLink extends React.PureComponent {
|
|
|
|
componentWillMount() {
|
|
|
|
this.checkAvailability(this.props.uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
this.checkAvailability(nextProps.uri);
|
|
|
|
this.restartDownload(nextProps);
|
|
|
|
}
|
|
|
|
|
|
|
|
restartDownload(props) {
|
|
|
|
const { downloading, fileInfo, uri, restartDownload } = props;
|
|
|
|
|
|
|
|
if (
|
|
|
|
!downloading &&
|
|
|
|
fileInfo &&
|
|
|
|
!fileInfo.completed &&
|
|
|
|
fileInfo.written_bytes !== false &&
|
|
|
|
fileInfo.written_bytes < fileInfo.total_bytes
|
|
|
|
) {
|
|
|
|
restartDownload(uri, fileInfo.outpoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkAvailability(uri) {
|
|
|
|
if (!this._uri || uri !== this._uri) {
|
|
|
|
this._uri = uri;
|
|
|
|
this.props.checkAvailability(uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = () => {
|
|
|
|
openInShell(fileInfo.download_path);
|
2017-12-21 00:38:11 +01:00
|
|
|
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 =
|
|
|
|
fileInfo && fileInfo.written_bytes
|
|
|
|
? fileInfo.written_bytes / fileInfo.total_bytes * 100
|
|
|
|
: 0,
|
2017-12-21 22:08:54 +01:00
|
|
|
label = fileInfo ? progress.toFixed(0) + __('% complete') : __('Connecting...'),
|
2017-09-08 05:15:05 +02:00
|
|
|
labelWithIcon = (
|
|
|
|
<span className="button__content">
|
|
|
|
<Icon icon="icon-download" />
|
2017-11-24 15:31:05 +01:00
|
|
|
<span>{label}</span>
|
2017-09-08 05:15:05 +02:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2017-09-15 15:56:32 +02:00
|
|
|
<div className="faux-button-block file-download button-set-item">
|
2017-09-08 05:15:05 +02:00
|
|
|
<div
|
2017-09-15 15:56:32 +02:00
|
|
|
className="faux-button-block file-download__overlay"
|
2017-12-21 22:08:54 +01:00
|
|
|
style={{ width: `${progress}%` }}
|
2017-09-08 05:15:05 +02:00
|
|
|
>
|
|
|
|
{labelWithIcon}
|
|
|
|
</div>
|
|
|
|
{labelWithIcon}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
} else if (fileInfo === null && !downloading) {
|
|
|
|
if (!costInfo) {
|
2017-12-21 22:08:54 +01:00
|
|
|
return <BusyMessage message={__('Fetching cost info')} />;
|
2017-09-08 05:15:05 +02:00
|
|
|
}
|
2017-12-21 22:08:54 +01:00
|
|
|
return (
|
|
|
|
<Link
|
|
|
|
button="text"
|
|
|
|
label={__('Download')}
|
|
|
|
icon="icon-download"
|
|
|
|
className="no-underline"
|
|
|
|
onClick={() => {
|
|
|
|
purchaseUri(uri);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
2017-09-08 05:15:05 +02:00
|
|
|
} else if (fileInfo && fileInfo.download_path) {
|
|
|
|
return (
|
|
|
|
<Link
|
2017-12-21 22:08:54 +01:00
|
|
|
label={__('Open')}
|
2017-09-08 05:15:05 +02:00
|
|
|
button="text"
|
|
|
|
icon="icon-external-link-square"
|
2017-10-05 06:13:06 +02:00
|
|
|
className="no-underline"
|
2017-12-18 18:02:55 +01:00
|
|
|
onClick={() => openFile()}
|
2017-09-08 05:15:05 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FileDownloadLink;
|