add method to format path for web to util

This commit is contained in:
zxawry 2019-07-04 12:15:40 +01:00
parent d23d06646e
commit e43ad2ce89
No known key found for this signature in database
GPG key ID: 70F5D1B4F51F051A
3 changed files with 68 additions and 40 deletions

View file

@ -1,6 +1,7 @@
// @flow // @flow
import React, { useRef } from 'react'; import React, { useRef } from 'react';
import { Modal } from 'modal/modal'; import { Modal } from 'modal/modal';
import { formatPathForWeb } from 'util/uri';
type Props = { type Props = {
upload: Buffer => void, upload: Buffer => void,
@ -12,10 +13,7 @@ type Props = {
function ModalAutoGenerateThumbnail(props: Props) { function ModalAutoGenerateThumbnail(props: Props) {
const { closeModal, filePath, upload, showToast } = props; const { closeModal, filePath, upload, showToast } = props;
const playerRef = useRef(); const playerRef = useRef();
const videoSrc = formatPathForWeb(filePath);
let src = filePath.replace(/\\/g, '/');
src = src[0] !== '/' ? `/${src}` : src;
src = encodeURI(`file://${src}`).replace(/[?#]/g, encodeURIComponent);
function uploadImage() { function uploadImage() {
const imageBuffer = captureSnapshot(); const imageBuffer = captureSnapshot();
@ -73,7 +71,7 @@ function ModalAutoGenerateThumbnail(props: Props) {
> >
<section className="card__content"> <section className="card__content">
<p className="card__subtitle">{__('Pause at any time to select a thumbnail from your video')}.</p> <p className="card__subtitle">{__('Pause at any time to select a thumbnail from your video')}.</p>
<video ref={playerRef} src={src} onLoadedMetadata={resize} onError={onError} controls /> <video ref={playerRef} src={videoSrc} onLoadedMetadata={resize} onError={onError} controls />
</section> </section>
</Modal> </Modal>
); );

View file

@ -1,20 +1,27 @@
// @flow // @flow
import React from 'react'; import React from 'react';
import { Modal } from 'modal/modal'; import { Modal } from 'modal/modal';
import { formatLbryUriForWeb } from 'util/uri'; import { formatPathForWeb } from 'util/uri';
// @if TARGET='app' // @if TARGET='app'
import { shell } from 'electron'; import { shell } from 'electron';
// @endif // @endif
type Props = { type Props = {
uri: string, uri: string,
isTrusted: boolean,
path: string, path: string,
isMine: boolean,
closeModal: () => void, closeModal: () => void,
}; };
class ModalOpenExternalResource extends React.PureComponent<Props> { function ModalOpenExternalResource(props: Props) {
openExternalResource() { const { uri, isTrusted, path, isMine, closeModal } = props;
const { uri, path, closeModal } = this.props;
if ((uri && isTrusted) || (path && isMine)) {
openResource();
}
function openResource() {
// @if TARGET='app' // @if TARGET='app'
const { openExternal, openItem, showItemInFolder } = shell; const { openExternal, openItem, showItemInFolder } = shell;
if (uri) { if (uri) {
@ -30,43 +37,33 @@ class ModalOpenExternalResource extends React.PureComponent<Props> {
if (uri) { if (uri) {
window.open(uri); window.open(uri);
} else if (path) { } else if (path) {
// Converintg path into uri, like "file://path/to/file" window.open(formatPathForWeb(path));
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);
} }
// @endif // @endif
closeModal(); closeModal();
} }
render() { return (
const { uri, path, closeModal } = this.props; <Modal
return ( isOpen
<Modal title={__('Warning!')}
isOpen contentLabel={__('Confirm External Resource')}
title={__('Warning!')} type="confirm"
contentLabel={__('Confirm External Resource')} confirmButtonLabel={__('Continue')}
type="confirm" onConfirmed={() => openResource()}
confirmButtonLabel={__('Continue')} onAborted={closeModal}
onConfirmed={() => this.openExternalResource()} >
onAborted={closeModal} <section className="card__content">
> <p>
<section className="card__content"> {(uri && __('This link leads to an external website.')) ||
<p> (path && __('This file has been shared with you by other people.'))}
{(uri && __('This link leads to an external website.')) || </p>
(path && __('This file has been shared with you by other people.'))} <blockquote>{uri || path}</blockquote>
</p> <p>{__('LBRY Inc is not responsible for its content, click continue to proceed at your own risk.')}</p>
<blockquote>{uri || path}</blockquote> </section>
<p>{__('LBRY Inc is not responsible for its content, click continue to proceed at your own risk.')}</p> </Modal>
</section> );
</Modal>
);
}
} }
export default ModalOpenExternalResource; export default ModalOpenExternalResource;

View file

@ -1,6 +1,8 @@
// @flow // @flow
import { parseURI } from 'lbry-redux'; 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) => { export const formatLbryUriForWeb = (uri: string) => {
const { claimName, claimId } = parseURI(uri); const { claimName, claimId } = parseURI(uri);
@ -11,3 +13,34 @@ export const formatLbryUriForWeb = (uri: string) => {
return webUrl; 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;
};