Short urls #143

Merged
bones7242 merged 2 commits from short-urls into master 2017-08-10 22:22:41 +02:00
4 changed files with 59 additions and 46 deletions

View file

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

View file

@ -2,47 +2,59 @@ const logger = require('winston');
const db = require('../models');
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('claimlist starting length:', claimList.length);
// remove this claim from the claim list, if it exists
claimList = claimList.filter(claim => {
return claim.claim_id !== claimId;
});
logger.debug('claim list length:', claimList.length);
// if there are no other claims, return the first letter of the claim id
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 (claimList.length === 0) {
return claimId.substring(0, 1);
// otherwise determine the proper url
// ...otherwise determine the proper short id.
} else {
let i = 0;
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++;
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;
const lastMatch = claimId.substring(0, i);
const matchingClaims = claimListCopy.filter(claim => {
return (claim.claim_id.substring(0, i) === lastMatch);
});
for (let j = 0; j < matchingClaims.length; j++) {
if (matchingClaims[j].height < height) {
return claimId.substring(0, i + 1);
}
// use that longest shared prefix to get only those competing claims
const lastMatchIndex = i - 1;
const lastMatch = claimId.substring(0, lastMatchIndex);
logger.debug('last match index:', lastMatchIndex, 'last match:', lastMatch);
if (lastMatchIndex === 0) { // if no other claims share a prefix, return with first letter.
return claimId.substring(0, 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
.findAll({
where: {
name,
claimId: { $like: `${shortUrl}%` },
claimId: { $like: `${shortId}%` },
},
})
.then(records => {
@ -102,7 +114,7 @@ module.exports = {
const openGraphInfo = createOpenGraphInfo(fileInfo);
res.status(200).render('showLite', { layout: 'show', fileInfo, openGraphInfo });
},
getClaimIdFromShortUrl (shortUrl, name) {
getFullClaimIdFromShortId (shortId, name) {
return new Promise((resolve, reject) => {
logger.debug('getting claim_id from short url');
// use the daemon to check for claims list
@ -111,7 +123,7 @@ module.exports = {
logger.debug('Number of claims from getClaimsList:', claims.length);
// if no claims were found, check locally for possible claims
if (claims.length === 0) {
return checkLocalDbForClaims(name, shortUrl);
return checkLocalDbForClaims(name, shortId);
} else {
return claims;
}
@ -119,7 +131,7 @@ module.exports = {
// handle the claims list
.then(claims => {
logger.debug('Claims ready for filtering');
const regex = new RegExp(`^${shortUrl}`);
const regex = new RegExp(`^${shortId}`);
const filteredClaimsList = claims.filter(claim => {
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) => {
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
lbryApi.getClaimsList(name)
// find the smallest possible unique url for this claim
.then(({ claims }) => {
const shortUrl = determineShortUrl(claimId, height, claims);
resolve(shortUrl);
const shortId = determineShortClaimId(claimId, height, claims);
logger.debug('short claim id ===', shortId);
resolve(shortId);
})
.catch(error => {
reject(error);

View file

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

View file

@ -15,11 +15,11 @@
</div>
{{!--short direct link to asset--}}
<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)
<div class="input-error" id="input-error-copy-short-link" hidden="true"></div>
<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>
</div>
{{!-- html text for embedding asset--}}