spee.ch/server/routes/asset/serveAssetByIdentifierAndClaim.js

56 lines
2 KiB
JavaScript
Raw Normal View History

2018-03-29 18:26:55 +02:00
const { sendGAServeEvent } = require('helpers/googleAnalytics');
const { determineResponseType, flipClaimNameAndIdForBackwardsCompatibility, logRequestData, getClaimIdAndServeAsset } = require('helpers/serveHelpers.js');
const lbryUri = require('helpers/lbryUri.js');
const handleShowRender = require('helpers/handleShowRender.jsx');
2018-03-29 02:35:41 +02:00
const SERVE = 'SERVE';
2017-11-28 03:12:58 +01:00
2018-03-29 02:35:41 +02:00
/*
route to serve an asset or the react app via the claim name and an identifier
*/
const identifierClaim = () => {
return (req, res) => {
const { headers, ip, originalUrl, params } = req;
// 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});
}
let responseType = determineResponseType(hasFileExtension, headers);
if (responseType !== SERVE) {
return handleShowRender(req, res);
}
2018-02-15 22:18:22 +01:00
// handle serve request
// send google analytics
sendGAServeEvent(headers, ip, originalUrl);
// parse the claim
let claimName;
try {
({ claimName } = lbryUri.parseClaim(params.claim));
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
// parse the identifier
let isChannel, channelName, channelClaimId, claimId;
2017-12-07 03:36:17 +01:00
try {
2017-12-07 19:13:42 +01:00
({ isChannel, channelName, channelClaimId, claimId } = lbryUri.parseIdentifier(params.identifier));
2017-12-07 03:36:17 +01:00
} catch (error) {
return res.status(400).json({success: false, message: error.message});
2017-12-07 03:36:17 +01:00
}
if (!isChannel) {
[claimId, claimName] = flipClaimNameAndIdForBackwardsCompatibility(claimId, claimName);
2017-08-24 02:00:16 +02:00
}
2017-11-30 19:02:18 +01:00
// log the request data for debugging
2017-11-28 03:12:58 +01:00
logRequestData(responseType, claimName, channelName, claimId);
// get the claim Id and then serve the asset
2018-02-15 22:18:22 +01:00
getClaimIdAndServeAsset(channelName, channelClaimId, claimName, claimId, originalUrl, ip, res);
2018-03-29 02:35:41 +02:00
};
};
2018-03-29 02:35:41 +02:00
module.exports = identifierClaim;