2018-06-11 08:41:25 +02:00
|
|
|
// @flow
|
2019-01-16 05:33:06 +01:00
|
|
|
import { remote } from 'electron';
|
2019-06-27 08:18:45 +02:00
|
|
|
import React, { Suspense } from 'react';
|
2018-06-11 08:41:25 +02:00
|
|
|
import LoadingScreen from 'component/common/loading-screen';
|
2019-04-03 07:56:58 +02:00
|
|
|
import VideoViewer from 'component/viewers/videoViewer';
|
2019-08-06 05:25:33 +02:00
|
|
|
import ImageViewer from 'component/viewers/imageViewer';
|
|
|
|
import AppViewer from 'component/viewers/appViewer';
|
2019-08-02 08:28:14 +02:00
|
|
|
import path from 'path';
|
|
|
|
import fs from 'fs';
|
2019-04-03 07:56:58 +02:00
|
|
|
|
2019-08-02 08:28:14 +02:00
|
|
|
// This is half complete, the video viewer works fine for audio, it just doesn't look pretty
|
2019-06-28 09:27:55 +02:00
|
|
|
// const AudioViewer = React.lazy<*>(() =>
|
2019-06-27 08:18:45 +02:00
|
|
|
// import(
|
|
|
|
// /* webpackChunkName: "audioViewer" */
|
|
|
|
// 'component/viewers/audioViewer'
|
|
|
|
// )
|
|
|
|
// );
|
2019-04-03 07:56:58 +02:00
|
|
|
|
2019-04-24 16:02:08 +02:00
|
|
|
const DocumentViewer = React.lazy<*>(() =>
|
2019-06-25 05:27:18 +02:00
|
|
|
import(
|
|
|
|
/* webpackChunkName: "documentViewer" */
|
|
|
|
'component/viewers/documentViewer'
|
|
|
|
)
|
2019-04-24 16:02:08 +02:00
|
|
|
);
|
2019-04-03 07:56:58 +02:00
|
|
|
|
2019-04-24 16:02:08 +02:00
|
|
|
const DocxViewer = React.lazy<*>(() =>
|
2019-06-25 05:27:18 +02:00
|
|
|
import(
|
|
|
|
/* webpackChunkName: "docxViewer" */
|
|
|
|
'component/viewers/docxViewer'
|
|
|
|
)
|
2019-04-24 16:02:08 +02:00
|
|
|
);
|
2019-04-03 07:56:58 +02:00
|
|
|
|
2019-04-24 16:02:08 +02:00
|
|
|
const HtmlViewer = React.lazy<*>(() =>
|
2019-06-25 05:27:18 +02:00
|
|
|
import(
|
|
|
|
/* webpackChunkName: "htmlViewer" */
|
|
|
|
'component/viewers/htmlViewer'
|
|
|
|
)
|
2019-04-24 16:02:08 +02:00
|
|
|
);
|
2019-04-03 07:56:58 +02:00
|
|
|
|
2019-04-24 16:02:08 +02:00
|
|
|
const PdfViewer = React.lazy<*>(() =>
|
2019-06-25 05:27:18 +02:00
|
|
|
import(
|
|
|
|
/* webpackChunkName: "pdfViewer" */
|
|
|
|
'component/viewers/pdfViewer'
|
|
|
|
)
|
2019-04-24 16:02:08 +02:00
|
|
|
);
|
2019-04-03 07:56:58 +02:00
|
|
|
|
2019-03-08 20:20:17 +01:00
|
|
|
// @if TARGET='app'
|
2019-05-09 00:47:39 +02:00
|
|
|
const ComicBookViewer = React.lazy<*>(() =>
|
2019-06-25 05:27:18 +02:00
|
|
|
import(
|
|
|
|
/* webpackChunkName: "comicBookViewer" */
|
|
|
|
'component/viewers/comicBookViewer'
|
|
|
|
)
|
2019-05-09 00:47:39 +02:00
|
|
|
);
|
|
|
|
|
2019-04-24 16:02:08 +02:00
|
|
|
const ThreeViewer = React.lazy<*>(() =>
|
2019-06-25 05:27:18 +02:00
|
|
|
import(
|
|
|
|
/* webpackChunkName: "threeViewer" */
|
|
|
|
'component/viewers/threeViewer'
|
|
|
|
)
|
2019-04-24 16:02:08 +02:00
|
|
|
);
|
2019-03-08 20:20:17 +01:00
|
|
|
// @endif
|
2018-06-11 08:41:25 +02:00
|
|
|
|
|
|
|
type Props = {
|
2019-08-06 05:25:33 +02:00
|
|
|
uri: string,
|
2018-06-11 08:41:25 +02:00
|
|
|
mediaType: string,
|
2019-08-02 08:28:14 +02:00
|
|
|
streamingUrl: string,
|
|
|
|
contentType: string,
|
2019-04-24 16:02:08 +02:00
|
|
|
claim: StreamClaim,
|
2018-06-11 08:41:25 +02:00
|
|
|
currentTheme: string,
|
2019-08-06 05:25:33 +02:00
|
|
|
downloadPath: string,
|
|
|
|
fileName: string,
|
2018-06-11 08:41:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class FileRender extends React.PureComponent<Props> {
|
2019-01-19 19:54:06 +01:00
|
|
|
constructor(props: Props) {
|
2019-01-16 05:33:06 +01:00
|
|
|
super(props);
|
|
|
|
|
2019-01-19 19:54:06 +01:00
|
|
|
(this: any).escapeListener = this.escapeListener.bind(this);
|
2019-01-16 05:33:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
window.addEventListener('keydown', this.escapeListener, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('keydown', this.escapeListener, true);
|
|
|
|
}
|
|
|
|
|
2019-01-19 19:54:06 +01:00
|
|
|
escapeListener(e: SyntheticKeyboardEvent<*>) {
|
2019-01-16 05:33:06 +01:00
|
|
|
if (e.keyCode === 27) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
this.exitFullscreen();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exitFullscreen() {
|
|
|
|
remote.getCurrentWindow().setFullScreen(false);
|
|
|
|
}
|
|
|
|
|
2018-06-13 06:06:53 +02:00
|
|
|
renderViewer() {
|
2019-08-06 05:25:33 +02:00
|
|
|
const { mediaType, currentTheme, claim, contentType, downloadPath, fileName, streamingUrl, uri } = this.props;
|
2018-08-02 02:53:38 +02:00
|
|
|
|
2019-08-02 08:28:14 +02:00
|
|
|
const fileType = fileName && path.extname(fileName).substring(1);
|
2018-06-11 08:41:25 +02:00
|
|
|
|
2018-08-02 06:56:17 +02:00
|
|
|
// Human-readable files (scripts and plain-text files)
|
|
|
|
const readableFiles = ['text', 'document', 'script'];
|
|
|
|
|
2018-06-11 08:41:25 +02:00
|
|
|
// Supported mediaTypes
|
|
|
|
const mediaTypes = {
|
2019-03-08 20:20:17 +01:00
|
|
|
// @if TARGET='app'
|
2018-08-02 02:53:38 +02:00
|
|
|
'3D-file': <ThreeViewer source={{ fileType, downloadPath }} theme={currentTheme} />,
|
2019-05-16 08:32:53 +02:00
|
|
|
'comic-book': <ComicBookViewer source={{ fileType, downloadPath }} theme={currentTheme} />,
|
2019-08-06 05:25:33 +02:00
|
|
|
application: <AppViewer uri={uri} />,
|
2019-03-08 20:20:17 +01:00
|
|
|
// @endif
|
|
|
|
|
2019-08-02 08:28:14 +02:00
|
|
|
video: <VideoViewer source={streamingUrl} contentType={contentType} />,
|
|
|
|
audio: <VideoViewer source={streamingUrl} contentType={contentType} />,
|
2019-08-06 05:25:33 +02:00
|
|
|
image: <ImageViewer source={streamingUrl} />,
|
2018-06-11 08:41:25 +02:00
|
|
|
// Add routes to viewer...
|
|
|
|
};
|
|
|
|
|
2018-07-05 04:49:12 +02:00
|
|
|
// Supported fileType
|
|
|
|
const fileTypes = {
|
2018-08-02 02:53:38 +02:00
|
|
|
pdf: <PdfViewer source={downloadPath} />,
|
|
|
|
docx: <DocxViewer source={downloadPath} />,
|
2018-08-02 06:56:17 +02:00
|
|
|
html: <HtmlViewer source={downloadPath} />,
|
2018-07-05 04:49:12 +02:00
|
|
|
// Add routes to viewer...
|
|
|
|
};
|
|
|
|
|
2018-08-02 06:56:17 +02:00
|
|
|
// Check for a valid fileType or mediaType
|
2019-08-06 05:25:33 +02:00
|
|
|
let viewer = fileType ? fileTypes[fileType] : mediaTypes[mediaType];
|
2018-08-02 06:56:17 +02:00
|
|
|
|
|
|
|
// Check for Human-readable files
|
|
|
|
if (!viewer && readableFiles.includes(mediaType)) {
|
2019-08-02 08:28:14 +02:00
|
|
|
viewer = (
|
|
|
|
<DocumentViewer
|
|
|
|
source={{
|
|
|
|
stream: options => fs.createReadStream(downloadPath, options),
|
|
|
|
fileType,
|
|
|
|
contentType,
|
|
|
|
}}
|
|
|
|
theme={currentTheme}
|
|
|
|
/>
|
|
|
|
);
|
2018-08-02 06:56:17 +02:00
|
|
|
}
|
2019-03-21 14:59:31 +01:00
|
|
|
|
|
|
|
// @if TARGET='web'
|
|
|
|
// temp workaround to disabled paid content on web
|
2019-06-25 05:27:18 +02:00
|
|
|
if (claim && claim.value.fee && Number(claim.value.fee.amount) > 0) {
|
2019-03-19 07:32:53 +01: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;
|
|
|
|
}
|
2019-03-21 14:59:31 +01:00
|
|
|
// @endif
|
|
|
|
|
2018-08-02 06:56:17 +02:00
|
|
|
// Message Error
|
2019-08-02 08:28:14 +02:00
|
|
|
const unsupportedMessage = __("We can't preview this file.");
|
2018-06-13 06:06:53 +02:00
|
|
|
const unsupported = <LoadingScreen status={unsupportedMessage} spinner={false} />;
|
|
|
|
|
2018-06-11 08:41:25 +02:00
|
|
|
// Return viewer
|
2018-06-13 06:06:53 +02:00
|
|
|
return viewer || unsupported;
|
2018-06-11 08:41:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-03-27 05:40:02 +01:00
|
|
|
return (
|
|
|
|
<div className="file-render">
|
2019-06-28 09:27:55 +02:00
|
|
|
<Suspense fallback={<div />}>{this.renderViewer()}</Suspense>
|
2019-03-27 05:40:02 +01:00
|
|
|
</div>
|
|
|
|
);
|
2018-06-11 08:41:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FileRender;
|