diff --git a/server/controllers/assets/constants/request_types.js b/server/controllers/assets/constants/request_types.js new file mode 100644 index 00000000..22f0d0ce --- /dev/null +++ b/server/controllers/assets/constants/request_types.js @@ -0,0 +1,9 @@ +const EMBED = 'EMBED'; +const BROWSER = 'BROWSER'; +const SOCIAL = 'SOCIAL'; + +module.exports = { + EMBED, + BROWSER, + SOCIAL, +}; diff --git a/server/controllers/assets/serveByIdentifierAndClaim/index.js b/server/controllers/assets/serveByIdentifierAndClaim/index.js index e493e01e..0caf3a76 100644 --- a/server/controllers/assets/serveByIdentifierAndClaim/index.js +++ b/server/controllers/assets/serveByIdentifierAndClaim/index.js @@ -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); }; diff --git a/server/controllers/assets/utils/determineResponseType.js b/server/controllers/assets/utils/determineRequestType.js similarity index 65% rename from server/controllers/assets/utils/determineResponseType.js rename to server/controllers/assets/utils/determineRequestType.js index 2592ffed..c268380f 100644 --- a/server/controllers/assets/utils/determineResponseType.js +++ b/server/controllers/assets/utils/determineRequestType.js @@ -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;