updated short url route

This commit is contained in:
bill bittner 2018-07-18 16:05:50 -07:00
parent 651f90cad9
commit 7dea5d5cd4
3 changed files with 29 additions and 47 deletions

View file

@ -1,3 +1,5 @@
const logger = require('winston');
const { sendGAServeEvent } = require('../../../utils/googleAnalytics'); const { sendGAServeEvent } = require('../../../utils/googleAnalytics');
const handleShowRender = require('../../../render/build/handleShowRender.js'); const handleShowRender = require('../../../render/build/handleShowRender.js');
@ -16,39 +18,36 @@ const { SHOW } = require('../constants/request_types.js');
const serveByClaim = (req, res) => { const serveByClaim = (req, res) => {
const { headers, ip, originalUrl, params } = req; const { headers, ip, originalUrl, params } = req;
// return early if channel request
let isChannel = false;
try { 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) { } catch (error) {
return res.status(400).json({success: false, message: error.message}); 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; module.exports = serveByClaim;

View file

@ -27,19 +27,7 @@ const serverByIdentifierAndClaim = (req, res) => {
return res.status(400).json({success: false, message: error.message}); return res.status(400).json({success: false, message: error.message});
} }
// determine request type // determine request type
let requestType = determineRequestType(hasFileExtension, headers); if (determineRequestType(hasFileExtension, headers) === SHOW) {
/* test logging */
logger.info('serveByIdentifierAndClaim', {
headers,
ip,
originalUrl,
params,
response: requestType,
});
/* end test logging */
if (requestType === SHOW) {
return handleShowRender(req, res); return handleShowRender(req, res);
} }
// parse the claim // parse the claim

View file

@ -1,4 +1,3 @@
const logger = require('winston');
const { EMBED, SHOW } = require('../constants/request_types.js'); const { EMBED, SHOW } = require('../constants/request_types.js');
function clientWantsAsset ({accept, range}) { function clientWantsAsset ({accept, range}) {
@ -8,10 +7,6 @@ function clientWantsAsset ({accept, range}) {
} }
const determineRequestType = (hasFileExtension, headers) => { const determineRequestType = (hasFileExtension, headers) => {
logger.info('determineRequestType', {
hasFileExtension,
headers,
});
if (hasFileExtension || clientWantsAsset(headers)) { if (hasFileExtension || clientWantsAsset(headers)) {
return EMBED; return EMBED;
} }