Merge branch 'master' of github.com:lbryio/spee.ch into claim-table

This commit is contained in:
bill bittner 2017-08-14 10:54:07 -07:00
commit ddc823aaec
4 changed files with 59 additions and 46 deletions

View file

@ -94,10 +94,10 @@ module.exports = {
// get teh asset by claim Id // get teh asset by claim Id
}); });
}, },
getAssetByShortUrl: function (shortUrl, name) { getAssetByShortId: function (shortId, name) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// get the full claimId // get the full claimId
serveHelpers.getClaimIdFromShortUrl(shortUrl, name) serveHelpers.getFullClaimIdFromShortId(shortId, name)
// get the asset by the claimId // get the asset by the claimId
.then(claimId => { .then(claimId => {
resolve(getAssetByClaimId(claimId, name)); resolve(getAssetByClaimId(claimId, name));

View file

@ -2,47 +2,59 @@ const logger = require('winston');
const db = require('../models'); const db = require('../models');
const lbryApi = require('./lbryApi'); const lbryApi = require('./lbryApi');
function determineShortUrl (claimId, height, claimList) { function determineShortClaimId (claimId, height, claimList) {
logger.debug('determining short url based on claim id and claim list'); logger.debug('determining short url based on claim id and claim list');
logger.debug('claimlist starting length:', claimList.length);
// remove this claim from the claim list, if it exists // remove this claim from the claim list, if it exists
claimList = claimList.filter(claim => { claimList = claimList.filter(claim => {
return claim.claim_id !== claimId; return claim.claim_id !== claimId;
}); });
logger.debug('claim list length:', claimList.length); logger.debug('claim list length without this claim:', claimList.length);
// if there are no other claims, return the first letter of the claim id // If there are no other claims, return the first letter of the claim id...
if (claimList.length === 0) { if (claimList.length === 0) {
return claimId.substring(0, 1); return claimId.substring(0, 1);
// otherwise determine the proper url // ...otherwise determine the proper short id.
} else { } else {
let i = 0;
const claimListCopy = claimList; const claimListCopy = claimList;
while (claimList.length !== 0) { // filter out matching claims let i = 0;
// find the longest shared prefix (there is a better way to do this that filters, checks next filter, then filters (i.e. combine this step and next))
while (claimList.length !== 0) {
i++; i++;
claimList = claimList.filter(claim => { claimList = claimList.filter(claim => {
return (claim.claim_id.substring(0, i) === claimId.substring(0, i)); const otherClaimIdSegmentToCompare = claim.claim_id.substring(0, i);
const thisClaimIdSegmentToCompare = claimId.substring(0, i);
logger.debug('compare:', otherClaimIdSegmentToCompare, '===', thisClaimIdSegmentToCompare, '?');
return (otherClaimIdSegmentToCompare === thisClaimIdSegmentToCompare);
}); });
} }
i -= 1; // use that longest shared prefix to get only those competing claims
const lastMatch = claimId.substring(0, i); const lastMatchIndex = i - 1;
const lastMatch = claimId.substring(0, lastMatchIndex);
const matchingClaims = claimListCopy.filter(claim => { logger.debug('last match index:', lastMatchIndex, 'last match:', lastMatch);
return (claim.claim_id.substring(0, i) === lastMatch); if (lastMatchIndex === 0) { // if no other claims share a prefix, return with first letter.
}); return claimId.substring(0, 1);
for (let j = 0; j < matchingClaims.length; j++) {
if (matchingClaims[j].height < height) {
return claimId.substring(0, i + 1);
}
} }
return claimId.substring(0, i); const allMatchingClaimsAtLastMatch = claimListCopy.filter(claim => {
return (claim.claim_id.substring(0, lastMatchIndex) === lastMatch);
});
// for those that share the longest shared prefix: see which came first in time. whichever is earliest, the others take the extra character
const sortedMatchingClaims = allMatchingClaimsAtLastMatch.sort((a, b) => {
return (a.height < b.height);
});
// compare to the earliest one, if it is earlier, this claim takes the extra character
if (sortedMatchingClaims[0].height < height) {
return claimId.substring(0, lastMatchIndex + 1);
}
return claimId.substring(0, lastMatchIndex);
} }
} }
function checkLocalDbForClaims (name, shortUrl) { function checkLocalDbForClaims (name, shortId) {
return db.File return db.File
.findAll({ .findAll({
where: { where: {
name, name,
claimId: { $like: `${shortUrl}%` }, claimId: { $like: `${shortId}%` },
}, },
}) })
.then(records => { .then(records => {
@ -102,7 +114,7 @@ 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 });
}, },
getClaimIdFromShortUrl (shortUrl, name) { getFullClaimIdFromShortId (shortId, name) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
logger.debug('getting claim_id from short url'); logger.debug('getting claim_id from short url');
// use the daemon to check for claims list // use the daemon to check for claims list
@ -111,7 +123,7 @@ module.exports = {
logger.debug('Number of claims from getClaimsList:', claims.length); logger.debug('Number of claims from getClaimsList:', claims.length);
// if no claims were found, check locally for possible claims // if no claims were found, check locally for possible claims
if (claims.length === 0) { if (claims.length === 0) {
return checkLocalDbForClaims(name, shortUrl); return checkLocalDbForClaims(name, shortId);
} else { } else {
return claims; return claims;
} }
@ -119,7 +131,7 @@ module.exports = {
// handle the claims list // handle the claims list
.then(claims => { .then(claims => {
logger.debug('Claims ready for filtering'); logger.debug('Claims ready for filtering');
const regex = new RegExp(`^${shortUrl}`); const regex = new RegExp(`^${shortId}`);
const filteredClaimsList = claims.filter(claim => { const filteredClaimsList = claims.filter(claim => {
return regex.test(claim.claim_id); return regex.test(claim.claim_id);
}); });
@ -143,15 +155,16 @@ module.exports = {
}); });
}); });
}, },
getShortUrlFromClaimId (claimId, height, name) { getShortIdFromClaimId (claimId, height, name) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
logger.debug('finding short url from claim_id'); logger.debug('finding short claim id from full claim id');
// get a list of all the claims // get a list of all the claims
lbryApi.getClaimsList(name) lbryApi.getClaimsList(name)
// find the smallest possible unique url for this claim // find the smallest possible unique url for this claim
.then(({ claims }) => { .then(({ claims }) => {
const shortUrl = determineShortUrl(claimId, height, claims); const shortId = determineShortClaimId(claimId, height, claims);
resolve(shortUrl); logger.debug('short claim id ===', shortId);
resolve(shortId);
}) })
.catch(error => { .catch(error => {
reject(error); reject(error);

View file

@ -1,6 +1,6 @@
const logger = require('winston'); const logger = require('winston');
const { serveFile, showFile, showFileLite, getShortUrlFromClaimId } = require('../helpers/serveHelpers.js'); const { serveFile, showFile, showFileLite, getShortIdFromClaimId } = require('../helpers/serveHelpers.js');
const { getAssetByChannel, getAssetByShortUrl, getAssetByClaimId, getAssetByName } = require('../controllers/serveController.js'); const { getAssetByChannel, getAssetByShortId, getAssetByClaimId, getAssetByName } = require('../controllers/serveController.js');
const { handleRequestError } = require('../helpers/errorHandlers.js'); const { handleRequestError } = require('../helpers/errorHandlers.js');
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js'); const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
const SERVE = 'SERVE'; const SERVE = 'SERVE';
@ -11,12 +11,12 @@ const SHORTURL = 'SHORTURL';
const CLAIMID = 'CLAIMID'; const CLAIMID = 'CLAIMID';
const NAME = 'NAME'; const NAME = 'NAME';
function getAsset (claimType, channelName, shortUrl, fullClaimId, name) { function getAsset (claimType, channelName, shortId, fullClaimId, name) {
switch (claimType) { switch (claimType) {
case CHANNEL: case CHANNEL:
return getAssetByChannel(channelName, name); return getAssetByChannel(channelName, name);
case SHORTURL: case SHORTURL:
return getAssetByShortUrl(shortUrl, name); return getAssetByShortId(shortId, name);
case CLAIMID: case CLAIMID:
return getAssetByClaimId(fullClaimId, name); return getAssetByClaimId(fullClaimId, name);
case NAME: case NAME:
@ -41,9 +41,9 @@ function serveOrShowAsset (fileInfo, method, headers, originalUrl, ip, res) {
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success'); postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
return fileInfo; return fileInfo;
case SHOW: case SHOW:
return getShortUrlFromClaimId(fileInfo.claimId, fileInfo.height, fileInfo.name) return getShortIdFromClaimId(fileInfo.claimId, fileInfo.height, fileInfo.name)
.then(shortUrl => { .then(shortId => {
fileInfo['shortUrl'] = shortUrl; fileInfo['shortId'] = shortId;
showFile(fileInfo, res); showFile(fileInfo, res);
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success'); postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
return fileInfo; return fileInfo;
@ -62,12 +62,12 @@ 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));
} }
function isValidShortUrl (claimId) { function isValidShortId (claimId) {
return claimId.length === 1; // really it should evaluate the short url itself return claimId.length === 1; // really it should evaluate the short url itself
} }
function isValidShortUrlOrClaimId (input) { function isValidShortIdOrClaimId (input) {
return (isValidClaimId(input) || isValidShortUrl(input)); return (isValidClaimId(input) || isValidShortId(input));
} }
module.exports = (app) => { module.exports = (app) => {
@ -77,7 +77,7 @@ module.exports = (app) => {
let name = params.name; let name = params.name;
let claimType; let claimType;
let channelName = null; let channelName = null;
let shortUrl = null; let shortId = null;
let fullClaimId = null; let fullClaimId = null;
let method; let method;
let extension; let extension;
@ -101,7 +101,7 @@ module.exports = (app) => {
method = SHOW; method = SHOW;
} }
/* patch for backwards compatability with spee.ch/name/claim_id */ /* patch for backwards compatability with spee.ch/name/claim_id */
if (isValidShortUrlOrClaimId(name) && !isValidShortUrlOrClaimId(identifier)) { if (isValidShortIdOrClaimId(name) && !isValidShortIdOrClaimId(identifier)) {
let tempName = name; let tempName = name;
name = identifier; name = identifier;
identifier = tempName; identifier = tempName;
@ -119,8 +119,8 @@ module.exports = (app) => {
logger.debug('full claim id =', fullClaimId); logger.debug('full claim id =', fullClaimId);
claimType = CLAIMID; claimType = CLAIMID;
} else if (identifier.length < 40) { } else if (identifier.length < 40) {
shortUrl = identifier; shortId = identifier;
logger.debug('short url =', shortUrl); logger.debug('short claim id =', shortId);
claimType = SHORTURL; claimType = SHORTURL;
} else { } else {
logger.error('The URL provided could not be parsed'); logger.error('The URL provided could not be parsed');
@ -128,7 +128,7 @@ module.exports = (app) => {
return; return;
}; };
// 1. retrieve the asset and information // 1. retrieve the asset and information
getAsset(claimType, channelName, shortUrl, fullClaimId, name) getAsset(claimType, channelName, shortId, fullClaimId, name)
// 2. serve or show // 2. serve or show
.then(fileInfo => { .then(fileInfo => {
if (!fileInfo) { if (!fileInfo) {

View file

@ -15,11 +15,11 @@
</div> </div>
{{!--short direct link to asset--}} {{!--short direct link to asset--}}
<div class="share-option"> <div class="share-option">
<a href="/{{fileInfo.shortUrl}}/{{fileInfo.name}}{{fileInfo.fileExt}}">Permanent Short Link</a> <a href="/{{fileInfo.shortId}}/{{fileInfo.name}}{{fileInfo.fileExt}}">Permanent Short Link</a>
(most convenient) (most convenient)
<div class="input-error" id="input-error-copy-short-link" hidden="true"></div> <div class="input-error" id="input-error-copy-short-link" hidden="true"></div>
<br/> <br/>
<input type="text" id="short-link" class="link" readonly spellcheck="false" value="https://spee.ch/{{fileInfo.shortUrl}}/{{fileInfo.name}}{{fileInfo.fileExt}}" onclick="select()"/> <input type="text" id="short-link" class="link" readonly spellcheck="false" value="https://spee.ch/{{fileInfo.shortId}}/{{fileInfo.name}}{{fileInfo.fileExt}}" onclick="select()"/>
<button class="copy-button" data-elementtocopy="short-link" onclick="copyToClipboard(event)">copy</button> <button class="copy-button" data-elementtocopy="short-link" onclick="copyToClipboard(event)">copy</button>
</div> </div>
{{!-- html text for embedding asset--}} {{!-- html text for embedding asset--}}