Fix embed url param parsing

This commit is contained in:
Rafael 2022-01-31 12:24:38 -03:00 committed by Thomas Zarebczan
parent 92f0fd8745
commit b30a8568e5
3 changed files with 27 additions and 28 deletions

View file

@ -3,7 +3,7 @@ import * as ICONS from 'constants/icons';
import { FormField } from 'component/common/form'; import { FormField } from 'component/common/form';
import Button from 'component/button'; import Button from 'component/button';
import React, { useRef } from 'react'; import React, { useRef } from 'react';
import { generateEmbedUrl, generateEmbedIframeData } from 'util/web'; import { generateEmbedUrlEncoded, generateEmbedIframeData } from 'util/web';
type Props = { type Props = {
copyable: string, copyable: string,
@ -21,7 +21,7 @@ export default function EmbedTextArea(props: Props) {
const { claim_id: claimId, name } = claim; const { claim_id: claimId, name } = claim;
const input = useRef(); const input = useRef();
const streamUrl = generateEmbedUrl(name, claimId, includeStartTime, startTime, referralCode); const streamUrl = generateEmbedUrlEncoded(name, claimId, includeStartTime && startTime, referralCode);
const { html: embedText } = generateEmbedIframeData(streamUrl); const { html: embedText } = generateEmbedIframeData(streamUrl);
function copyToClipboard() { function copyToClipboard() {

View file

@ -9,9 +9,10 @@ function generateStreamUrl(claimName, claimId) {
.replace(/\)/g, '%29')}/${claimId}/stream`; .replace(/\)/g, '%29')}/${claimId}/stream`;
} }
function generateEmbedUrl(claimName, claimId, includeStartTime, startTime, referralLink) { function generateEmbedUrl(claimName, claimId, startTime, referralLink) {
let urlParams = new URLSearchParams(); let urlParams = new URLSearchParams();
if (includeStartTime && startTime) {
if (startTime) {
urlParams.append('t', startTime); urlParams.append('t', startTime);
} }
@ -19,26 +20,16 @@ function generateEmbedUrl(claimName, claimId, includeStartTime, startTime, refer
urlParams.append('r', referralLink); urlParams.append('r', referralLink);
} }
return `${URL}/$/embed/${encodeURIComponent(claimName) const encodedUriName = encodeURIComponent(claimName).replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29');
.replace(/'/g, '%27')
.replace(/\(/g, '%28') const embedUrl = `${URL}/$/embed/${encodedUriName}/${claimId}`;
.replace(/\)/g, '%29')}/${claimId}?${urlParams.toString()}`; const embedUrlParams = urlParams.toString() ? `?${urlParams.toString()}` : '';
return `${embedUrl}${embedUrlParams}`;
} }
function generateEmbedUrlEncoded(claimName, claimId, includeStartTime, startTime, referralLink) { function generateEmbedUrlEncoded(claimName, claimId, startTime, referralLink) {
let urlParams = new URLSearchParams(); return generateEmbedUrl(claimName, claimId, startTime, referralLink).replace(/\$/g, '%24');
if (includeStartTime && startTime) {
urlParams.append('t', startTime);
}
if (referralLink) {
urlParams.append('r', referralLink);
}
return `${URL}/%24/embed/${encodeURIComponent(claimName)
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')}/${claimId}?${urlParams.toString()}`;
} }
function generateEmbedIframeData(src) { function generateEmbedIframeData(src) {

View file

@ -45,7 +45,7 @@ async function getClaim(requestUrl) {
// Generate // Generate
// **************************************************************************** // ****************************************************************************
function generateOEmbedData(claim, referrerQuery) { function generateOEmbedData(claim, embedlyReferrer, timestamp, referral) {
const { value, signing_channel: authorClaim } = claim; const { value, signing_channel: authorClaim } = claim;
const claimTitle = value.title; const claimTitle = value.title;
@ -53,9 +53,10 @@ function generateOEmbedData(claim, referrerQuery) {
const authorUrlPath = authorClaim && authorClaim.canonical_url.replace('lbry://', '').replace('#', ':'); const authorUrlPath = authorClaim && authorClaim.canonical_url.replace('lbry://', '').replace('#', ':');
const authorUrl = authorClaim ? `${URL}/${authorUrlPath}` : null; const authorUrl = authorClaim ? `${URL}/${authorUrlPath}` : null;
const thumbnailUrl = value && value.thumbnail && value.thumbnail.url && getThumbnailCdnUrl(value.thumbnail.url); const thumbnailUrl = value && value.thumbnail && value.thumbnail.url && getThumbnailCdnUrl(value.thumbnail.url);
const embedUrl = generateEmbedUrlEncoded(claim.name, claim.claim_id, timestamp, referral);
const videoUrl = const videoUrl =
generateEmbedUrlEncoded(claim.name, claim.claim_id) + embedUrl + (embedlyReferrer ? `referrer=${encodeURIComponent(escapeHtmlProperty(embedlyReferrer))}` : '');
(referrerQuery ? `r=${encodeURIComponent(escapeHtmlProperty(referrerQuery))}` : '');
const { html, width, height } = generateEmbedIframeData(videoUrl); const { html, width, height } = generateEmbedIframeData(videoUrl);
@ -116,12 +117,19 @@ function generateXmlData(oEmbedData) {
async function getOEmbed(ctx) { async function getOEmbed(ctx) {
const requestUrl = ctx.request.url; const requestUrl = ctx.request.url;
const urlQuery = getParameterByName('url', requestUrl); const urlQuery = getParameterByName('url', requestUrl);
const embedlyReferrer = getParameterByName('referrer', requestUrl);
const decodedQueryUri = decodeURIComponent(urlQuery);
const hasUrlParams = RegExp(/[?&]\w=/).test(decodedQueryUri);
const claimUrl = hasUrlParams ? decodedQueryUri.substring(0, decodedQueryUri.search(/[?&]\w=/)) : decodedQueryUri;
const { claim, error } = await getClaim(claimUrl);
const { claim, error } = await getClaim(urlQuery);
if (error) return error; if (error) return error;
const referrerQuery = getParameterByName('referrer', requestUrl); const queryTimestampParam = getParameterByName('t', decodedQueryUri);
const oEmbedData = generateOEmbedData(claim, referrerQuery); const queryReferralParam = getParameterByName('r', decodedQueryUri);
const oEmbedData = generateOEmbedData(claim, embedlyReferrer, queryTimestampParam, queryReferralParam);
const formatQuery = getParameterByName('format', requestUrl); const formatQuery = getParameterByName('format', requestUrl);
if (formatQuery === 'xml') { if (formatQuery === 'xml') {