From 7dea5d5cd4b964fb48fa7a4d55609242c098c349 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Wed, 18 Jul 2018 16:05:50 -0700 Subject: [PATCH] updated short url route --- .../controllers/assets/serveByClaim/index.js | 57 +++++++++---------- .../assets/serveByIdentifierAndClaim/index.js | 14 +---- .../assets/utils/determineRequestType.js | 5 -- 3 files changed, 29 insertions(+), 47 deletions(-) diff --git a/server/controllers/assets/serveByClaim/index.js b/server/controllers/assets/serveByClaim/index.js index fdabdc96..bd3b2a21 100644 --- a/server/controllers/assets/serveByClaim/index.js +++ b/server/controllers/assets/serveByClaim/index.js @@ -1,3 +1,5 @@ +const logger = require('winston'); + const { sendGAServeEvent } = require('../../../utils/googleAnalytics'); const handleShowRender = require('../../../render/build/handleShowRender.js'); @@ -16,39 +18,36 @@ const { SHOW } = require('../constants/request_types.js'); const serveByClaim = (req, res) => { const { headers, ip, originalUrl, params } = req; - // return early if channel request - let isChannel = false; + try { - ({ isChannel } = lbryUri.parseIdentifier(params.claim)); + // return early if channel request + const { isChannel } = lbryUri.parseIdentifier(params.claim); + if (isChannel) { + logger.info('channel request:', { headers, ip, originalUrl, params }); + return handleShowRender(req, res); + } + + // decide if this is a show request + const { hasFileExtension } = lbryUri.parseModifier(params.claim); + if (determineRequestType(hasFileExtension, headers) === SHOW) { + logger.info('show request:', { headers, ip, originalUrl, params }); + return handleShowRender(req, res); + } + + // parse the claim + const { claimName } = lbryUri.parseClaim(params.claim); + + // send google analytics + sendGAServeEvent(headers, ip, originalUrl); + + // get the claim Id and then serve the asset + logger.info('embed request:', { headers, ip, originalUrl, params }); + getClaimIdAndServeAsset(null, null, claimName, null, originalUrl, ip, res); + } catch (error) { return res.status(400).json({success: false, message: error.message}); } - if (isChannel) { - return handleShowRender(req, res); - } - // decide if this is a show request - let hasFileExtension; - try { - ({ hasFileExtension } = lbryUri.parseModifier(params.claim)); - } catch (error) { - return res.status(400).json({success: false, message: error.message}); - } - // determine request type - let requestType = determineRequestType(hasFileExtension, headers); - if (requestType === SHOW) { - return handleShowRender(req, res); - } - // parse the claim - let claimName; - try { - ({claimName} = lbryUri.parseClaim(params.claim)); - } catch (error) { - return res.status(400).json({success: false, message: error.message}); - } - // send google analytics - sendGAServeEvent(headers, ip, originalUrl); - // get the claim Id and then serve the asset - getClaimIdAndServeAsset(null, null, claimName, null, originalUrl, ip, res); + }; module.exports = serveByClaim; diff --git a/server/controllers/assets/serveByIdentifierAndClaim/index.js b/server/controllers/assets/serveByIdentifierAndClaim/index.js index b89f5c56..029f76e0 100644 --- a/server/controllers/assets/serveByIdentifierAndClaim/index.js +++ b/server/controllers/assets/serveByIdentifierAndClaim/index.js @@ -27,19 +27,7 @@ const serverByIdentifierAndClaim = (req, res) => { return res.status(400).json({success: false, message: error.message}); } // determine request type - let requestType = determineRequestType(hasFileExtension, headers); - - /* test logging */ - logger.info('serveByIdentifierAndClaim', { - headers, - ip, - originalUrl, - params, - response: requestType, - }); - /* end test logging */ - - if (requestType === SHOW) { + if (determineRequestType(hasFileExtension, headers) === SHOW) { return handleShowRender(req, res); } // parse the claim diff --git a/server/controllers/assets/utils/determineRequestType.js b/server/controllers/assets/utils/determineRequestType.js index cd2121ac..c170c3dc 100644 --- a/server/controllers/assets/utils/determineRequestType.js +++ b/server/controllers/assets/utils/determineRequestType.js @@ -1,4 +1,3 @@ -const logger = require('winston'); const { EMBED, SHOW } = require('../constants/request_types.js'); function clientWantsAsset ({accept, range}) { @@ -8,10 +7,6 @@ function clientWantsAsset ({accept, range}) { } const determineRequestType = (hasFileExtension, headers) => { - logger.info('determineRequestType', { - hasFileExtension, - headers, - }); if (hasFileExtension || clientWantsAsset(headers)) { return EMBED; }