condensed functions on serve-route

This commit is contained in:
bill bittner 2017-08-23 17:00:16 -07:00
parent c4a4f93a32
commit fb900d40be
4 changed files with 85 additions and 113 deletions

View file

@ -1,11 +1,9 @@
const lbryApi = require('../helpers/lbryApi.js'); const lbryApi = require('../helpers/lbryApi.js');
const db = require('../models'); const db = require('../models');
const logger = require('winston'); const logger = require('winston');
const { getTopFreeClaim, getFullClaimIdFromShortId, resolveAgainstClaimTable, serveFile, showFile, showFileLite, getShortClaimIdFromLongClaimId, getClaimIdByLongChannelId, getAllChannelClaims, getLongChannelId, getShortChannelIdFromLongChannelId } = require('../helpers/serveHelpers.js'); const { resolveAgainstClaimTable, serveFile, showFile, showFileLite, getShortClaimIdFromLongClaimId, getClaimIdByLongChannelId, getAllChannelClaims, getLongChannelId, getShortChannelIdFromLongChannelId, getLongClaimId } = require('../helpers/serveHelpers.js');
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js'); const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
const SERVE = 'SERVE'; const { SERVE, SHOW, SHOWLITE } = require('../helpers/constants.js');
const SHOW = 'SHOW';
const SHOWLITE = 'SHOWLITE';
function checkForLocalAssetByClaimId (claimId, name) { function checkForLocalAssetByClaimId (claimId, name) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -45,7 +43,7 @@ function createFileRecord ({ name, claimId, outpoint, height, address, nsfw }) {
}; };
} }
function getAssetByClaimId (fullClaimId, name) { function getAssetByLongClaimId (fullClaimId, name) {
logger.debug('...getting asset by claim Id...'); logger.debug('...getting asset by claim Id...');
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// 1. check locally for claim // 1. check locally for claim
@ -94,40 +92,15 @@ function getAssetByClaimId (fullClaimId, name) {
} }
module.exports = { module.exports = {
getAssetByShortId: function (shortId, name) { getAssetByClaim (claimName, claimId) {
logger.debug('...getting asset by short id...'); logger.debug('getting asset by claim');
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// get the full claimId // 1. get the long claim id
getFullClaimIdFromShortId(shortId, name) getLongClaimId(claimName, claimId) // here
// get the asset by the claimId // 2. get the claim Id
.then(claimId => { .then(longClaimId => {
logger.debug('claim id =', claimId); logger.debug('long claim id = ', longClaimId);
resolve(getAssetByClaimId(claimId, name)); resolve(getAssetByLongClaimId(longClaimId, claimName));
})
.catch(error => {
reject(error);
});
});
},
getAssetByClaimId (fullClaimId, name) {
return getAssetByClaimId(fullClaimId, name);
},
getAssetByName (name) {
logger.debug('...getting asset by claim name...');
return new Promise((resolve, reject) => {
// 1. get a list of the free public claims
getTopFreeClaim(name)
// 2. check locally for the top claim
.then(topFreeClaim => {
// if no claims were found, return null
if (!topFreeClaim) {
return resolve(null);
}
// parse the result
const claimId = topFreeClaim.claimId;
logger.debug('top free claim id =', claimId);
// get the asset
resolve(getAssetByClaimId(claimId, name));
}) })
.catch(error => { .catch(error => {
reject(error); reject(error);
@ -135,7 +108,7 @@ module.exports = {
}); });
}, },
getAssetByChannel (channelName, channelId, claimName) { getAssetByChannel (channelName, channelId, claimName) {
logger.debug('channelId =', channelId); logger.debug('getting asset by channel');
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// 1. get the long channel id // 1. get the long channel id
getLongChannelId(channelName, channelId) getLongChannelId(channelName, channelId)
@ -146,7 +119,7 @@ module.exports = {
// 3. get the asset by this claim id and name // 3. get the asset by this claim id and name
.then(claimId => { .then(claimId => {
logger.debug('asset claim id = ', claimId); logger.debug('asset claim id = ', claimId);
resolve(getAssetByClaimId(claimId, claimName)); resolve(getAssetByLongClaimId(claimId, claimName));
}) })
.catch(error => { .catch(error => {
reject(error); reject(error);

8
helpers/constants.js Normal file
View file

@ -0,0 +1,8 @@
module.exports = {
SERVE : 'SERVE',
SHOW : 'SHOW',
SHOWLITE : 'SHOWLITE',
CHANNEL : 'CHANNEL',
CLAIM : 'CLAIM',
CHANNELID_INDICATOR: ':',
};

View file

@ -51,6 +51,42 @@ function getLongChannelIdFromChannelName (channelName) {
}); });
} }
function getLongClaimIdFromShortClaimId (name, shortId) {
return new Promise((resolve, reject) => {
logger.debug('getting claim_id from short url');
// use the daemon to check for claims list
db.sequelize.query(`SELECT claimId FROM Claim WHERE name = '${name}' AND claimId LIKE '${shortId}%' ORDER BY height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
.then(result => {
switch (result.length) {
case 0:
return reject(new Error('That is an invalid Short Claim Id'));
default: // note results must be sorted
return resolve(result[0].claimId);
}
})
.catch(error => {
reject(error);
});
});
}
function getTopFreeClaimIdByClaimName (name) {
return new Promise((resolve, reject) => {
db.sequelize.query(`SELECT claimId FROM Claim WHERE name = '${name}' ORDER BY amount DESC, height ASC LIMIT 1`, { type: db.sequelize.QueryTypes.SELECT })
.then(result => {
switch (result.length) {
case 0:
return resolve(null);
default:
return resolve(result[0].claimId);
}
})
.catch(error => {
reject(error);
});
});
};
function sortResult (result, longId) { function sortResult (result, longId) {
let claimIndex; let claimIndex;
let shortId = longId.substring(0, 1); // default sort id is the first letter let shortId = longId.substring(0, 1); // default sort id is the first letter
@ -108,23 +144,14 @@ module.exports = {
const openGraphInfo = createOpenGraphInfo(fileInfo); const openGraphInfo = createOpenGraphInfo(fileInfo);
res.status(200).render('showLite', { layout: 'show', fileInfo, openGraphInfo }); res.status(200).render('showLite', { layout: 'show', fileInfo, openGraphInfo });
}, },
getFullClaimIdFromShortId (shortId, name) { getLongClaimId (claimName, claimId) { // read the various inputs and decide how to return the long claim id
return new Promise((resolve, reject) => { if (claimId && (claimId.length === 40)) {
logger.debug('getting claim_id from short url'); return new Promise((resolve, reject) => resolve(claimId));
// use the daemon to check for claims list } else if (claimId && claimId.length < 40) {
db.sequelize.query(`SELECT claimId FROM Claim WHERE name = '${name}' AND claimId LIKE '${shortId}%' ORDER BY height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) return getLongClaimIdFromShortClaimId(claimName, claimId); // need to create this function
.then(result => { } else { // if no claim id provided
switch (result.length) { return getTopFreeClaimIdByClaimName(claimName);
case 0: }
return reject(new Error('That is an invalid Short Claim Id'));
default: // note results must be sorted
return resolve(result[0].claimId);
}
})
.catch(error => {
reject(error);
});
});
}, },
getShortClaimIdFromLongClaimId (claimId, claimName) { getShortClaimIdFromLongClaimId (claimId, claimName) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -159,22 +186,6 @@ module.exports = {
}); });
}); });
}, },
getTopFreeClaim (name) {
return new Promise((resolve, reject) => {
db.sequelize.query(`SELECT * FROM Claim WHERE name = '${name}' ORDER BY amount DESC, height ASC LIMIT 1`, { type: db.sequelize.QueryTypes.SELECT })
.then(result => {
switch (result.length) {
case 0:
return resolve(null);
default:
return resolve(result[0]);
}
})
.catch(error => {
reject(error);
});
});
},
resolveAgainstClaimTable (name, claimId) { resolveAgainstClaimTable (name, claimId) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
db.sequelize.query(`SELECT * FROM Claim WHERE name = '${name}' AND claimId = '${claimId}'`, { type: db.sequelize.QueryTypes.SELECT }) db.sequelize.query(`SELECT * FROM Claim WHERE name = '${name}' AND claimId = '${claimId}'`, { type: db.sequelize.QueryTypes.SELECT })

View file

@ -1,29 +1,7 @@
const logger = require('winston'); const logger = require('winston');
const { getAssetByShortId, getAssetByClaimId, getAssetByName, getChannelContents, getAssetByChannel, serveOrShowAsset } = require('../controllers/serveController.js'); const { getAssetByClaim, getChannelContents, getAssetByChannel, serveOrShowAsset } = require('../controllers/serveController.js');
const { handleRequestError } = require('../helpers/errorHandlers.js'); const { handleRequestError } = require('../helpers/errorHandlers.js');
const SERVE = 'SERVE'; const { SERVE, SHOW, SHOWLITE, CHANNEL, CLAIM, CHANNELID_INDICATOR } = require('../helpers/constants.js');
const SHOW = 'SHOW';
const SHOWLITE = 'SHOWLITE';
const CHANNEL = 'CHANNEL';
const CLAIM_ID_SHORT = 'CLAIM_ID_SHORT';
const CLAIM_ID_LONG = 'CLAIM_ID_LONG';
const CLAIM_NAME = 'CLAIM_NAME';
const CHANNELID_INDICATOR = ':';
function getAsset (claimType, channelName, channelId, shortId, fullClaimId, name) {
switch (claimType) {
case CHANNEL:
return getAssetByChannel(channelName, channelId, name);
case CLAIM_ID_SHORT:
return getAssetByShortId(shortId, name);
case CLAIM_ID_LONG:
return getAssetByClaimId(fullClaimId, name);
case CLAIM_NAME:
return getAssetByName(name);
default:
return new Error('that claim type was not found');
}
}
function isValidClaimId (claimId) { function isValidClaimId (claimId) {
return ((claimId.length === 40) && !/[^A-Za-z0-9]/g.test(claimId)); return ((claimId.length === 40) && !/[^A-Za-z0-9]/g.test(claimId));
@ -37,6 +15,17 @@ function isValidShortIdOrClaimId (input) {
return (isValidClaimId(input) || isValidShortId(input)); return (isValidClaimId(input) || isValidShortId(input));
} }
function getAsset (claimType, channelName, channelId, name, claimId) {
switch (claimType) {
case CHANNEL:
return getAssetByChannel(channelName, channelId, name);
case CLAIM:
return getAssetByClaim(name, claimId);
default:
return new Error('that claim type was not found');
}
}
module.exports = (app) => { module.exports = (app) => {
// route to serve a specific asset // route to serve a specific asset
app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => { app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => {
@ -44,8 +33,7 @@ module.exports = (app) => {
let name = params.name; let name = params.name;
let claimType; let claimType;
let channelName = null; let channelName = null;
let shortId = null; let claimId = null;
let fullClaimId = null;
let channelId = null; let channelId = null;
let method; let method;
let extension; let extension;
@ -87,21 +75,13 @@ module.exports = (app) => {
channelName = channelName.substring(0, channelIdIndex); channelName = channelName.substring(0, channelIdIndex);
} }
logger.debug('channel name =', channelName); logger.debug('channel name =', channelName);
} else if (identifier.length === 40) {
fullClaimId = identifier;
logger.debug('full claim id =', fullClaimId);
claimType = CLAIM_ID_LONG;
} else if (identifier.length < 40) {
shortId = identifier;
logger.debug('short claim id =', shortId);
claimType = CLAIM_ID_SHORT;
} else { } else {
logger.error('The URL provided could not be parsed'); claimId = identifier;
res.send('that url is invalid'); logger.debug('claim id =', claimId);
return; claimType = CLAIM;
}; }
// 1. retrieve the asset and information // 1. retrieve the asset and information
getAsset(claimType, channelName, channelId, shortId, fullClaimId, name) getAsset(claimType, channelName, channelId, name, claimId)
// 2. serve or show // 2. serve or show
.then(fileInfo => { .then(fileInfo => {
logger.debug('fileInfo', fileInfo); logger.debug('fileInfo', fileInfo);
@ -171,7 +151,7 @@ module.exports = (app) => {
logger.debug('claim name = ', name); logger.debug('claim name = ', name);
logger.debug('method =', method); logger.debug('method =', method);
// 1. retrieve the asset and information // 1. retrieve the asset and information
getAsset(CLAIM_NAME, null, null, null, null, name) getAsset(CLAIM, null, null, null, name, null)
// 2. respond to the request // 2. respond to the request
.then(fileInfo => { .then(fileInfo => {
if (!fileInfo) { if (!fileInfo) {