lbry-desktop/ui/component/fileRender/view.jsx

251 lines
7.7 KiB
React
Raw Normal View History

2018-06-11 00:41:25 -06:00
// @flow
import { remote } from 'electron';
2019-09-03 10:02:04 -04:00
import React, { Suspense, Fragment } from 'react';
2020-01-06 13:32:35 -05:00
import classnames from 'classnames';
2018-06-11 00:41:25 -06:00
import LoadingScreen from 'component/common/loading-screen';
2019-04-03 00:56:58 -05:00
import VideoViewer from 'component/viewers/videoViewer';
2019-08-05 23:25:33 -04:00
import ImageViewer from 'component/viewers/imageViewer';
import AppViewer from 'component/viewers/appViewer';
2019-09-03 10:02:04 -04:00
import Button from 'component/button';
2019-09-05 20:26:03 -04:00
import { withRouter } from 'react-router-dom';
2020-01-27 13:52:25 -05:00
import AutoplayCountdown from 'component/autoplayCountdown';
// @if TARGET='web'
import { generateStreamUrl } from 'util/lbrytv';
// @endif
2019-09-03 10:02:04 -04:00
2019-08-02 02:28:14 -04:00
import path from 'path';
import fs from 'fs';
2019-09-02 09:24:00 -04:00
import Yrbl from 'component/yrbl';
2019-04-03 00:56:58 -05:00
2019-11-07 14:39:22 -05:00
import DocumentViewer from 'component/viewers/documentViewer';
import PdfViewer from 'component/viewers/pdfViewer';
import HtmlViewer from 'component/viewers/htmlViewer';
// @if TARGET='app'
import DocxViewer from 'component/viewers/docxViewer';
2019-11-07 14:39:22 -05:00
import ComicBookViewer from 'component/viewers/comicBookViewer';
import ThreeViewer from 'component/viewers/threeViewer';
2019-03-08 14:20:17 -05:00
// @endif
2018-06-11 00:41:25 -06:00
type Props = {
2019-08-05 23:25:33 -04:00
uri: string,
2018-06-11 00:41:25 -06:00
mediaType: string,
2020-01-06 15:57:49 -05:00
isText: true,
2019-08-02 02:28:14 -04:00
streamingUrl: string,
2020-01-27 16:37:54 -05:00
embedded?: boolean,
2019-08-02 02:28:14 -04:00
contentType: string,
2019-04-24 10:02:08 -04:00
claim: StreamClaim,
2018-06-11 00:41:25 -06:00
currentTheme: string,
2019-08-05 23:25:33 -04:00
downloadPath: string,
fileName: string,
2020-01-27 14:32:20 -05:00
autoplay: boolean,
setPlayingUri: (string | null) => void,
currentlyFloating: boolean,
2020-01-31 10:46:50 -05:00
thumbnail: string,
2018-06-11 00:41:25 -06:00
};
2020-01-27 13:52:25 -05:00
type State = {
showAutoplayCountdown: boolean,
showEmbededMessage: boolean,
2020-01-27 13:52:25 -05:00
};
class FileRender extends React.PureComponent<Props, State> {
2019-01-19 13:54:06 -05:00
constructor(props: Props) {
super(props);
2020-01-27 13:52:25 -05:00
this.state = {
showAutoplayCountdown: false,
showEmbededMessage: false,
2020-01-27 13:52:25 -05:00
};
2019-01-19 13:54:06 -05:00
(this: any).escapeListener = this.escapeListener.bind(this);
2020-01-27 14:32:20 -05:00
(this: any).onEndedAutoplay = this.onEndedAutoplay.bind(this);
(this: any).onEndedEmbedded = this.onEndedEmbedded.bind(this);
2020-01-27 14:32:20 -05:00
(this: any).getOnEndedCb = this.getOnEndedCb.bind(this);
}
componentDidMount() {
window.addEventListener('keydown', this.escapeListener, true);
}
componentWillUnmount() {
window.removeEventListener('keydown', this.escapeListener, true);
}
2019-01-19 13:54:06 -05:00
escapeListener(e: SyntheticKeyboardEvent<*>) {
if (e.keyCode === 27) {
e.preventDefault();
this.exitFullscreen();
return false;
}
}
exitFullscreen() {
remote.getCurrentWindow().setFullScreen(false);
}
2020-01-27 14:32:20 -05:00
getOnEndedCb() {
const { setPlayingUri, currentlyFloating, embedded } = this.props;
if (embedded) {
return this.onEndedEmbedded;
}
2020-01-27 14:32:20 -05:00
if (!currentlyFloating) {
return this.onEndedAutoplay;
}
return () => setPlayingUri(null);
}
onEndedAutoplay() {
const { autoplay } = this.props;
if (autoplay) {
this.setState({ showAutoplayCountdown: true });
}
2019-09-05 20:26:03 -04:00
}
onEndedEmbedded() {
this.setState({ showEmbededMessage: true });
}
renderViewer() {
2020-01-27 16:37:54 -05:00
const { mediaType, currentTheme, claim, contentType, downloadPath, fileName, streamingUrl, uri } = this.props;
2019-08-02 02:28:14 -04:00
const fileType = fileName && path.extname(fileName).substring(1);
2018-06-11 00:41:25 -06:00
2019-08-13 01:35:13 -04:00
// Ideally the lbrytv api server would just replace the streaming_url returned by the sdk so we don't need this check
// https://github.com/lbryio/lbrytv/issues/51
2020-01-27 16:37:54 -05:00
const source = IS_WEB ? generateStreamUrl(claim.name, claim.claim_id) : streamingUrl;
2019-08-13 01:35:13 -04:00
// Human-readable files (scripts and plain-text files)
const readableFiles = ['text', 'document', 'script'];
2018-06-11 00:41:25 -06:00
// Supported mediaTypes
const mediaTypes = {
2019-03-08 14:20:17 -05:00
// @if TARGET='app'
'3D-file': <ThreeViewer source={{ fileType, downloadPath }} theme={currentTheme} />,
2019-05-16 00:32:53 -06:00
'comic-book': <ComicBookViewer source={{ fileType, downloadPath }} theme={currentTheme} />,
2019-08-05 23:25:33 -04:00
application: <AppViewer uri={uri} />,
2019-03-08 14:20:17 -05:00
// @endif
2020-01-27 14:32:20 -05:00
video: <VideoViewer uri={uri} source={source} contentType={contentType} onEndedCB={this.getOnEndedCb()} />,
audio: <VideoViewer uri={uri} source={source} contentType={contentType} onEndedCB={this.getOnEndedCb()} />,
2019-08-13 01:35:13 -04:00
image: <ImageViewer uri={uri} source={source} />,
2018-06-11 00:41:25 -06:00
// Add routes to viewer...
};
// Supported contentTypes
const contentTypes = {
'application/x-ext-mkv': (
2020-01-27 14:32:20 -05:00
<VideoViewer uri={uri} source={source} contentType={contentType} onEndedCB={this.getOnEndedCb()} />
),
'video/x-matroska': (
2020-01-27 14:32:20 -05:00
<VideoViewer uri={uri} source={source} contentType={contentType} onEndedCB={this.getOnEndedCb()} />
),
'application/pdf': <PdfViewer source={downloadPath || source} />,
'text/html': <HtmlViewer source={downloadPath || source} />,
'text/htm': <HtmlViewer source={downloadPath || source} />,
};
// Supported fileType
const fileTypes = {
2019-08-13 01:35:13 -04:00
// @if TARGET='app'
docx: <DocxViewer source={downloadPath} />,
2019-08-13 01:35:13 -04:00
// @endif
// Add routes to viewer...
};
// Check for a valid fileType, mediaType, or contentType
let viewer = (fileType && fileTypes[fileType]) || mediaTypes[mediaType] || contentTypes[contentType];
// Check for Human-readable files
if (!viewer && readableFiles.includes(mediaType)) {
2019-08-02 02:28:14 -04:00
viewer = (
<DocumentViewer
source={{
// @if TARGET='app'
file: options => fs.createReadStream(downloadPath, options),
// @endif
stream: source,
2019-08-02 02:28:14 -04:00
fileType,
contentType,
}}
theme={currentTheme}
/>
);
}
// @if TARGET='web'
// temp workaround to disabled paid content on web
2019-06-24 23:27:18 -04:00
if (claim && claim.value.fee && Number(claim.value.fee.amount) > 0) {
2019-03-19 02:32:53 -04:00
const paidMessage = __(
'Currently, only free content is available on lbry.tv. Try viewing it in the desktop app.'
);
const paid = <LoadingScreen status={paidMessage} spinner={false} />;
return paid;
}
// @endif
2019-09-02 09:24:00 -04:00
const unsupported = IS_WEB ? (
<div className={'content__cover--disabled'}>
<Yrbl
className={'content__cover--disabled'}
2019-09-03 10:02:04 -04:00
title={'Not available on lbry.tv'}
subtitle={
<Fragment>
<p>
{__('Good news, though! You can')}{' '}
<Button button="link" label={__('Download the desktop app')} href="https://lbry.com/get" />{' '}
{'and have access to all file types.'}
</p>
</Fragment>
}
2019-09-02 09:24:00 -04:00
uri={uri}
/>
</div>
) : (
<div className={'content__cover--disabled'}>
<Yrbl
title={'Content Downloaded'}
subtitle={'This file is unsupported here, but you can view the content in an application of your choice'}
uri={uri}
/>
</div>
);
2018-06-11 00:41:25 -06:00
// Return viewer
return viewer || unsupported;
2018-06-11 00:41:25 -06:00
}
render() {
2020-01-27 16:37:54 -05:00
const { isText, uri, currentlyFloating, embedded } = this.props;
const { showAutoplayCountdown, showEmbededMessage } = this.state;
2020-01-06 13:32:35 -05:00
2019-03-26 23:40:02 -05:00
return (
2020-01-27 16:37:54 -05:00
<div
className={classnames({
'file-render': !embedded,
'file-render--document': isText && !embedded,
'file-render__embed': embedded,
})}
>
{embedded && showEmbededMessage && (
<div className="video-overlay__wrapper">
<div className="video-overlay__title">{__('View More on lbry.tv')}</div>
<div className="video-overlay__actions">
<div className="section__actions--centered">
2020-01-28 11:00:39 -05:00
<Button label={__('Explore')} button="primary" href="https://lbry.tv?src=embed" />
</div>
</div>
</div>
)}
2020-01-27 14:32:20 -05:00
{!currentlyFloating && showAutoplayCountdown && <AutoplayCountdown uri={uri} />}
<Suspense fallback={<div />}>{this.renderViewer()}</Suspense>
2019-03-26 23:40:02 -05:00
</div>
);
2018-06-11 00:41:25 -06:00
}
}
2019-09-05 20:26:03 -04:00
export default withRouter(FileRender);