2018-02-15 13:18:22 -08:00
|
|
|
const { sendGAServeEvent } = require('../helpers/googleAnalytics');
|
|
|
|
const { determineResponseType, flipClaimNameAndIdForBackwardsCompatibility, logRequestData, getClaimIdAndServeAsset } = require('../helpers/serveHelpers.js');
|
2017-12-07 10:13:42 -08:00
|
|
|
const lbryUri = require('../helpers/lbryUri.js');
|
2018-02-22 10:48:46 -08:00
|
|
|
const handleShowRender = require('../helpers/handleShowRender.jsx');
|
2017-09-07 15:31:32 -07:00
|
|
|
const SERVE = 'SERVE';
|
2017-11-27 18:12:58 -08:00
|
|
|
|
2017-06-29 17:10:14 -07:00
|
|
|
module.exports = (app) => {
|
2017-11-30 10:02:18 -08:00
|
|
|
// route to serve a specific asset using the channel or claim id
|
2018-02-22 10:48:46 -08:00
|
|
|
app.get('/:identifier/:claim', (req, res) => {
|
|
|
|
const { headers, ip, originalUrl, params } = req;
|
2018-01-31 19:12:54 -08:00
|
|
|
// decide if this is a show request
|
|
|
|
let hasFileExtension;
|
|
|
|
try {
|
|
|
|
({ hasFileExtension } = lbryUri.parseModifier(params.claim));
|
|
|
|
} catch (error) {
|
2018-02-13 12:19:57 -08:00
|
|
|
return res.status(400).json({success: false, message: error.message});
|
2018-01-31 19:12:54 -08:00
|
|
|
}
|
|
|
|
let responseType = determineResponseType(hasFileExtension, headers);
|
|
|
|
if (responseType !== SERVE) {
|
2018-02-22 10:48:46 -08:00
|
|
|
return handleShowRender(req, res);
|
2018-01-31 19:12:54 -08:00
|
|
|
}
|
2018-02-15 13:18:22 -08:00
|
|
|
// handle serve request
|
|
|
|
// send google analytics
|
|
|
|
sendGAServeEvent(headers, ip, originalUrl);
|
2018-01-31 19:12:54 -08:00
|
|
|
// parse the claim
|
|
|
|
let claimName;
|
|
|
|
try {
|
|
|
|
({ claimName } = lbryUri.parseClaim(params.claim));
|
|
|
|
} catch (error) {
|
2018-02-13 12:19:57 -08:00
|
|
|
return res.status(400).json({success: false, message: error.message});
|
2018-01-31 19:12:54 -08:00
|
|
|
}
|
|
|
|
// parse the identifier
|
|
|
|
let isChannel, channelName, channelClaimId, claimId;
|
2017-12-06 18:36:17 -08:00
|
|
|
try {
|
2017-12-07 10:13:42 -08:00
|
|
|
({ isChannel, channelName, channelClaimId, claimId } = lbryUri.parseIdentifier(params.identifier));
|
2017-12-06 18:36:17 -08:00
|
|
|
} catch (error) {
|
2018-02-13 12:19:57 -08:00
|
|
|
return res.status(400).json({success: false, message: error.message});
|
2017-12-06 18:36:17 -08:00
|
|
|
}
|
|
|
|
if (!isChannel) {
|
|
|
|
[claimId, claimName] = flipClaimNameAndIdForBackwardsCompatibility(claimId, claimName);
|
2017-08-23 17:00:16 -07:00
|
|
|
}
|
2017-11-30 10:02:18 -08:00
|
|
|
// log the request data for debugging
|
2017-11-27 18:12:58 -08:00
|
|
|
logRequestData(responseType, claimName, channelName, claimId);
|
2018-01-31 19:12:54 -08:00
|
|
|
// get the claim Id and then serve the asset
|
2018-02-15 13:18:22 -08:00
|
|
|
getClaimIdAndServeAsset(channelName, channelClaimId, claimName, claimId, originalUrl, ip, res);
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
2017-11-30 10:02:18 -08:00
|
|
|
// route to serve the winning asset at a claim or a channel page
|
2018-02-23 00:01:25 -08:00
|
|
|
app.get('/:claim', (req, res) => {
|
|
|
|
const { headers, ip, originalUrl, params } = req;
|
2018-01-31 19:12:54 -08:00
|
|
|
// decide if this is a show request
|
|
|
|
let hasFileExtension;
|
2017-12-06 18:36:17 -08:00
|
|
|
try {
|
2018-01-31 19:12:54 -08:00
|
|
|
({ hasFileExtension } = lbryUri.parseModifier(params.claim));
|
2017-12-06 18:36:17 -08:00
|
|
|
} catch (error) {
|
2018-02-13 12:19:57 -08:00
|
|
|
return res.status(400).json({success: false, message: error.message});
|
2017-12-06 18:36:17 -08:00
|
|
|
}
|
2018-01-31 19:12:54 -08:00
|
|
|
let responseType = determineResponseType(hasFileExtension, headers);
|
|
|
|
if (responseType !== SERVE) {
|
2018-02-23 00:01:25 -08:00
|
|
|
return handleShowRender(req, res);
|
2017-08-23 12:21:15 -07:00
|
|
|
}
|
2018-02-15 13:18:22 -08:00
|
|
|
// handle serve request
|
|
|
|
// send google analytics
|
|
|
|
sendGAServeEvent(headers, ip, originalUrl);
|
2018-01-31 19:12:54 -08:00
|
|
|
// parse the claim
|
|
|
|
let claimName;
|
|
|
|
try {
|
|
|
|
({claimName} = lbryUri.parseClaim(params.claim));
|
|
|
|
} catch (error) {
|
2018-02-13 12:19:57 -08:00
|
|
|
return res.status(400).json({success: false, message: error.message});
|
2018-01-31 19:12:54 -08:00
|
|
|
}
|
|
|
|
// log the request data for debugging
|
|
|
|
logRequestData(responseType, claimName, null, null);
|
|
|
|
// get the claim Id and then serve the asset
|
2018-02-15 13:18:22 -08:00
|
|
|
getClaimIdAndServeAsset(null, null, claimName, null, originalUrl, ip, res);
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
};
|