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
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) {
>
<section className="card__content">
<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>
</Modal>
);

View file

@ -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<Props> {
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<Props> {
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 (
<Modal
isOpen
title={__('Warning!')}
contentLabel={__('Confirm External Resource')}
type="confirm"
confirmButtonLabel={__('Continue')}
onConfirmed={() => this.openExternalResource()}
onAborted={closeModal}
>
<section className="card__content">
<p>
{(uri && __('This link leads to an external website.')) ||
(path && __('This file has been shared with you by other people.'))}
</p>
<blockquote>{uri || path}</blockquote>
<p>{__('LBRY Inc is not responsible for its content, click continue to proceed at your own risk.')}</p>
</section>
</Modal>
);
}
return (
<Modal
isOpen
title={__('Warning!')}
contentLabel={__('Confirm External Resource')}
type="confirm"
confirmButtonLabel={__('Continue')}
onConfirmed={() => openResource()}
onAborted={closeModal}
>
<section className="card__content">
<p>
{(uri && __('This link leads to an external website.')) ||
(path && __('This file has been shared with you by other people.'))}
</p>
<blockquote>{uri || path}</blockquote>
<p>{__('LBRY Inc is not responsible for its content, click continue to proceed at your own risk.')}</p>
</section>
</Modal>
);
}
export default ModalOpenExternalResource;

View file

@ -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;
};