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
|
|
|
|
2017-09-08 00:31:32 +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) => {
|
2018-02-22 19:48:46 +01:00
|
|
|
const { headers, ip, originalUrl, params } = req;
|
2018-02-01 04:12:54 +01:00
|
|
|
// decide if this is a show request
|
|
|
|
let hasFileExtension;
|
|
|
|
try {
|
|
|
|
({ hasFileExtension } = lbryUri.parseModifier(params.claim));
|
|
|
|
} catch (error) {
|
2018-02-13 21:19:57 +01:00
|
|
|
return res.status(400).json({success: false, message: error.message});
|
2018-02-01 04:12:54 +01:00
|
|
|
}
|
|
|
|
let responseType = determineResponseType(hasFileExtension, headers);
|
|
|
|
if (responseType !== SERVE) {
|
2018-02-22 19:48:46 +01:00
|
|
|
return handleShowRender(req, res);
|
2018-02-01 04:12:54 +01:00
|
|
|
}
|
2018-02-15 22:18:22 +01:00
|
|
|
// handle serve request
|
|
|
|
// send google analytics
|
|
|
|
sendGAServeEvent(headers, ip, originalUrl);
|
2018-02-01 04:12:54 +01:00
|
|
|
// parse the claim
|
|
|
|
let claimName;
|
|
|
|
try {
|
|
|
|
({ claimName } = lbryUri.parseClaim(params.claim));
|
|
|
|
} catch (error) {
|
2018-02-13 21:19:57 +01:00
|
|
|
return res.status(400).json({success: false, message: error.message});
|
2018-02-01 04:12:54 +01:00
|
|
|
}
|
|
|
|
// 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) {
|
2018-02-13 21:19:57 +01:00
|
|
|
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);
|
2018-02-01 04:12:54 +01:00
|
|
|
// 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
|
|
|
};
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|
2018-03-29 02:35:41 +02:00
|
|
|
|
|
|
|
module.exports = identifierClaim;
|