added functions for retreiving short url and full claim id

This commit is contained in:
bill bittner 2017-07-18 22:05:34 -07:00
parent 9560585035
commit b6de2affb6
6 changed files with 120 additions and 89 deletions

View file

@ -3,10 +3,10 @@ const db = require('../models');
const logger = require('winston'); const logger = require('winston');
const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js'); const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js');
const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js'); const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js');
const { validateClaimId } = require('../helpers/libraries/serveHelpers.js'); const { getClaimIdandShortUrl } = require('../helpers/libraries/serveHelpers.js');
function updateFileIfNeeded (uri, claimName, claimId, localOutpoint, localHeight) { function updateFileIfNeeded (uri, localOutpoint, localHeight) {
logger.debug(`Initiating resolve to check outpoint for ${claimName}:${claimId}.`); logger.debug(`Initiating resolve to check outpoint for ${uri}`);
// 1. resolve claim // 1. resolve claim
lbryApi lbryApi
.resolveUri(uri) .resolveUri(uri)
@ -117,7 +117,7 @@ function getClaimAndReturnResponse (uri, address, height) {
.getClaim(uri) .getClaim(uri)
.then(({ name, claim_id, outpoint, file_name, download_path, mime_type, metadata }) => { .then(({ name, claim_id, outpoint, file_name, download_path, mime_type, metadata }) => {
// create entry in the db // create entry in the db
logger.silly(`creating new File record`); logger.silly(`Creating new File record`);
db.File db.File
.create({ .create({
name, name,
@ -131,15 +131,16 @@ function getClaimAndReturnResponse (uri, address, height) {
nsfw : metadata.stream.metadata.nsfw, nsfw : metadata.stream.metadata.nsfw,
}) })
.then(result => { .then(result => {
logger.debug('successfully created File record'); logger.debug('Successfully created File record');
resolve(result); // note: result.dataValues ? resolve(result); // note: result.dataValues ?
}) })
.catch(error => { .catch(error => {
logger.error('sequelize create error', error); logger.debug('db.File.create error');
reject(error); reject(error);
}); });
}) })
.catch(error => { .catch(error => {
logger.debug('lbryApi.getClaim error');
reject(error); reject(error);
}); });
}); });
@ -171,7 +172,7 @@ module.exports = {
// serve the file // serve the file
resolve(claim.dataValues); resolve(claim.dataValues);
// trigger update if needed // trigger update if needed
updateFileIfNeeded(uri, name, claimId, claim.dataValues.outpoint, claim.dataValues.height); updateFileIfNeeded(uri, claim.dataValues.outpoint, claim.dataValues.height);
// 3. otherwise use daemon to retrieve it // 3. otherwise use daemon to retrieve it
} else { } else {
// get the claim and serve it // get the claim and serve it
@ -179,7 +180,6 @@ module.exports = {
} }
}) })
.catch(error => { .catch(error => {
logger.error('sequelize error', error);
reject(error); reject(error);
}); });
}) })
@ -189,50 +189,48 @@ module.exports = {
}); });
return deferred; return deferred;
}, },
getClaimByClaimId (name, claimId) { getClaimByClaimId (name, providedClaimId) {
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
let uri; let uri;
let shortUrl;
// 1. validate the claim id & retrieve the full claim id if needed // 1. validate the claim id & retrieve the full claim id if needed
validateClaimId(name, claimId) getClaimIdandShortUrl(name, providedClaimId)
.then(validClaimId => { .then(({ claimId, shortUrl }) => {
// 2. check locally for the claim // 2. check locally for the claim
logger.debug('valid claim id:', validClaimId); uri = `${name}#${claimId}`;
uri = `${name}#${validClaimId}`; return db.File.findOne({ where: { name, claimId } });
return db.File.findOne({ where: { name, claimId: validClaimId } });
}) })
.then(result => { .then(result => {
// 3. if a match is found locally, serve that claim // 3. if a match is found locally, serve that claim
if (result) { if (result) {
logger.debug('Result found in File table');
// return the data for the file to be served // return the data for the file to be served
result.dataValues['shortUrl'] = shortUrl;
resolve(result.dataValues); resolve(result.dataValues);
// update the file, as needed // update the file, as needed
updateFileIfNeeded(uri, name, claimId, result.dataValues.outpoint, result.dataValues.outpoint); updateFileIfNeeded(uri, result.dataValues.outpoint, result.dataValues.outpoint);
// 3. if a match was not found locally, use the daemon to retrieve the claim & return the db data once it is created // 3. if a match was not found locally, use the daemon to retrieve the claim & return the db data once it is created
} else { } else {
logger.debug('No result found in File table');
lbryApi lbryApi
.resolveUri(uri) .resolveUri(uri)
.then(result => { .then(result => {
if (!result.claim) { // check to make sure the result is a claim if (result.claim && isFreePublicClaim(result.claim)) { // check to see if the claim is free & public
logger.debug('resolve did not return a claim');
resolve(null);
}
if (isFreePublicClaim(result.claim)) { // check to see if the claim is free & public
// get claim and serve // get claim and serve
resolve(getClaimAndReturnResponse(uri, result.claim.address, result.claim.height)); getClaimAndReturnResponse(uri, result.claim.address, result.claim.height)
.then(result => {
logger.debug('returned');
result.dataValues['shortUrl'] = shortUrl;
resolve(result.dataValues);
})
.catch(error => reject(error));
} else { } else {
resolve(null); logger.debug('Resolve did not return a free, public claim');
resolve(null, null);
} }
}) })
.catch(error => { .catch(error => reject(error));
reject(error);
});
} }
}) })
.catch(error => { .catch(error => reject(error));
reject(error);
});
}); });
return deferred; return deferred;
}, },

