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