2019-12-06 22:12:48 +01:00
|
|
|
// Can't use aliases here because we're doing exports/require
|
|
|
|
const PAGES = require('../constants/pages');
|
2021-01-13 22:39:46 +01:00
|
|
|
const { parseURI, buildURI } = require('lbry-redux');
|
2019-12-06 22:12:48 +01:00
|
|
|
|
2021-05-13 21:21:21 +02:00
|
|
|
function encodeWithSpecialCharEncode(string) {
|
|
|
|
// encodeURIComponent doesn't encode `'` and others
|
|
|
|
// which other services may not like
|
|
|
|
return encodeURIComponent(string).replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29');
|
2021-01-22 21:37:20 +01:00
|
|
|
}
|
|
|
|
|
2021-04-23 21:59:48 +02:00
|
|
|
export const formatLbryUrlForWeb = (uri) => {
|
2020-07-23 16:22:57 +02:00
|
|
|
let newUrl = uri.replace('lbry://', '/').replace(/#/g, ':');
|
|
|
|
if (newUrl.startsWith('/?')) {
|
|
|
|
// This is a lbry link to an internal page ex: lbry://?rewards
|
|
|
|
newUrl = newUrl.replace('/?', '/$/');
|
|
|
|
}
|
|
|
|
|
|
|
|
return newUrl;
|
2019-12-02 18:30:08 +01:00
|
|
|
};
|
|
|
|
|
2021-04-23 21:59:48 +02:00
|
|
|
export const formatFileSystemPath = (path) => {
|
2019-12-02 18:30:08 +01:00
|
|
|
if (!path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let webUrl = path.replace(/\\/g, '/');
|
|
|
|
|
|
|
|
if (webUrl[0] !== '/') {
|
|
|
|
webUrl = `/${webUrl}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return encodeURI(`file://${webUrl}`).replace(/[?#]/g, encodeURIComponent);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Function that handles page redirects
|
|
|
|
ex: lbry://?rewards
|
|
|
|
ex: open.lbry.com/?rewards
|
|
|
|
*/
|
2021-04-23 21:59:48 +02:00
|
|
|
export const formatInAppUrl = (path) => {
|
2019-12-02 18:30:08 +01:00
|
|
|
// Determine if we need to add a leading "/$/" for app pages
|
|
|
|
const APP_PAGE_REGEX = /(\?)([a-z]*)(.*)/;
|
|
|
|
const appPageMatches = APP_PAGE_REGEX.exec(path);
|
|
|
|
|
|
|
|
if (appPageMatches && appPageMatches.length) {
|
|
|
|
// Definitely an app page (or it's formatted like one)
|
|
|
|
const [, , page, queryString] = appPageMatches;
|
|
|
|
|
2019-12-09 20:42:10 +01:00
|
|
|
if (Object.values(PAGES).includes(page)) {
|
|
|
|
let actualUrl = '/$/' + page;
|
|
|
|
|
|
|
|
if (queryString) {
|
|
|
|
actualUrl += `?${queryString.slice(1)}`;
|
|
|
|
}
|
2019-12-03 18:55:29 +01:00
|
|
|
|
2019-12-09 20:42:10 +01:00
|
|
|
return actualUrl;
|
|
|
|
}
|
2019-12-02 18:30:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Regular claim url
|
|
|
|
return path;
|
|
|
|
};
|
2019-12-06 22:12:48 +01:00
|
|
|
|
2021-01-25 18:05:23 +01:00
|
|
|
export const formatWebUrlIntoLbryUrl = (pathname, search) => {
|
2019-12-06 22:12:48 +01:00
|
|
|
// If there is no uri, the user is on an internal page
|
|
|
|
// pathname will either be "/" or "/$/{page}"
|
|
|
|
const path = pathname.startsWith('/$/') ? pathname.slice(3) : pathname.slice(1);
|
|
|
|
let appLink = `lbry://?${path || PAGES.DISCOVER}`;
|
|
|
|
|
|
|
|
if (search) {
|
|
|
|
// We already have a leading "?" for the query param on internal pages
|
|
|
|
appLink += search.replace('?', '&');
|
|
|
|
}
|
|
|
|
|
|
|
|
return appLink;
|
|
|
|
};
|
2020-02-12 05:59:30 +01:00
|
|
|
|
2021-04-23 21:59:48 +02:00
|
|
|
export const generateInitialUrl = (hash) => {
|
2020-02-12 05:59:30 +01:00
|
|
|
let url = '/';
|
|
|
|
if (hash) {
|
|
|
|
hash = hash.replace('#', '');
|
|
|
|
url = hash.startsWith('/') ? hash : '/' + hash;
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
};
|
2020-05-06 05:05:59 +02:00
|
|
|
|
2021-01-25 18:05:23 +01:00
|
|
|
export const generateLbryContentUrl = (canonicalUrl, permanentUrl) => {
|
2020-05-06 05:05:59 +02:00
|
|
|
return canonicalUrl ? canonicalUrl.split('lbry://')[1] : permanentUrl.split('lbry://')[1];
|
|
|
|
};
|
|
|
|
|
2021-04-23 21:59:48 +02:00
|
|
|
export const generateLbryWebUrl = (lbryUrl) => {
|
2020-05-06 05:05:59 +02:00
|
|
|
return lbryUrl.replace(/#/g, ':');
|
|
|
|
};
|
|
|
|
|
2021-01-25 18:05:23 +01:00
|
|
|
export const generateEncodedLbryURL = (domain, lbryWebUrl, includeStartTime, startTime) => {
|
2020-05-06 05:05:59 +02:00
|
|
|
const queryParam = includeStartTime ? `?t=${startTime}` : '';
|
2021-05-13 21:21:21 +02:00
|
|
|
const encodedPart = encodeWithSpecialCharEncode(`${lbryWebUrl}${queryParam}`);
|
2020-08-29 19:58:38 +02:00
|
|
|
return `${domain}/${encodedPart}`;
|
2020-05-06 05:05:59 +02:00
|
|
|
};
|
|
|
|
|
2021-01-25 18:05:23 +01:00
|
|
|
export const generateShareUrl = (domain, lbryUrl, referralCode, rewardsApproved, includeStartTime, startTime) => {
|
2020-05-06 05:05:59 +02:00
|
|
|
let urlParams = new URLSearchParams();
|
|
|
|
if (referralCode && rewardsApproved) {
|
|
|
|
urlParams.append('r', referralCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (includeStartTime) {
|
|
|
|
urlParams.append('t', startTime.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
const urlParamsString = urlParams.toString();
|
2020-12-14 20:48:08 +01:00
|
|
|
|
2021-01-13 22:39:46 +01:00
|
|
|
const { streamName, streamClaimId, channelName, channelClaimId } = parseURI(lbryUrl);
|
|
|
|
|
|
|
|
let uriParts = {
|
2021-05-13 21:21:21 +02:00
|
|
|
...(streamName ? { streamName: encodeWithSpecialCharEncode(streamName) } : {}),
|
2021-01-13 22:39:46 +01:00
|
|
|
...(streamClaimId ? { streamClaimId } : {}),
|
2021-05-13 21:21:21 +02:00
|
|
|
...(channelName ? { channelName: encodeWithSpecialCharEncode(channelName) } : {}),
|
2021-01-13 22:39:46 +01:00
|
|
|
...(channelClaimId ? { channelClaimId } : {}),
|
|
|
|
};
|
|
|
|
|
|
|
|
const encodedUrl = buildURI(uriParts, false);
|
|
|
|
const lbryWebUrl = encodedUrl.replace(/#/g, ':');
|
|
|
|
|
2020-12-16 01:44:37 +01:00
|
|
|
const url = `${domain}/${lbryWebUrl}` + (urlParamsString === '' ? '' : `?${urlParamsString}`);
|
2020-05-06 05:05:59 +02:00
|
|
|
return url;
|
|
|
|
};
|