2018-05-21 22:30:00 +02:00
|
|
|
// @flow
|
2018-11-26 02:21:25 +01:00
|
|
|
import type { Claim } from 'types/claim';
|
|
|
|
import * as ICONS from 'constants/icons';
|
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';
|
2018-06-20 05:55:25 +02:00
|
|
|
import ToolTip from 'component/common/tooltip';
|
2018-08-22 19:17:11 +02:00
|
|
|
import analytics from 'analytics';
|
2017-09-08 05:15:05 +02:00
|
|
|
|
2018-05-21 22:30:00 +02:00
|
|
|
type Props = {
|
2018-08-22 19:17:11 +02:00
|
|
|
claim: Claim,
|
2018-05-21 22:30:00 +02:00
|
|
|
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,
|
2018-07-31 15:29:28 +02:00
|
|
|
pause: () => void,
|
2018-05-21 22:30:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
2018-07-31 15:29:28 +02:00
|
|
|
pause,
|
2018-09-19 17:55:14 +02:00
|
|
|
claim,
|
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);
|
2018-07-31 15:29:28 +02:00
|
|
|
pause();
|
2018-05-21 22:30:00 +02:00
|
|
|
}
|
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"
|
2018-11-26 02:21:25 +01:00
|
|
|
icon={ICONS.DOWNLOAD}
|
2018-06-25 08:28:03 +02:00
|
|
|
iconColor="green"
|
2018-06-20 05:55:25 +02:00
|
|
|
onClick={() => {
|
|
|
|
purchaseUri(uri);
|
2018-08-22 19:17:11 +02:00
|
|
|
|
|
|
|
const { name, claim_id: claimId, nout, txid } = claim;
|
|
|
|
// // ideally outpoint would exist inside of claim information
|
|
|
|
// // we can use it after https://github.com/lbryio/lbry/issues/1306 is addressed
|
|
|
|
const outpoint = `${txid}:${nout}`;
|
|
|
|
analytics.apiLogView(`${name}#${claimId}`, outpoint, claimId);
|
2018-06-20 05:55:25 +02:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</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')}>
|
2019-01-22 21:36:28 +01:00
|
|
|
<Button button="alt" iconColor="green" icon={ICONS.EXTERNAL} onClick={() => openFile()} />
|
2018-06-20 05:55:25 +02:00
|
|
|
</ToolTip>
|
2017-09-08 05:15:05 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FileDownloadLink;
|