From e43ad2ce89c761e9d4e38077c36179f50af5955d Mon Sep 17 00:00:00 2001 From: zxawry Date: Thu, 4 Jul 2019 12:15:40 +0100 Subject: [PATCH] add method to format path for web to util --- .../modal/modalAutoGenerateThumbnail/view.jsx | 8 +-- .../modal/modalOpenExternalResource/view.jsx | 67 +++++++++---------- src/ui/util/uri.js | 33 +++++++++ 3 files changed, 68 insertions(+), 40 deletions(-) diff --git a/src/ui/modal/modalAutoGenerateThumbnail/view.jsx b/src/ui/modal/modalAutoGenerateThumbnail/view.jsx index b8e847177..b6279999f 100644 --- a/src/ui/modal/modalAutoGenerateThumbnail/view.jsx +++ b/src/ui/modal/modalAutoGenerateThumbnail/view.jsx @@ -1,6 +1,7 @@ // @flow import React, { useRef } from 'react'; import { Modal } from 'modal/modal'; +import { formatPathForWeb } from 'util/uri'; type Props = { upload: Buffer => void, @@ -12,10 +13,7 @@ type Props = { function ModalAutoGenerateThumbnail(props: Props) { const { closeModal, filePath, upload, showToast } = props; const playerRef = useRef(); - - let src = filePath.replace(/\\/g, '/'); - src = src[0] !== '/' ? `/${src}` : src; - src = encodeURI(`file://${src}`).replace(/[?#]/g, encodeURIComponent); + const videoSrc = formatPathForWeb(filePath); function uploadImage() { const imageBuffer = captureSnapshot(); @@ -73,7 +71,7 @@ function ModalAutoGenerateThumbnail(props: Props) { >

{__('Pause at any time to select a thumbnail from your video')}.

-
); diff --git a/src/ui/modal/modalOpenExternalResource/view.jsx b/src/ui/modal/modalOpenExternalResource/view.jsx index d1ba71ac0..a2438611d 100644 --- a/src/ui/modal/modalOpenExternalResource/view.jsx +++ b/src/ui/modal/modalOpenExternalResource/view.jsx @@ -1,20 +1,27 @@ // @flow import React from 'react'; import { Modal } from 'modal/modal'; -import { formatLbryUriForWeb } from 'util/uri'; +import { formatPathForWeb } from 'util/uri'; // @if TARGET='app' import { shell } from 'electron'; // @endif type Props = { uri: string, + isTrusted: boolean, path: string, + isMine: boolean, closeModal: () => void, }; -class ModalOpenExternalResource extends React.PureComponent { - openExternalResource() { - const { uri, path, closeModal } = this.props; +function ModalOpenExternalResource(props: Props) { + const { uri, isTrusted, path, isMine, closeModal } = props; + + if ((uri && isTrusted) || (path && isMine)) { + openResource(); + } + + function openResource() { // @if TARGET='app' const { openExternal, openItem, showItemInFolder } = shell; if (uri) { @@ -30,43 +37,33 @@ class ModalOpenExternalResource extends React.PureComponent { if (uri) { window.open(uri); } else if (path) { - // Converintg path into uri, like "file://path/to/file" - let _uri = path.replace(/\\/g, '/'); - // Windows drive letter must be prefixed with a slash - if (_uri[0] !== '/') { - _uri = `/${_uri}`; - } - _uri = encodeURI(`file://${_uri}`).replace(/[?#]/g, encodeURIComponent); - window.open(_uri); + window.open(formatPathForWeb(path)); } // @endif closeModal(); } - render() { - const { uri, path, closeModal } = this.props; - return ( - this.openExternalResource()} - onAborted={closeModal} - > -
-

- {(uri && __('This link leads to an external website.')) || - (path && __('This file has been shared with you by other people.'))} -

-
{uri || path}
-

{__('LBRY Inc is not responsible for its content, click continue to proceed at your own risk.')}

-
-
- ); - } + return ( + openResource()} + onAborted={closeModal} + > +
+

+ {(uri && __('This link leads to an external website.')) || + (path && __('This file has been shared with you by other people.'))} +

+
{uri || path}
+

{__('LBRY Inc is not responsible for its content, click continue to proceed at your own risk.')}

+
+
+ ); } export default ModalOpenExternalResource; diff --git a/src/ui/util/uri.js b/src/ui/util/uri.js index 6d8818e55..70d20d765 100644 --- a/src/ui/util/uri.js +++ b/src/ui/util/uri.js @@ -1,6 +1,8 @@ // @flow import { parseURI } from 'lbry-redux'; +const LBRY_INC_DOMAINS = ['lbry.io', 'lbry.com', 'lbry.tv', 'lbry.tech', 'lbry.fund', 'spee.ch']; + export const formatLbryUriForWeb = (uri: string) => { const { claimName, claimId } = parseURI(uri); @@ -11,3 +13,34 @@ export const formatLbryUriForWeb = (uri: string) => { return webUrl; }; + +export const formatPathForWeb = (path: string) => { + if (!path) { + return; + } + + let webUrl = path.replace(/\\/g, '/'); + + if (webUrl[0] !== '/') { + webUrl = `/${webUrl}`; + } + + return encodeURI(`file://${webUrl}`).replace(/[?#]/g, encodeURIComponent); +}; + +export const isLBRYDomain = (uri: string) => { + if (!uri) { + return; + } + + const myURL = new URL(uri); + const hostname = myURL.hostname; + + for (let domain of LBRY_INC_DOMAINS) { + if (hostname.endsWith(domain)) { + return true; + } + } + + return false; +};