2018-06-11 08:41:25 +02:00
|
|
|
// @flow
|
2019-03-21 14:59:31 +01:00
|
|
|
import type { Claim } from 'types/claim';
|
2019-01-16 05:33:06 +01:00
|
|
|
import { remote } from 'electron';
|
2018-06-11 08:41:25 +02:00
|
|
|
import React from 'react';
|
|
|
|
import LoadingScreen from 'component/common/loading-screen';
|
2018-07-05 04:49:12 +02:00
|
|
|
import PdfViewer from 'component/viewers/pdfViewer';
|
2018-07-26 07:18:35 +02:00
|
|
|
import DocumentViewer from 'component/viewers/documentViewer';
|
2018-07-27 04:06:39 +02:00
|
|
|
import DocxViewer from 'component/viewers/docxViewer';
|
2018-08-02 06:56:17 +02:00
|
|
|
import HtmlViewer from 'component/viewers/htmlViewer';
|
2019-02-22 06:01:59 +01:00
|
|
|
import AudioVideoViewer from 'component/viewers/audioVideoViewer';
|
2019-03-08 20:20:17 +01:00
|
|
|
// @if TARGET='app'
|
2019-03-27 05:40:02 +01:00
|
|
|
const ThreeViewer = React.lazy(() => import(
|
|
|
|
/* webpackChunkName: "threeViewer" */
|
|
|
|
'component/viewers/threeViewer'
|
|
|
|
));
|
2019-03-08 20:20:17 +01:00
|
|
|
// @endif
|
2018-06-11 08:41:25 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
mediaType: string,
|
2019-02-22 06:01:59 +01:00
|
|
|
poster?: string,
|
2019-03-21 14:59:31 +01:00
|
|
|
claim: Claim,
|
2018-06-13 06:06:53 +02:00
|
|
|
source: {
|
2018-10-15 02:27:09 +02:00
|
|
|
stream: string => void,
|
2018-08-02 02:53:38 +02:00
|
|
|
fileName: string,
|
2018-06-11 08:41:25 +02:00
|
|
|
fileType: string,
|
2018-10-15 02:27:09 +02:00
|
|
|
contentType: string,
|
2018-07-05 04:49:12 +02:00
|
|
|
downloadPath: string,
|
2019-01-19 19:54:06 +01:00
|
|
|
url: ?string,
|
2018-06-11 08:41:25 +02:00
|
|
|
},
|
|
|
|
currentTheme: string,
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
// This should use React.createRef()
|
|
|
|
processSandboxRef(element: any) {
|
2019-01-16 05:33:06 +01:00
|
|
|
if (!element) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.sandbox = element;
|
|
|
|
|
|
|
|
element.addEventListener('permissionrequest', e => {
|
|
|
|
console.log('permissionrequest', e);
|
|
|
|
});
|
|
|
|
|
2019-01-19 19:54:06 +01:00
|
|
|
element.addEventListener('console-message', (e: { message: string }) => {
|
2019-01-16 05:33:06 +01:00
|
|
|
if (/^\$LBRY_IPC:/.test(e.message)) {
|
|
|
|
// Process command
|
|
|
|
let message = {};
|
|
|
|
try {
|
2019-02-22 06:01:59 +01:00
|
|
|
// $FlowFixMe
|
2019-01-16 05:33:06 +01:00
|
|
|
message = JSON.parse(/^\$LBRY_IPC:(.*)/.exec(e.message)[1]);
|
|
|
|
} catch (err) {}
|
|
|
|
console.log('IPC', message);
|
|
|
|
} else {
|
|
|
|
console.log('Sandbox:', e.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
element.addEventListener('enter-html-full-screen', () => {
|
|
|
|
// stub
|
|
|
|
});
|
|
|
|
|
|
|
|
element.addEventListener('leave-html-full-screen', () => {
|
|
|
|
// stub
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-03-13 06:59:07 +01:00
|
|
|
const { source, mediaType, currentTheme, poster, claim } = this.props;
|
2018-08-02 02:53:38 +02:00
|
|
|
|
|
|
|
// Extract relevant data to render file
|
2019-02-22 06:01:59 +01:00
|
|
|
const { stream, fileType, contentType, downloadPath, fileName } = source;
|
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-03-08 20:20:17 +01:00
|
|
|
// @endif
|
|
|
|
|
2019-01-19 19:54:06 +01:00
|
|
|
application: !source.url ? null : (
|
2019-01-16 05:33:06 +01:00
|
|
|
<webview
|
|
|
|
ref={element => this.processSandboxRef(element)}
|
2019-03-19 07:32:53 +01:00
|
|
|
title=""
|
|
|
|
sandbox="allow-scripts allow-forms allow-pointer-lock"
|
2019-01-16 05:33:06 +01:00
|
|
|
src={source.url}
|
2019-03-19 07:32:53 +01:00
|
|
|
autosize="on"
|
2019-01-16 05:33:06 +01:00
|
|
|
style={{ border: 0, width: '100%', height: '100%' }}
|
2019-03-19 07:32:53 +01:00
|
|
|
useragent="Mozilla/5.0 AppleWebKit/537 Chrome/60 Safari/537"
|
|
|
|
enableremotemodule="false"
|
|
|
|
webpreferences="sandbox=true,contextIsolation=true,webviewTag=false,enableRemoteModule=false,devTools=false"
|
2019-01-16 05:33:06 +01:00
|
|
|
/>
|
|
|
|
),
|
2019-02-22 06:01:59 +01:00
|
|
|
video: (
|
|
|
|
<AudioVideoViewer
|
2019-03-13 06:59:07 +01:00
|
|
|
claim={claim}
|
2019-02-22 06:01:59 +01:00
|
|
|
source={{ downloadPath, fileName }}
|
|
|
|
contentType={contentType}
|
|
|
|
poster={poster}
|
|
|
|
/>
|
|
|
|
),
|
2019-03-13 06:59:07 +01:00
|
|
|
audio: (
|
|
|
|
<AudioVideoViewer
|
|
|
|
claim={claim}
|
|
|
|
source={{ downloadPath, fileName }}
|
|
|
|
contentType={contentType}
|
|
|
|
/>
|
|
|
|
),
|
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
|
|
|
|
let viewer = fileTypes[fileType] || mediaTypes[mediaType];
|
|
|
|
|
|
|
|
// Check for Human-readable files
|
|
|
|
if (!viewer && readableFiles.includes(mediaType)) {
|
|
|
|
viewer = <DocumentViewer source={{ stream, fileType, contentType }} theme={currentTheme} />;
|
|
|
|
}
|
2019-03-21 14:59:31 +01:00
|
|
|
|
|
|
|
// @if TARGET='web'
|
|
|
|
// temp workaround to disabled paid content on web
|
2019-03-19 07:32:53 +01:00
|
|
|
if (claim && claim.value.stream.metadata.fee && claim.value.stream.metadata.fee.amount > 0) {
|
|
|
|
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
|
2018-07-09 06:57:31 +02:00
|
|
|
const unsupportedMessage = __("Sorry, looks like 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">
|
|
|
|
<React.Suspense fallback={<div></div>}>
|
|
|
|
{this.renderViewer()}
|
|
|
|
</React.Suspense>
|
|
|
|
</div>
|
|
|
|
);
|
2018-06-11 08:41:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FileRender;
|