From b9af50dedef90d8fc37068804ae6d9976b957c67 Mon Sep 17 00:00:00 2001 From: infinite-persistence <64950861+infinite-persistence@users.noreply.github.com> Date: Wed, 15 Jun 2022 18:54:53 +0800 Subject: [PATCH] GoogleVideo: generate instead of fetch the content url (#1691) Seeing performance issues. --- .env.defaults | 1 + config.js | 1 + web/src/fetchStreamUrl.js | 26 ++++++++++++++++++++++++++ web/src/metadata/googleVideo.js | 8 ++------ 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/.env.defaults b/.env.defaults index 143edd0d1..9032f0c94 100644 --- a/.env.defaults +++ b/.env.defaults @@ -29,6 +29,7 @@ THUMBNAIL_WIDTH=390 THUMBNAIL_WIDTH_MOBILE=900 THUMBNAIL_QUALITY=85 THUMBNAIL_CDN_SIZE_LIMIT_BYTES=5242880 +PLAYER_SERVER=https://player.odycdn.com WELCOME_VERSION=1.0 # --- Stripe --- diff --git a/config.js b/config.js index c4715d6a3..c92444057 100644 --- a/config.js +++ b/config.js @@ -31,6 +31,7 @@ const config = { THUMBNAIL_WIDTH_MOBILE: process.env.THUMBNAIL_WIDTH_MOBILE, THUMBNAIL_QUALITY: process.env.THUMBNAIL_QUALITY, THUMBNAIL_CDN_SIZE_LIMIT_BYTES: process.env.THUMBNAIL_CDN_SIZE_LIMIT_BYTES, + PLAYER_SERVER: process.env.PLAYER_SERVER, SITE_TITLE: process.env.SITE_TITLE, SITE_NAME: process.env.SITE_NAME, SITE_DESCRIPTION: process.env.SITE_DESCRIPTION, diff --git a/web/src/fetchStreamUrl.js b/web/src/fetchStreamUrl.js index 7b3869771..ff4ef2c25 100644 --- a/web/src/fetchStreamUrl.js +++ b/web/src/fetchStreamUrl.js @@ -1,3 +1,5 @@ +const Mime = require('mime-types'); +const { PLAYER_SERVER } = require('../../config.js'); const { lbryProxy: Lbry } = require('../lbry'); const { buildURI } = require('./lbryURI'); @@ -10,6 +12,30 @@ async function fetchStreamUrl(claimName, claimId) { }); } +/** + * Direct URL to the content's bits without redirects. + * + * Move back to 'utils/web' when `fetchStreamUrl` is no longer needed. + * + * @param claim + */ +function generateContentUrl(claim) { + const streamUrl = (claim) => { + // Hardcoded version of fetchStreamUrl(). + return `${PLAYER_SERVER}/api/v3/streams/free/${claim.name}/${claim.claim_id}`; + }; + + const value = claim?.value; + if (value?.source?.media_type && value?.source?.sd_hash) { + const fileExt = `.${Mime.extension(value.source.media_type)}`; + const sdHash = value.source.sd_hash.slice(0, 6); + return `${streamUrl(claim)}/${sdHash}${fileExt}`; + } + + return streamUrl(claim); +} + module.exports = { fetchStreamUrl, + generateContentUrl, }; diff --git a/web/src/metadata/googleVideo.js b/web/src/metadata/googleVideo.js index f563a523b..17c50e0ef 100644 --- a/web/src/metadata/googleVideo.js +++ b/web/src/metadata/googleVideo.js @@ -1,9 +1,8 @@ -const Mime = require('mime-types'); const moment = require('moment'); const removeMd = require('remove-markdown'); // TODO: fix relative path for server -const { fetchStreamUrl } = require('../fetchStreamUrl'); +const { generateContentUrl } = require('../fetchStreamUrl'); const { parseURI } = require('../lbryURI'); const { OG_IMAGE_URL, SITE_NAME, URL } = require('../../../config.js'); const { generateEmbedUrl, getThumbnailCdnUrl, escapeHtmlProperty } = require('../../../ui/util/web'); @@ -98,10 +97,7 @@ async function buildGoogleVideoMetadata(uri, claim) { } const claimThumbnail = escapeHtmlProperty(thumbnail) || getThumbnailCdnUrl(OG_IMAGE_URL) || `${URL}/public/v2-og.png`; - - const fileExt = value.source && value.source.media_type && '.' + Mime.extension(value.source.media_type); - const claimStreamUrl = - (await fetchStreamUrl(claim.name, claim.claim_id)).replace('/v4/', '/v3/') + (fileExt || '.mp4'); // v3 = mp4 always, v4 may redirect to m3u8; + const claimStreamUrl = generateContentUrl(claim); // https://developers.google.com/search/docs/data-types/video const googleVideoMetadata = {