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 db = require('../models');
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 SERVE = 'SERVE';
const SHOW = 'SHOW';
const SHOWLITE = 'SHOWLITE';
const { SERVE, SHOW, SHOWLITE } = require('../helpers/constants.js');
function checkForLocalAssetByClaimId (claimId, name) {
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...');
return new Promise((resolve, reject) => {
// 1. check locally for claim
@ -94,40 +92,15 @@ function getAssetByClaimId (fullClaimId, name) {
}
module.exports = {
getAssetByShortId: function (shortId, name) {
logger.debug('...getting asset by short id...');
getAssetByClaim (claimName, claimId) {
logger.debug('getting asset by claim');
return new Promise((resolve, reject) => {
// get the full claimId
getFullClaimIdFromShortId(shortId, name)
// get the asset by the claimId
.then(claimId => {
logger.debug('claim id =', claimId);
resolve(getAssetByClaimId(claimId, name));
})
.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));
// 1. get the long claim id
getLongClaimId(claimName, claimId) // here
// 2. get the claim Id
.then(longClaimId => {
logger.debug('long claim id = ', longClaimId);
resolve(getAssetByLongClaimId(longClaimId, claimName));
})
.catch(error => {
reject(error);
@ -135,7 +108,7 @@ module.exports = {
});
},
getAssetByChannel (channelName, channelId, claimName) {
logger.debug('channelId =', channelId);
logger.debug('getting asset by channel');
return new Promise((resolve, reject) => {
// 1. get the long channel id
getLongChannelId(channelName, channelId)
@ -146,7 +119,7 @@ module.exports = {
// 3. get the asset by this claim id and name
.then(claimId => {
logger.debug('asset claim id = ', claimId);
resolve(getAssetByClaimId(claimId, claimName));
resolve(getAssetByLongClaimId(claimId, claimName));
})
.catch(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) {
let claimIndex;
let shortId = longId.substring(0, 1); // default sort id is the first letter
@ -108,23 +144,14 @@ module.exports = {
const openGraphInfo = createOpenGraphInfo(fileInfo);
res.status(200).render('showLite', { layout: 'show', fileInfo, openGraphInfo });
},
getFullClaimIdFromShortId (shortId, name) {
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);
getLongClaimId (claimName, claimId) { // read the various inputs and decide how to return the long claim id
if (claimId && (claimId.length === 40)) {
return new Promise((resolve, reject) => resolve(claimId));
} else if (claimId && claimId.length < 40) {
return getLongClaimIdFromShortClaimId(claimName, claimId); // need to create this function
} else { // if no claim id provided
return getTopFreeClaimIdByClaimName(claimName);
}
})
.catch(error => {
reject(error);
});
});
},
getShortClaimIdFromLongClaimId (claimId, claimName) {
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) {
return new Promise((resolve, reject) => {
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 { 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 SERVE = 'SERVE';
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');
}
}
const { SERVE, SHOW, SHOWLITE, CHANNEL, CLAIM, CHANNELID_INDICATOR } = require('../helpers/constants.js');
function isValidClaimId (claimId) {
return ((claimId.length === 40) && !/[^A-Za-z0-9]/g.test(claimId));
@ -37,6 +15,17 @@ function isValidShortIdOrClaimId (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) => {
// route to serve a specific asset
app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => {
@ -44,8 +33,7 @@ module.exports = (app) => {
let name = params.name;
let claimType;
let channelName = null;
let shortId = null;
let fullClaimId = null;
let claimId = null;
let channelId = null;
let method;
let extension;
@ -87,21 +75,13 @@ module.exports = (app) => {
channelName = channelName.substring(0, channelIdIndex);
}
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 {
logger.error('The URL provided could not be parsed');
res.send('that url is invalid');
return;
};
claimId = identifier;
logger.debug('claim id =', claimId);
claimType = CLAIM;
}
// 1. retrieve the asset and information
getAsset(claimType, channelName, channelId, shortId, fullClaimId, name)
getAsset(claimType, channelName, channelId, name, claimId)
// 2. serve or show
.then(fileInfo => {
logger.debug('fileInfo', fileInfo);
@ -171,7 +151,7 @@ module.exports = (app) => {
logger.debug('claim name = ', name);
logger.debug('method =', method);
// 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
.then(fileInfo => {
if (!fileInfo) {