updated the constants used for determining request type

This commit is contained in:
bill bittner 2018-06-25 15:01:50 -07:00
parent 87acd65d2d
commit 1b1ebb1b7c
3 changed files with 33 additions and 19 deletions

View file

@ -0,0 +1,9 @@
const EMBED = 'EMBED';
const BROWSER = 'BROWSER';
const SOCIAL = 'SOCIAL';
module.exports = {
EMBED,
BROWSER,
SOCIAL,
};

View file

@ -3,12 +3,12 @@ const handleShowRender = require('../../../render/build/handleShowRender.js');
const lbryUri = require('../utils/lbryUri.js');
const determineResponseType = require('../utils/determineResponseType.js');
const determineRequestType = require('../utils/determineRequestType.js');
const getClaimIdAndServeAsset = require('../utils/getClaimIdAndServeAsset.js');
const flipClaimNameAndId = require('../utils/flipClaimNameAndId.js');
const logRequestData = require('../utils/logRequestData.js');
const SERVE = 'SERVE';
const { BROWSER, SOCIAL } = require('../constants/request_types.js');
/*
@ -25,10 +25,13 @@ const serverAssetByIdentifierAndClaim = (req, res) => {
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
let responseType = determineResponseType(hasFileExtension, headers);
if (responseType !== SERVE) {
let requestType = determineRequestType(hasFileExtension, headers);
if (requestType == BROWSER) {
return handleShowRender(req, res);
}
if (requestType == SOCIAL) {
return handleSocialRequest(req, res);
}
// handle serve request
// send google analytics
sendGAServeEvent(headers, ip, originalUrl);
@ -51,7 +54,7 @@ const serverAssetByIdentifierAndClaim = (req, res) => {
[claimId, claimName] = flipClaimNameAndId(claimId, claimName);
}
// log the request data for debugging
logRequestData(responseType, claimName, channelName, claimId);
logRequestData(requestType, claimName, channelName, claimId);
// get the claim Id and then serve the asset
getClaimIdAndServeAsset(channelName, channelClaimId, claimName, claimId, originalUrl, ip, res);
};

View file

@ -1,7 +1,5 @@
const logger = require('winston');
const SERVE = 'SERVE';
const SHOW = 'SHOW';
const { EMBED, BROWSER, SOCIAL } = require('../constants/request_types.js');
function headersMatchesSocialBotList (headers) {
const socialBotList = [
@ -33,27 +31,31 @@ function clientWantsAsset ({accept, range}) {
return imageIsWanted || videoIsWanted;
};
const determineResponseType = (hasFileExtension, headers) => {
const determineRequestType = (hasFileExtension, headers) => {
let responseType;
logger.debug('headers:', headers);
// return early with 'show' if headers match the list
if (headersMatchesSocialBotList(headers)) {
// return SHOW;
return SOCIAL;
}
// fallback logic if not on the list
// if request is not from a social bot...
if (hasFileExtension) {
responseType = SERVE; // assume a serve request if file extension is present
if (clientAcceptsHtml(headers)) { // if the request comes from a browser, change it to a show request
responseType = SHOW;
// assume embed,
responseType = EMBED;
// but change to browser if client accepts html.
if (clientAcceptsHtml(headers)) {
responseType = BROWSER;
}
// if request does not have file extentsion...
} else {
responseType = SHOW;
if (clientWantsAsset(headers) && requestIsFromBrowser(headers)) { // this is in case someone embeds a show url
logger.debug('Show request came from browser but wants an image/video. Changing response to serve...');
responseType = SERVE;
// assume browser,
responseType = BROWSER;
// but change to embed if someone embeded a show url...
if (clientWantsAsset(headers) && requestIsFromBrowser(headers)) {
responseType = EMBED;
}
}
return responseType;
};
module.exports = determineResponseType;
module.exports = determineRequestType;