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';
|
2020-02-01 16:17:20 -05:00
|
|
|
import React, { useState } from 'react';
|
2018-03-26 14:32:43 -07:00
|
|
|
import Button from 'component/button';
|
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-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,
|
2021-02-24 10:19:10 +08:00
|
|
|
download: (string) => void,
|
2020-01-07 14:02:26 -05:00
|
|
|
costInfo: ?{ cost: string },
|
2020-04-01 14:43:50 -04:00
|
|
|
buttonType: ?string,
|
|
|
|
showLabel: ?boolean,
|
2020-01-29 13:58:43 -05:00
|
|
|
hideOpenButton: boolean,
|
2020-02-04 21:54:19 -05:00
|
|
|
hideDownloadStatus: boolean,
|
2020-05-21 11:38:28 -04:00
|
|
|
streamingUrl: ?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,
|
2020-09-30 16:46:20 -04:00
|
|
|
buttonType,
|
2020-04-01 14:43:50 -04:00
|
|
|
showLabel = false,
|
2020-01-29 13:58:43 -05:00
|
|
|
hideOpenButton = false,
|
2020-02-04 21:54:19 -05:00
|
|
|
hideDownloadStatus = false,
|
2020-05-21 11:38:28 -04:00
|
|
|
streamingUrl,
|
2020-01-07 14:02:26 -05:00
|
|
|
} = props;
|
2020-01-30 11:48:14 -05:00
|
|
|
|
2020-05-21 11:38:28 -04:00
|
|
|
const [didClickDownloadButton, setDidClickDownloadButton] = useState(false);
|
|
|
|
const fileName = claim && claim.value && claim.value.source && claim.value.source.name;
|
2020-02-01 16:17:20 -05:00
|
|
|
|
2020-06-08 16:26:49 -04:00
|
|
|
// @if TARGET='web'
|
2020-05-21 11:38:28 -04:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (didClickDownloadButton && streamingUrl) {
|
|
|
|
let element = document.createElement('a');
|
|
|
|
element.setAttribute('href', `${streamingUrl}?download=true`);
|
|
|
|
element.setAttribute('download', fileName);
|
|
|
|
element.style.display = 'none';
|
|
|
|
// $FlowFixMe
|
|
|
|
document.body.appendChild(element);
|
|
|
|
element.click();
|
|
|
|
// $FlowFixMe
|
|
|
|
document.body.removeChild(element);
|
2020-01-30 11:48:14 -05:00
|
|
|
|
2020-05-21 11:38:28 -04:00
|
|
|
setDidClickDownloadButton(false);
|
|
|
|
}
|
2020-12-01 12:56:59 -05:00
|
|
|
}, [streamingUrl, didClickDownloadButton, fileName]);
|
2020-06-08 16:26:49 -04:00
|
|
|
// @endif
|
2017-09-07 23:15:05 -04:00
|
|
|
|
2020-01-29 13:58:43 -05:00
|
|
|
function handleDownload(e) {
|
2020-05-21 11:38:28 -04:00
|
|
|
setDidClickDownloadButton(true);
|
2020-02-01 16:17:20 -05:00
|
|
|
e.preventDefault();
|
2020-01-07 14:02:26 -05:00
|
|
|
download(uri);
|
|
|
|
}
|
|
|
|
|
2020-05-21 11:38:28 -04:00
|
|
|
if (!claim) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @if TARGET='app'
|
2019-08-14 22:50:41 -04:00
|
|
|
if (downloading || loading) {
|
2020-05-26 18:31:49 +08:00
|
|
|
if (hideDownloadStatus) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-03-26 14:32:43 -07:00
|
|
|
|
2020-05-26 18:31:49 +08:00
|
|
|
if (fileInfo && fileInfo.written_bytes > 0) {
|
|
|
|
const progress = (fileInfo.written_bytes / fileInfo.total_bytes) * 100;
|
|
|
|
return <span className="download-text">{__('%percent%% downloaded', { percent: progress.toFixed(0) })}</span>;
|
|
|
|
} else {
|
|
|
|
return <span className="download-text">{__('Connecting...')}</span>;
|
|
|
|
}
|
2019-08-02 02:28:14 -04:00
|
|
|
}
|
2020-05-21 11:38:28 -04:00
|
|
|
// @endif
|
2017-09-07 23:15:05 -04:00
|
|
|
|
2019-08-02 02:28:14 -04:00
|
|
|
if (fileInfo && fileInfo.download_path && fileInfo.completed) {
|
2020-04-01 14:43:50 -04:00
|
|
|
const openLabel = __('Open file');
|
2020-01-29 13:58:43 -05:00
|
|
|
return hideOpenButton ? null : (
|
|
|
|
<Button
|
2020-04-01 14:43:50 -04:00
|
|
|
button={buttonType}
|
2021-02-24 10:19:10 +08:00
|
|
|
className={buttonType ? undefined : 'button--file-action'}
|
2020-04-01 14:43:50 -04:00
|
|
|
title={openLabel}
|
|
|
|
label={showLabel ? openLabel : null}
|
2020-01-29 13:58:43 -05:00
|
|
|
icon={ICONS.EXTERNAL}
|
|
|
|
onClick={() => {
|
|
|
|
pause();
|
|
|
|
openModal(MODALS.CONFIRM_EXTERNAL_RESOURCE, { path: fileInfo.download_path, isMine: claimIsMine });
|
|
|
|
}}
|
|
|
|
/>
|
2019-08-02 02:28:14 -04:00
|
|
|
);
|
2017-09-07 23:15:05 -04:00
|
|
|
}
|
2020-01-07 14:02:26 -05:00
|
|
|
|
2020-05-21 11:38:28 -04:00
|
|
|
const label = __('Download');
|
2020-04-01 14:43:50 -04:00
|
|
|
|
2020-01-07 14:02:26 -05:00
|
|
|
return (
|
2020-01-29 13:58:43 -05:00
|
|
|
<Button
|
2020-04-01 14:43:50 -04:00
|
|
|
button={buttonType}
|
2020-09-30 16:46:20 -04:00
|
|
|
className={buttonType ? undefined : 'button--file-action'}
|
2020-04-01 14:43:50 -04:00
|
|
|
title={label}
|
2020-01-29 13:58:43 -05:00
|
|
|
icon={ICONS.DOWNLOAD}
|
2020-04-01 14:43:50 -04:00
|
|
|
label={showLabel ? label : null}
|
2020-01-29 13:58:43 -05:00
|
|
|
onClick={handleDownload}
|
|
|
|
/>
|
2020-01-07 14:02:26 -05:00
|
|
|
);
|
2017-09-07 23:15:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default FileDownloadLink;
|