2018-04-27 18:54:36 +02:00
|
|
|
const { sendGAServeEvent } = require('../../../utils/googleAnalytics');
|
|
|
|
const handleShowRender = require('../../../render/build/handleShowRender.js');
|
2018-04-27 19:24:40 +02:00
|
|
|
|
|
|
|
const lbryUri = require('../utils/lbryUri.js');
|
|
|
|
|
|
|
|
const determineResponseType = require('../utils/determineResponseType.js');
|
|
|
|
const getClaimIdAndServeAsset = require('../utils/getClaimIdAndServeAsset.js');
|
|
|
|
const logRequestData = require('../utils/logRequestData.js');
|
|
|
|
|
2018-03-29 20:24:52 +02:00
|
|
|
const SERVE = 'SERVE';
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
route to serve an asset or the react app via the claim name only
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
const serverAssetByClaim = (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);
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
getClaimIdAndServeAsset(null, null, claimName, null, originalUrl, ip, res);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = serverAssetByClaim;
|