2017-09-08 05:15:05 +02:00
|
|
|
import React from "react";
|
|
|
|
import { Icon, BusyMessage } from "component/common";
|
|
|
|
import Link from "component/link";
|
|
|
|
|
|
|
|
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,
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (loading || downloading) {
|
|
|
|
const progress = fileInfo && fileInfo.written_bytes
|
|
|
|
? fileInfo.written_bytes / fileInfo.total_bytes * 100
|
|
|
|
: 0,
|
|
|
|
label = fileInfo
|
|
|
|
? progress.toFixed(0) + __("% complete")
|
|
|
|
: __("Connecting..."),
|
|
|
|
labelWithIcon = (
|
|
|
|
<span className="button__content">
|
|
|
|
<Icon icon="icon-download" />
|
|
|
|
<span>
|
|
|
|
{label}
|
|
|
|
</span>
|
|
|
|
</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-09-08 05:15:05 +02:00
|
|
|
style={{ width: progress + "%" }}
|
|
|
|
>
|
|
|
|
{labelWithIcon}
|
|
|
|
</div>
|
|
|
|
{labelWithIcon}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
} else if (fileInfo === null && !downloading) {
|
|
|
|
if (!costInfo) {
|
|
|
|
return <BusyMessage message={__("Fetching cost info")} />;
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<Link
|
|
|
|
button="text"
|
|
|
|
label={__("Download")}
|
|
|
|
icon="icon-download"
|
|
|
|
onClick={() => {
|
2017-09-18 04:08:43 +02:00
|
|
|
purchaseUri(uri);
|
2017-09-08 05:15:05 +02:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (fileInfo && fileInfo.download_path) {
|
|
|
|
return (
|
|
|
|
<Link
|
|
|
|
label={__("Open")}
|
|
|
|
button="text"
|
|
|
|
icon="icon-external-link-square"
|
2017-09-20 14:47:08 +02:00
|
|
|
onClick={() => openInShell(fileInfo.download_path)}
|
2017-09-08 05:15:05 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FileDownloadLink;
|