updated the constants used for determining request type
This commit is contained in:
parent
87acd65d2d
commit
1b1ebb1b7c
3 changed files with 33 additions and 19 deletions
9
server/controllers/assets/constants/request_types.js
Normal file
9
server/controllers/assets/constants/request_types.js
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
const EMBED = 'EMBED';
|
||||||
|
const BROWSER = 'BROWSER';
|
||||||
|
const SOCIAL = 'SOCIAL';
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
EMBED,
|
||||||
|
BROWSER,
|
||||||
|
SOCIAL,
|
||||||
|
};
|
|
@ -3,12 +3,12 @@ const handleShowRender = require('../../../render/build/handleShowRender.js');
|
||||||
|
|
||||||
const lbryUri = require('../utils/lbryUri.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 getClaimIdAndServeAsset = require('../utils/getClaimIdAndServeAsset.js');
|
||||||
const flipClaimNameAndId = require('../utils/flipClaimNameAndId.js');
|
const flipClaimNameAndId = require('../utils/flipClaimNameAndId.js');
|
||||||
const logRequestData = require('../utils/logRequestData.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) {
|
} catch (error) {
|
||||||
return res.status(400).json({success: false, message: error.message});
|
return res.status(400).json({success: false, message: error.message});
|
||||||
}
|
}
|
||||||
let responseType = determineResponseType(hasFileExtension, headers);
|
let requestType = determineRequestType(hasFileExtension, headers);
|
||||||
if (responseType !== SERVE) {
|
if (requestType == BROWSER) {
|
||||||
return handleShowRender(req, res);
|
return handleShowRender(req, res);
|
||||||
}
|
}
|
||||||
|
if (requestType == SOCIAL) {
|
||||||
|
return handleSocialRequest(req, res);
|
||||||
|
}
|
||||||
// handle serve request
|
// handle serve request
|
||||||
// send google analytics
|
// send google analytics
|
||||||
sendGAServeEvent(headers, ip, originalUrl);
|
sendGAServeEvent(headers, ip, originalUrl);
|
||||||
|
@ -51,7 +54,7 @@ const serverAssetByIdentifierAndClaim = (req, res) => {
|
||||||
[claimId, claimName] = flipClaimNameAndId(claimId, claimName);
|
[claimId, claimName] = flipClaimNameAndId(claimId, claimName);
|
||||||
}
|
}
|
||||||
// log the request data for debugging
|
// 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
|
// get the claim Id and then serve the asset
|
||||||
getClaimIdAndServeAsset(channelName, channelClaimId, claimName, claimId, originalUrl, ip, res);
|
getClaimIdAndServeAsset(channelName, channelClaimId, claimName, claimId, originalUrl, ip, res);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
|
const { EMBED, BROWSER, SOCIAL } = require('../constants/request_types.js');
|
||||||
const SERVE = 'SERVE';
|
|
||||||
const SHOW = 'SHOW';
|
|
||||||
|
|
||||||
function headersMatchesSocialBotList (headers) {
|
function headersMatchesSocialBotList (headers) {
|
||||||
const socialBotList = [
|
const socialBotList = [
|
||||||
|
@ -33,27 +31,31 @@ function clientWantsAsset ({accept, range}) {
|
||||||
return imageIsWanted || videoIsWanted;
|
return imageIsWanted || videoIsWanted;
|
||||||
};
|
};
|
||||||
|
|
||||||
const determineResponseType = (hasFileExtension, headers) => {
|
const determineRequestType = (hasFileExtension, headers) => {
|
||||||
let responseType;
|
let responseType;
|
||||||
logger.debug('headers:', headers);
|
logger.debug('headers:', headers);
|
||||||
// return early with 'show' if headers match the list
|
// return early with 'show' if headers match the list
|
||||||
if (headersMatchesSocialBotList(headers)) {
|
if (headersMatchesSocialBotList(headers)) {
|
||||||
// return SHOW;
|
return SOCIAL;
|
||||||
}
|
}
|
||||||
// fallback logic if not on the list
|
// if request is not from a social bot...
|
||||||
if (hasFileExtension) {
|
if (hasFileExtension) {
|
||||||
responseType = SERVE; // assume a serve request if file extension is present
|
// assume embed,
|
||||||
if (clientAcceptsHtml(headers)) { // if the request comes from a browser, change it to a show request
|
responseType = EMBED;
|
||||||
responseType = SHOW;
|
// but change to browser if client accepts html.
|
||||||
|
if (clientAcceptsHtml(headers)) {
|
||||||
|
responseType = BROWSER;
|
||||||
}
|
}
|
||||||
|
// if request does not have file extentsion...
|
||||||
} else {
|
} else {
|
||||||
responseType = SHOW;
|
// assume browser,
|
||||||
if (clientWantsAsset(headers) && requestIsFromBrowser(headers)) { // this is in case someone embeds a show url
|
responseType = BROWSER;
|
||||||
logger.debug('Show request came from browser but wants an image/video. Changing response to serve...');
|
// but change to embed if someone embeded a show url...
|
||||||
responseType = SERVE;
|
if (clientWantsAsset(headers) && requestIsFromBrowser(headers)) {
|
||||||
|
responseType = EMBED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return responseType;
|
return responseType;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = determineResponseType;
|
module.exports = determineRequestType;
|
Loading…
Reference in a new issue