2017-08-01 18:58:13 -07:00
|
|
|
const logger = require('winston');
|
2018-01-30 11:46:22 -08:00
|
|
|
const { getClaimId, getLocalFileRecord } = require('../controllers/serveController.js');
|
2017-11-27 14:35:29 -08:00
|
|
|
const serveHelpers = require('../helpers/serveHelpers.js');
|
2017-08-02 17:13:02 -07:00
|
|
|
const { handleRequestError } = require('../helpers/errorHandlers.js');
|
2018-01-22 22:00:04 -08:00
|
|
|
const { postToStats } = require('../helpers/statsHelpers.js');
|
2017-11-21 12:53:43 -08:00
|
|
|
const db = require('../models');
|
2017-12-07 10:13:42 -08:00
|
|
|
const lbryUri = require('../helpers/lbryUri.js');
|
2017-09-07 15:31:32 -07:00
|
|
|
|
|
|
|
const SERVE = 'SERVE';
|
|
|
|
const SHOW = 'SHOW';
|
|
|
|
const SHOWLITE = 'SHOWLITE';
|
2017-10-17 15:09:43 -07:00
|
|
|
const NO_CHANNEL = 'NO_CHANNEL';
|
2017-10-17 15:50:35 -07:00
|
|
|
const NO_CLAIM = 'NO_CLAIM';
|
2017-11-27 15:57:20 -08:00
|
|
|
const NO_FILE = 'NO_FILE';
|
2017-07-25 01:52:31 -07:00
|
|
|
|
2017-08-03 16:46:44 -07:00
|
|
|
function isValidClaimId (claimId) {
|
2017-08-03 21:59:22 -07:00
|
|
|
return ((claimId.length === 40) && !/[^A-Za-z0-9]/g.test(claimId));
|
2017-08-03 16:46:44 -07:00
|
|
|
}
|
|
|
|
|
2017-08-10 10:49:19 -07:00
|
|
|
function isValidShortId (claimId) {
|
2017-11-27 14:35:29 -08:00
|
|
|
return claimId.length === 1; // it should really evaluate the short url itself
|
2017-08-03 16:46:44 -07:00
|
|
|
}
|
|
|
|
|
2017-08-10 10:49:19 -07:00
|
|
|
function isValidShortIdOrClaimId (input) {
|
|
|
|
return (isValidClaimId(input) || isValidShortId(input));
|
2017-08-03 16:46:44 -07:00
|
|
|
}
|
|
|
|
|
2017-12-13 14:55:26 -08:00
|
|
|
function clientAcceptsHtml ({accept}) {
|
|
|
|
return accept && accept.match(/text\/html/);
|
|
|
|
}
|
|
|
|
|
2017-12-13 18:30:09 -08:00
|
|
|
function requestIsFromBrowser (headers) {
|
|
|
|
return headers['user-agent'] && headers['user-agent'].match(/Mozilla/);
|
|
|
|
};
|
|
|
|
|
2017-12-13 15:09:01 -08:00
|
|
|
function clientWantsAsset ({accept, range}) {
|
2017-12-13 15:32:58 -08:00
|
|
|
const imageIsWanted = accept && accept.match(/image\/.*/) && !accept.match(/text\/html/) && !accept.match(/text\/\*/);
|
2017-12-13 18:30:09 -08:00
|
|
|
const videoIsWanted = accept && range;
|
2017-12-13 14:55:26 -08:00
|
|
|
return imageIsWanted || videoIsWanted;
|
2017-12-06 15:35:40 -08:00
|
|
|
}
|
|
|
|
|
2017-12-06 18:36:17 -08:00
|
|
|
function determineResponseType (isServeRequest, headers) {
|
2017-11-27 13:15:09 -08:00
|
|
|
let responseType;
|
2017-12-06 18:36:17 -08:00
|
|
|
if (isServeRequest) {
|
2017-11-27 13:15:09 -08:00
|
|
|
responseType = SERVE;
|
2017-12-06 15:35:40 -08:00
|
|
|
if (clientAcceptsHtml(headers)) { // this is in case a serve request comes from a browser
|
2017-11-27 13:15:09 -08:00
|
|
|
responseType = SHOWLITE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
responseType = SHOW;
|
2017-12-13 18:30:09 -08:00
|
|
|
if (clientWantsAsset(headers) && requestIsFromBrowser(headers)) { // this is in case someone embeds a show url
|
2017-12-14 10:07:05 -08:00
|
|
|
logger.debug('Show request came from browser and wants an image/video; changing response to serve.');
|
2017-11-29 14:36:51 -08:00
|
|
|
responseType = SERVE;
|
|
|
|
}
|
2017-11-27 13:15:09 -08:00
|
|
|
}
|
|
|
|
return responseType;
|
|
|
|
}
|
|
|
|
|
2018-01-30 11:46:22 -08:00
|
|
|
// function showAssetToClient (claimId, name, res) {
|
|
|
|
// return Promise
|
|
|
|
// .all([db.Claim.resolveClaim(name, claimId), db.Claim.getShortClaimIdFromLongClaimId(claimId, name)])
|
|
|
|
// .then(([claimInfo, shortClaimId]) => {
|
|
|
|
// // logger.debug('claimInfo:', claimInfo);
|
|
|
|
// // logger.debug('shortClaimId:', shortClaimId);
|
|
|
|
// return serveHelpers.showFile(claimInfo, shortClaimId, res);
|
|
|
|
// })
|
|
|
|
// .catch(error => {
|
|
|
|
// throw error;
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// function showLiteAssetToClient (claimId, name, res) {
|
|
|
|
// return Promise
|
|
|
|
// .all([db.Claim.resolveClaim(name, claimId), db.Claim.getShortClaimIdFromLongClaimId(claimId, name)])
|
|
|
|
// .then(([claimInfo, shortClaimId]) => {
|
|
|
|
// // logger.debug('claimInfo:', claimInfo);
|
|
|
|
// // logger.debug('shortClaimId:', shortClaimId);
|
|
|
|
// return serveHelpers.showFileLite(claimInfo, shortClaimId, res);
|
|
|
|
// })
|
|
|
|
// .catch(error => {
|
|
|
|
// throw error;
|
|
|
|
// });
|
|
|
|
// }
|
2017-11-27 14:35:29 -08:00
|
|
|
|
|
|
|
function serveAssetToClient (claimId, name, res) {
|
|
|
|
return getLocalFileRecord(claimId, name)
|
2017-11-27 14:52:46 -08:00
|
|
|
.then(fileInfo => {
|
2017-12-13 14:55:26 -08:00
|
|
|
// logger.debug('fileInfo:', fileInfo);
|
2017-11-27 15:57:20 -08:00
|
|
|
if (fileInfo === NO_FILE) {
|
2017-12-07 10:13:42 -08:00
|
|
|
return res.status(307).redirect(`/api/claim-get/${name}/${claimId}`);
|
2017-11-27 14:52:46 -08:00
|
|
|
}
|
2017-12-07 10:13:42 -08:00
|
|
|
return serveHelpers.serveFile(fileInfo, claimId, name, res);
|
2017-11-27 14:52:46 -08:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
throw error;
|
|
|
|
});
|
2017-11-27 14:35:29 -08:00
|
|
|
}
|
|
|
|
|
2017-11-27 18:12:58 -08:00
|
|
|
function flipClaimNameAndIdForBackwardsCompatibility (identifier, name) {
|
2017-12-14 12:32:20 -08:00
|
|
|
// this is a patch for backwards compatability with '/name/claim_id' url format
|
2017-11-27 18:12:58 -08:00
|
|
|
if (isValidShortIdOrClaimId(name) && !isValidShortIdOrClaimId(identifier)) {
|
|
|
|
const tempName = name;
|
|
|
|
name = identifier;
|
|
|
|
identifier = tempName;
|
|
|
|
}
|
|
|
|
return [identifier, name];
|
|
|
|
}
|
|
|
|
|
|
|
|
function logRequestData (responseType, claimName, channelName, claimId) {
|
|
|
|
logger.debug('responseType ===', responseType);
|
|
|
|
logger.debug('claim name === ', claimName);
|
|
|
|
logger.debug('channel name ===', channelName);
|
|
|
|
logger.debug('claim id ===', claimId);
|
|
|
|
}
|
|
|
|
|
2017-06-29 17:10:14 -07:00
|
|
|
module.exports = (app) => {
|
2017-11-30 10:02:18 -08:00
|
|
|
// route to serve a specific asset using the channel or claim id
|
2017-07-31 17:02:39 -07:00
|
|
|
app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => {
|
2017-12-06 18:36:17 -08:00
|
|
|
let isChannel, channelName, channelClaimId, claimId, claimName, isServeRequest;
|
|
|
|
try {
|
2017-12-07 10:13:42 -08:00
|
|
|
({ isChannel, channelName, channelClaimId, claimId } = lbryUri.parseIdentifier(params.identifier));
|
|
|
|
({ claimName, isServeRequest } = lbryUri.parseName(params.name));
|
2017-12-06 18:36:17 -08:00
|
|
|
} catch (error) {
|
2017-12-06 18:49:05 -08:00
|
|
|
return handleRequestError(originalUrl, ip, error, res);
|
2017-12-06 18:36:17 -08:00
|
|
|
}
|
|
|
|
if (!isChannel) {
|
|
|
|
[claimId, claimName] = flipClaimNameAndIdForBackwardsCompatibility(claimId, claimName);
|
2017-08-23 17:00:16 -07:00
|
|
|
}
|
2017-12-06 18:36:17 -08:00
|
|
|
let responseType = determineResponseType(isServeRequest, headers);
|
2017-11-30 10:02:18 -08:00
|
|
|
// log the request data for debugging
|
2017-11-27 18:12:58 -08:00
|
|
|
logRequestData(responseType, claimName, channelName, claimId);
|
2018-01-30 11:46:22 -08:00
|
|
|
// if a serve request, serve, otherwise send the react app
|
|
|
|
if (responseType === SERVE) {
|
|
|
|
// get the claim Id and then serve/show the asset
|
|
|
|
getClaimId(channelName, channelClaimId, claimName, claimId)
|
|
|
|
.then(fullClaimId => {
|
|
|
|
if (fullClaimId === NO_CLAIM) {
|
|
|
|
return res.status(200).json({success: false, message: 'no claim id could be found'});
|
|
|
|
} else if (fullClaimId === NO_CHANNEL) {
|
|
|
|
return res.status(200).json({success: false, message: 'no channel id could be found'});
|
|
|
|
}
|
|
|
|
serveAssetToClient(fullClaimId, claimName, res);
|
|
|
|
// postToStats(responseType, originalUrl, ip, claimName, fullClaimId, 'success');
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
handleRequestError(originalUrl, ip, error, res);
|
|
|
|
// postToStats(responseType, originalUrl, ip, claimName, fullClaimId, 'fail');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.status(200).render('index');
|
|
|
|
}
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
2017-11-30 10:02:18 -08:00
|
|
|
// route to serve the winning asset at a claim or a channel page
|
2017-12-06 18:36:17 -08:00
|
|
|
app.get('/:identifier', ({ headers, ip, originalUrl, params, query }, res) => {
|
|
|
|
let isChannel, channelName, channelClaimId;
|
|
|
|
try {
|
2017-12-07 10:13:42 -08:00
|
|
|
({ isChannel, channelName, channelClaimId } = lbryUri.parseIdentifier(params.identifier));
|
2018-01-30 15:32:42 -08:00
|
|
|
// log the result
|
|
|
|
logger.debug(`isChannel: ${isChannel}, channelName: ${channelName}, channelClaimId: ${channelClaimId}`);
|
2017-12-06 18:36:17 -08:00
|
|
|
} catch (error) {
|
2017-12-06 18:49:05 -08:00
|
|
|
return handleRequestError(originalUrl, ip, error, res);
|
2017-12-06 18:36:17 -08:00
|
|
|
}
|
|
|
|
if (isChannel) {
|
|
|
|
// handle showing the channel page
|
2018-01-30 09:00:02 -08:00
|
|
|
return res.status(200).render('index');
|
2017-08-03 11:14:50 -07:00
|
|
|
} else {
|
2017-12-06 18:36:17 -08:00
|
|
|
let claimName, isServeRequest;
|
|
|
|
try {
|
2017-12-07 10:13:42 -08:00
|
|
|
({claimName, isServeRequest} = lbryUri.parseName(params.identifier));
|
2017-12-06 18:36:17 -08:00
|
|
|
} catch (error) {
|
2017-12-06 18:49:05 -08:00
|
|
|
return handleRequestError(originalUrl, ip, error, res);
|
2017-12-06 18:36:17 -08:00
|
|
|
}
|
|
|
|
let responseType = determineResponseType(isServeRequest, headers);
|
2017-11-30 10:02:18 -08:00
|
|
|
// log the request data for debugging
|
2017-11-27 18:12:58 -08:00
|
|
|
logRequestData(responseType, claimName, null, null);
|
2018-01-30 11:46:22 -08:00
|
|
|
// if a serve request, serve, otherwise send the react app
|
|
|
|
if (responseType === SERVE) {
|
|
|
|
// get the claim Id and then serve/show the asset
|
|
|
|
getClaimId(null, null, claimName, null)
|
|
|
|
.then(fullClaimId => {
|
|
|
|
if (fullClaimId === NO_CLAIM) {
|
|
|
|
return res.status(200).render('index');
|
|
|
|
}
|
|
|
|
serveAssetToClient(fullClaimId, claimName, res);
|
|
|
|
// postToStats(responseType, originalUrl, ip, claimName, fullClaimId, 'success');
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
handleRequestError(originalUrl, ip, error, res);
|
|
|
|
// postToStats(responseType, originalUrl, ip, claimName, fullClaimId, 'fail');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.status(200).render('index');
|
|
|
|
}
|
2017-08-23 12:21:15 -07:00
|
|
|
}
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
};
|