spee.ch/server/controllers/assets/serveByIdentifierAndClaim/index.js

70 lines
2.2 KiB
JavaScript
Raw Normal View History

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');
const handleShowRender = require('../../../render/build/handleShowRender.js');
2018-03-29 20:24:52 +02:00
2018-04-27 19:24:40 +02:00
const lbryUri = require('../utils/lbryUri.js');
const determineRequestType = require('../utils/determineRequestType.js');
2018-04-27 19:24:40 +02:00
const getClaimIdAndServeAsset = require('../utils/getClaimIdAndServeAsset.js');
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-06-26 03:42:30 +02:00
// parse request
2018-03-29 20:24:52 +02:00
let hasFileExtension;
try {
({ hasFileExtension } = lbryUri.parseModifier(params.claim));
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
2018-06-26 03:42:30 +02:00
// determine request type
let requestType = determineRequestType(hasFileExtension, headers);
2018-07-19 00:33:11 +02:00
/* test logging */
logger.info('serveByIdentifierAndClaim', {
headers,
ip,
originalUrl,
params,
response: requestType,
});
/* end test logging */
2018-07-17 23:44:15 +02:00
if (requestType === SHOW) {
2018-03-29 20:24:52 +02:00
return handleShowRender(req, res);
}
// 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;
try {
({ isChannel, channelName, channelClaimId, claimId } = lbryUri.parseIdentifier(params.identifier));
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
// for backwards compatability, flip claim name and claim id if necessary
2018-03-29 20:24:52 +02:00
if (!isChannel) {
[claimId, claimName] = flipClaimNameAndId(claimId, claimName);
2018-03-29 20:24:52 +02:00
}
2018-06-26 03:42:30 +02:00
// send google analytics
sendGAServeEvent(headers, ip, originalUrl);
2018-03-29 20:24:52 +02:00
// get the claim Id and then serve the asset
getClaimIdAndServeAsset(channelName, channelClaimId, claimName, claimId, originalUrl, ip, res);
};
2018-06-26 03:42:30 +02:00
module.exports = serverByIdentifierAndClaim;