spee.ch/routes/asset-routes.js

77 lines
3 KiB
JavaScript
Raw Normal View History

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');
const handleShowRender = require('../helpers/handleShowRender.jsx');
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
app.get('/:identifier/:claim', (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);
});
2017-11-30 19:02:18 +01:00
// route to serve the winning asset at a claim or a channel page
2018-02-23 09:01:25 +01:00
app.get('/:claim', (req, res) => {
const { headers, ip, originalUrl, params } = req;
// decide if this is a show request
let hasFileExtension;
2017-12-07 03:36:17 +01:00
try {
({ hasFileExtension } = lbryUri.parseModifier(params.claim));
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
}
let responseType = determineResponseType(hasFileExtension, headers);
if (responseType !== SERVE) {
2018-02-23 09:01:25 +01:00
return handleShowRender(req, res);
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);
// parse the claim
let claimName;
try {
({claimName} = lbryUri.parseClaim(params.claim));
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
// 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);
});
};