2018-02-15 22:18:22 +01:00
|
|
|
const { sendGAServeEvent } = require('../helpers/googleAnalytics');
|
|
|
|
const { determineResponseType, flipClaimNameAndIdForBackwardsCompatibility, logRequestData, getClaimIdAndServeAsset } = require('../helpers/serveHelpers.js');
|
2017-12-07 19:13:42 +01:00
|
|
|
const lbryUri = require('../helpers/lbryUri.js');
|
2018-02-22 19:48:46 +01:00
|
|
|
const handleShowRender = require('../helpers/handleShowRender.jsx');
|
2017-09-08 00:31:32 +02:00
|
|
|
const SERVE = 'SERVE';
|
2017-11-28 03:12:58 +01:00
|
|
|
|
2017-06-30 02:10:14 +02:00
|
|
|
module.exports = (app) => {
|
2017-11-30 19:02:18 +01:00
|
|
|
// route to serve a specific asset using the channel or claim id
|
2018-02-22 19:48:46 +01:00
|
|
|
app.get('/:identifier/:claim', (req, res) => {
|
|
|
|
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 res.status(200).render('index');
|
|
|
|
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);
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
2017-11-30 19:02:18 +01:00
|
|
|
// route to serve the winning asset at a claim or a channel page
|
2018-02-01 04:12:54 +01:00
|
|
|
app.get('/:claim', ({ headers, ip, originalUrl, params, query }, res) => {
|
|
|
|
// decide if this is a show request
|
|
|
|
let hasFileExtension;
|
2017-12-07 03:36:17 +01:00
|
|
|
try {
|
2018-02-01 04:12:54 +01:00
|
|
|
({ hasFileExtension } = lbryUri.parseModifier(params.claim));
|
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
|
|
|
}
|
2018-02-01 04:12:54 +01:00
|
|
|
let responseType = determineResponseType(hasFileExtension, headers);
|
|
|
|
if (responseType !== SERVE) {
|
2018-01-30 18:00:02 +01:00
|
|
|
return res.status(200).render('index');
|
2017-08-23 21:21:15 +02: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
|
|
|
}
|
|
|
|
// log the request data for debugging
|
|
|
|
logRequestData(responseType, claimName, null, null);
|
|
|
|
// get the claim Id and then serve the asset
|
2018-02-15 22:18:22 +01:00
|
|
|
getClaimIdAndServeAsset(null, null, claimName, null, originalUrl, ip, res);
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
};
|