2018-07-19 00:33:11 +02:00
|
|
|
const logger = require('winston');
|
|
|
|
|
2018-04-27 18:54:36 +02:00
|
|
|
const { sendGAServeEvent } = require('../../../utils/googleAnalytics');
|
2018-12-14 04:55:59 +01:00
|
|
|
const handleShowRender = require('../../../render/handleShowRender');
|
2018-03-29 20:24:52 +02:00
|
|
|
|
2018-08-02 02:31:20 +02:00
|
|
|
const lbryUri = require('../../../../utils/lbryUri.js');
|
2018-04-27 19:24:40 +02:00
|
|
|
|
2018-06-26 00:01:50 +02:00
|
|
|
const determineRequestType = require('../utils/determineRequestType.js');
|
2018-04-27 19:24:40 +02:00
|
|
|
const getClaimIdAndServeAsset = require('../utils/getClaimIdAndServeAsset.js');
|
2018-04-27 23:29:00 +02:00
|
|
|
const flipClaimNameAndId = require('../utils/flipClaimNameAndId.js');
|
2018-04-27 19:24:40 +02:00
|
|
|
|
2018-07-17 23:44:15 +02:00
|
|
|
const { SHOW } = require('../constants/request_types.js');
|
2018-03-29 20:24:52 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
route to serve an asset or the react app via the claim name and an identifier
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2018-06-26 03:42:30 +02:00
|
|
|
const serverByIdentifierAndClaim = (req, res) => {
|
2018-03-29 20:24:52 +02:00
|
|
|
const { headers, ip, originalUrl, params } = req;
|
2018-07-19 01:17:20 +02:00
|
|
|
|
2018-03-29 20:24:52 +02:00
|
|
|
try {
|
2018-07-19 01:23:58 +02:00
|
|
|
let hasFileExtension, claimName, isChannel, channelName, channelClaimId, claimId;
|
|
|
|
|
|
|
|
({ hasFileExtension } = lbryUri.parseModifier(params.claim));
|
2018-07-19 01:17:20 +02:00
|
|
|
if (determineRequestType(hasFileExtension, headers) === SHOW) {
|
2018-07-19 01:23:58 +02:00
|
|
|
logger.debug('show request:', { headers, ip, originalUrl, params });
|
2018-07-19 01:17:20 +02:00
|
|
|
return handleShowRender(req, res);
|
|
|
|
}
|
|
|
|
|
2018-07-19 01:23:58 +02:00
|
|
|
({ claimName } = lbryUri.parseClaim(params.claim));
|
|
|
|
({ isChannel, channelName, channelClaimId, claimId } = lbryUri.parseIdentifier(params.identifier));
|
2018-07-19 01:17:20 +02:00
|
|
|
|
|
|
|
if (!isChannel) {
|
|
|
|
[claimId, claimName] = flipClaimNameAndId(claimId, claimName);
|
|
|
|
}
|
|
|
|
|
2018-08-08 20:39:35 +02:00
|
|
|
logger.debug('serve request:', {
|
|
|
|
headers,
|
|
|
|
ip,
|
|
|
|
originalUrl,
|
|
|
|
params,
|
|
|
|
channelName,
|
|
|
|
channelClaimId,
|
|
|
|
claimName,
|
|
|
|
claimId,
|
|
|
|
});
|
2018-07-31 02:02:18 +02:00
|
|
|
|
2018-10-25 00:11:52 +02:00
|
|
|
getClaimIdAndServeAsset(channelName, channelClaimId, claimName, claimId, originalUrl, ip, res, headers);
|
2018-07-19 01:17:20 +02:00
|
|
|
|
2018-07-19 01:23:58 +02:00
|
|
|
sendGAServeEvent(headers, ip, originalUrl);
|
2018-03-29 20:24:52 +02:00
|
|
|
} catch (error) {
|
|
|
|
return res.status(400).json({success: false, message: error.message});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-26 03:42:30 +02:00
|
|
|
module.exports = serverByIdentifierAndClaim;
|