2018-05-21 16:30:00 -04:00
|
|
|
// @flow
|
2018-11-25 20:21:25 -05:00
|
|
|
import * as ICONS from 'constants/icons';
|
2019-05-12 15:46:14 +01:00
|
|
|
import * as MODALS from 'constants/modal_types';
|
2019-11-13 10:20:54 -05:00
|
|
|
import React from 'react';
|
2018-03-26 14:32:43 -07:00
|
|
|
import Button from 'component/button';
|
2018-06-19 23:55:25 -04:00
|
|
|
import ToolTip from 'component/common/tooltip';
|
2017-09-07 23:15:05 -04:00
|
|
|
|
2018-05-21 16:30:00 -04:00
|
|
|
type Props = {
|
2019-08-05 23:25:33 -04:00
|
|
|
uri: string,
|
2020-01-07 13:17:43 -05:00
|
|
|
claim: StreamClaim,
|
2019-07-04 12:23:22 +01:00
|
|
|
claimIsMine: boolean,
|
2018-05-21 16:30:00 -04:00
|
|
|
downloading: boolean,
|
|
|
|
loading: boolean,
|
2019-08-02 02:28:14 -04:00
|
|
|
isStreamable: boolean,
|
2019-08-05 23:25:33 -04:00
|
|
|
fileInfo: ?FileListItem,
|
2019-05-12 15:46:14 +01:00
|
|
|
openModal: (id: string, { path: string }) => void,
|
2018-07-31 09:29:28 -04:00
|
|
|
pause: () => void,
|
2019-08-05 23:25:33 -04:00
|
|
|
download: string => void,
|
2020-01-07 14:02:26 -05:00
|
|
|
triggerViewEvent: string => void,
|
|
|
|
costInfo: ?{ cost: string },
|
2018-05-21 16:30:00 -04:00
|
|
|
};
|
|
|
|
|
2019-08-02 02:28:14 -04:00
|
|
|
function FileDownloadLink(props: Props) {
|
2020-01-07 14:02:26 -05:00
|
|
|
const {
|
|
|
|
fileInfo,
|
|
|
|
downloading,
|
|
|
|
loading,
|
|
|
|
openModal,
|
|
|
|
pause,
|
|
|
|
claimIsMine,
|
|
|
|
download,
|
|
|
|
uri,
|
|
|
|
claim,
|
|
|
|
triggerViewEvent,
|
|
|
|
costInfo,
|
|
|
|
} = props;
|
|
|
|
const cost = costInfo ? Number(costInfo.cost) : 0;
|
|
|
|
const isPaidContent = cost > 0;
|
2020-01-07 13:17:43 -05:00
|
|
|
const { name, claim_id: claimId, value } = claim;
|
|
|
|
const fileName = value && value.source && value.source.name;
|
2020-01-13 12:36:26 -05:00
|
|
|
const downloadUrl = `/$/download/${name}/${claimId}`;
|
2017-09-07 23:15:05 -04:00
|
|
|
|
2020-01-07 14:02:26 -05:00
|
|
|
function handleDownload() {
|
|
|
|
// @if TARGET='app'
|
|
|
|
download(uri);
|
|
|
|
// @endif;
|
|
|
|
// @if TARGET='web'
|
|
|
|
triggerViewEvent(uri);
|
|
|
|
// @endif;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IS_WEB && isPaidContent) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-08-14 22:50:41 -04:00
|
|
|
if (downloading || loading) {
|
2019-08-02 02:28:14 -04:00
|
|
|
const progress = fileInfo && fileInfo.written_bytes > 0 ? (fileInfo.written_bytes / fileInfo.total_bytes) * 100 : 0;
|
|
|
|
const label =
|
2019-08-13 23:56:11 -04:00
|
|
|
fileInfo && fileInfo.written_bytes > 0 ? progress.toFixed(0) + __('% downloaded') : __('Connecting...');
|
2018-03-26 14:32:43 -07:00
|
|
|
|
2019-08-02 02:28:14 -04:00
|
|
|
return <span>{label}</span>;
|
|
|
|
}
|
2017-09-07 23:15:05 -04:00
|
|
|
|
2019-08-02 02:28:14 -04:00
|
|
|
if (fileInfo && fileInfo.download_path && fileInfo.completed) {
|
|
|
|
return (
|
|
|
|
<ToolTip label={__('Open file')}>
|
|
|
|
<Button
|
2019-11-22 16:13:00 -05:00
|
|
|
button="alt"
|
2019-08-02 02:28:14 -04:00
|
|
|
icon={ICONS.EXTERNAL}
|
|
|
|
onClick={() => {
|
|
|
|
pause();
|
|
|
|
openModal(MODALS.CONFIRM_EXTERNAL_RESOURCE, { path: fileInfo.download_path, isMine: claimIsMine });
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</ToolTip>
|
|
|
|
);
|
2017-09-07 23:15:05 -04:00
|
|
|
}
|
2020-01-07 14:02:26 -05:00
|
|
|
|
|
|
|
return (
|
|
|
|
<ToolTip label={IS_WEB ? __('Download') : __('Add to your library')}>
|
|
|
|
<Button
|
|
|
|
button="alt"
|
|
|
|
icon={ICONS.DOWNLOAD}
|
|
|
|
onClick={handleDownload}
|
|
|
|
// @if TARGET='web'
|
|
|
|
download={fileName}
|
|
|
|
href={downloadUrl}
|
|
|
|
// @endif
|
|
|
|
/>
|
|
|
|
</ToolTip>
|
|
|
|
);
|
2017-09-07 23:15:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default FileDownloadLink;
|