View file

@ -59,6 +59,7 @@ module.exports = {
resolve(data.result); resolve(data.result);
}) })
.catch(error => { .catch(error => {
logger.debug("axios.post 'get' error");
reject(error); reject(error);
}); });
}); });

View file

@ -2,6 +2,65 @@ const logger = require('winston');
// const db = require('../../models'); // const db = require('../../models');
const { getClaimsList } = require('./lbryApi'); const { getClaimsList } = require('./lbryApi');
function getClaimIdByShortUrl (name, shortUrl) {
const deferred = new Promise((resolve, reject) => {
getClaimsList(name)
.then(({ claims }) => {
const regex = new RegExp(`^${shortUrl}`);
logger.debug('regex:', regex);
const filteredClaimsList = claims.filter(claim => {
return regex.test(claim.claim_id);
});
logger.debug('filtered claims list', filteredClaimsList);
switch (filteredClaimsList.length) {
case 0:
reject(new Error('That is an invalid short url'));
break;
case 1:
resolve(filteredClaimsList[0].claim_id);
break;
default:
const sortedClaimsList = filteredClaimsList.sort((a, b) => {
return a.height > b.height;
});
resolve(sortedClaimsList[0].claim_id);
break;
}
})
.catch(error => {
reject(error);
});
});
return deferred;
}
function getShortUrlByClaimId (name, claimId) {
const deferred = new Promise((resolve, reject) => {
// get a list of all the claims
getClaimsList(name)
// find the smallest possible unique url for this claim
.then(({ claims }) => {
let shortUrl = claimId.substring(0, 1);
let i = 1;
claims = claims.filter(claim => { // filter out this exact claim id
return claim.claim_id !== claimId;
});
while (claims.length !== 0) { // filter out matching claims until none or left
shortUrl = claimId.substring(0, i);
claims = claims.filter(claim => {
return (claim.claim_id.substring(0, i) === shortUrl);
});
i++;
}
resolve(shortUrl);
})
.catch(error => {
reject(error);
});
});
return deferred;
}
module.exports = { module.exports = {
serveFile ({ fileName, fileType, filePath }, res) { serveFile ({ fileName, fileType, filePath }, res) {
logger.info(`serving file ${fileName}`); logger.info(`serving file ${fileName}`);
@ -30,63 +89,37 @@ module.exports = {
// send the file // send the file
res.status(200).sendFile(filePath, options); res.status(200).sendFile(filePath, options);
}, },
validateClaimId (name, claimId) { getClaimIdandShortUrl (name, url) {
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
logger.debug('claim id length:', claimId.length); let claimId;
// see if claim id is the full 40 characters let shortUrl;
if (claimId.length === 40) { logger.debug('claim url length:', url.length);
logger.debug('Full 40-character claim id was provided.'); // if claim id is full 40 chars, retrieve the shortest possible url
resolve(claimId); if (url.length === 40) {
// if the claim id is shorter than 40, check the db for the full claim id getShortUrlByClaimId(name, url)
} else if (claimId.length < 40) { .then(result => {
getClaimsList(name) claimId = url;
.then(({ claims }) => { shortUrl = result;
const regex = new RegExp(`^${claimId}`); resolve({ claimId, shortUrl });
logger.debug('regex:', regex); })
const filteredClaimsList = claims.filter(claim => { .catch(error => {
return regex.test(claim.claim_id); reject(error);
}); });
logger.debug('filtered claims list', filteredClaimsList); // if the claim id is shorter than 40, retrieve the full claim id & shortest possible url
switch (filteredClaimsList.length) { } else if (url.length < 40) {
case 0: getClaimIdByShortUrl(name, url)
reject(new Error('That is an invalid short url')); .then(result => {
break; claimId = result;
case 1: return getShortUrlByClaimId(name, claimId);
resolve(filteredClaimsList[0].claim_id); })
break; .then(result => {
default: shortUrl = result;
const sortedClaimsList = filteredClaimsList.sort((a, b) => { resolve({claimId, shortUrl});
return a.height > b.height;
});
resolve(sortedClaimsList[0].claim_id);
break;
}
}) })
.catch(error => { .catch(error => {
reject(error); reject(error);
}); });
// logger.debug(`Finding claim id for "${name}" "${claimId}"`);
// db.File
// .find({
// where: {
// name,
// claimId: { $like: `${claimId}%` },
// },
// })
// .then(file => {
// // if no results were found, throw an error
// if (!file) {
// reject(new Error('That is not a valid short URL.'));
// }
// // if a result was found, resolve with the full claim id
// logger.debug('Full claim id:', file.dataValues.claimId);
// resolve(file.dataValues.claimId);
// })
// .catch(error => {
// reject(error);
// });
} else { } else {
logger.error('The Claim Id was larger than 40 characters');
reject(new Error('That Claim Id is not valid.')); reject(new Error('That Claim Id is not valid.'));
} }
}); });

View file

@ -1,4 +1,3 @@
const logger = require('winston');
const { getClaimByClaimId, getClaimByName } = require('../controllers/serveController.js'); const { getClaimByClaimId, getClaimByName } = require('../controllers/serveController.js');
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js'); const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js');
@ -11,8 +10,7 @@ module.exports = (app) => {
sendGoogleAnalytics('serve', headers, ip, originalUrl); sendGoogleAnalytics('serve', headers, ip, originalUrl);
// begin image-serve processes // begin image-serve processes
getClaimByClaimId(params.name, params.claim_id) getClaimByClaimId(params.name, params.claim_id)
.then(fileInfo => { .then((fileInfo) => {
logger.debug('file info:', fileInfo);
// check to make sure a file was found // check to make sure a file was found
if (!fileInfo) { if (!fileInfo) {
res.status(307).render('noClaims'); res.status(307).render('noClaims');

View file

@ -66,7 +66,8 @@ module.exports = (app) => {
app.get('/show/:name/:claim_id', ({ ip, originalUrl, params }, res) => { app.get('/show/:name/:claim_id', ({ ip, originalUrl, params }, res) => {
// begin image-serve processes // begin image-serve processes
getClaimByClaimId(params.name, params.claim_id) getClaimByClaimId(params.name, params.claim_id)
.then(fileInfo => { .then((fileInfo) => {
console.log('SHORT URL:', fileInfo.shortUrl);
// check to make sure a file was found // check to make sure a file was found
if (!fileInfo) { if (!fileInfo) {
res.status(307).render('noClaims'); res.status(307).render('noClaims');

View file

@ -6,18 +6,18 @@
<h2 class="subheader">Links</h2> <h2 class="subheader">Links</h2>
{{!--short direct link to asset--}} {{!--short direct link to asset--}}
<div class="share-option"> <div class="share-option">
<a href="/{{fileInfo.name}}/{{firstCharacter fileInfo.claimId}}">Direct Link</a> <a href="/{{fileInfo.name}}/{{fileInfo.shortUrl}}">Direct Link</a>
<div class="input-error" id="input-error-copy-direct-link" hidden="true"></div> <div class="input-error" id="input-error-copy-direct-link" hidden="true"></div>
<br/> <br/>
<input type="text" id="direct-link" class="link" readonly spellcheck="false" value="https://spee.ch/{{fileInfo.name}}/{{firstCharacter fileInfo.claimId}}" onclick="select()"/> <input type="text" id="direct-link" class="link" readonly spellcheck="false" value="https://spee.ch/{{fileInfo.name}}/{{fileInfo.shortUrl}}" onclick="select()"/>
<button class="copy-button" data-elementtocopy="direct-link" onclick="copyToClipboard(event)">copy</button> <button class="copy-button" data-elementtocopy="direct-link" onclick="copyToClipboard(event)">copy</button>
</div> </div>
{{!-- link to show route for asset--}} {{!-- link to show route for asset--}}
<div class="share-option"> <div class="share-option">
<a href="/show/{{fileInfo.name}}/{{firstCharacter fileInfo.claimId}}">Details Link</a> <a href="/show/{{fileInfo.name}}/{{fileInfo.shortUrl}}">Details Link</a>
<div class="input-error" id="input-error-copy-show-link" hidden="true"></div> <div class="input-error" id="input-error-copy-show-link" hidden="true"></div>
</br> </br>
<input type="text" id="show-link" class="link" readonly onclick="select()" spellcheck="false" value="https://spee.ch/show/{{fileInfo.name}}/{{firstCharacter fileInfo.claimId}}"/> <input type="text" id="show-link" class="link" readonly onclick="select()" spellcheck="false" value="https://spee.ch/show/{{fileInfo.name}}/{{fileInfo.shortUrl}}"/>
<button class="copy-button" data-elementtocopy="show-link" onclick="copyToClipboard(event)">copy</button> <button class="copy-button" data-elementtocopy="show-link" onclick="copyToClipboard(event)">copy</button>
</div> </div>
{{!-- html text for embedding asset--}} {{!-- html text for embedding asset--}}