got basic serving on long and short claim ids working

This commit is contained in:
bill bittner 2017-07-19 09:11:08 -07:00
parent db1813cd08
commit a6788886fd
5 changed files with 141 additions and 90 deletions

View file

@ -3,7 +3,7 @@ const db = require('../models');
const logger = require('winston');
const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js');
const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js');
const { getClaimIdandShortUrl } = require('../helpers/libraries/serveHelpers.js');
const { getShortUrlByClaimId, getClaimIdByShortUrl } = require('../helpers/libraries/serveHelpers.js');
function updateFileIfNeeded (uri, localOutpoint, localHeight) {
logger.debug(`Initiating resolve to check outpoint for ${uri}`);
@ -189,17 +189,72 @@ module.exports = {
});
return deferred;
},
getClaimByClaimId (name, providedClaimId) {
getClaimByClaimId (name, claimId) {
logger.debug(`Getting claim name: ${name} by claimid: ${claimId}`);
const deferred = new Promise((resolve, reject) => {
// 1. check locally for the claim
const uri = `${name}#${claimId}`;
db.File
.findOne({ where: { name, claimId } })
.then(result => {
// 3. if a match is found locally, serve that claim
if (result) {
logger.debug('local result found');
// return the data for the file to be served
getShortUrlByClaimId(name, claimId)
.then(shortUrl => {
result.dataValues['shortUrl'] = shortUrl;
resolve(result.dataValues);
})
.catch(error => reject(error));
// update the file, as needed
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
} else {
logger.debug('no local result found');
lbryApi
.resolveUri(uri)
.then(result => {
logger.debug('resolve returned successfully');
if (result.claim && isFreePublicClaim(result.claim)) { // check to see if the claim is free & public
// get claim and serve
getClaimAndReturnResponse(uri, result.claim.address, result.claim.height)
.then(result => {
logger.debug('get request returned');
getShortUrlByClaimId(name, claimId)
.then(shortUrl => {
result.dataValues['shortUrl'] = shortUrl;
resolve(result.dataValues);
})
.catch(error => reject(error));
})
.catch(error => reject(error));
} else {
logger.debug('Resolve did not return a free, public claim');
resolve(null, null);
}
})
.catch(error => {
logger.debug('resolve returned an error');
reject(error);
});
}
})
.catch(error => reject(error));
});
return deferred;
},
getClaimByShortUrl (name, shortUrl) {
const deferred = new Promise((resolve, reject) => {
let uri;
let shortUrl;
let claimId;
// 1. validate the claim id & retrieve the full claim id if needed
getClaimIdandShortUrl(name, providedClaimId)
getClaimIdByShortUrl(name, shortUrl)
.then(result => {
// 2. check locally for the claim
uri = `${name}#${result.claimId}`;
shortUrl = result.shortUrl;
return db.File.findOne({ where: { name, claimId: result.claimId } });
uri = `${name}#${result}`;
claimId = result;
return db.File.findOne({ where: { name, claimId } });
})
.then(result => {
// 3. if a match is found locally, serve that claim

View file

@ -91,12 +91,13 @@ module.exports = {
params: { uri },
})
.then(({ data }) => {
logger.debug('resolved successfully', data);
// check for errors
if (data.result[uri].error) {
reject(data.result[uri].error);
return;
} else {
resolve(data.result[uri]);
}
resolve(data.result[uri]);
})
.catch(error => {
reject(error);

View file

@ -18,54 +18,6 @@ function determineShortUrl (claimId, claimList) {
return (shortUrl);
}
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 }) => {
const shortUrl = determineShortUrl(claimId, claims);
resolve(shortUrl);
})
.catch(error => {
reject(error);
});
});
return deferred;
}
module.exports = {
serveFile ({ fileName, fileType, filePath }, res) {
logger.info(`serving file ${fileName}`);
@ -94,36 +46,49 @@ module.exports = {
// send the file
res.status(200).sendFile(filePath, options);
},
getClaimIdandShortUrl (name, url) {
getClaimIdByShortUrl (name, shortUrl) {
const deferred = new Promise((resolve, reject) => {
let claimId;
let shortUrl;
logger.debug('claim url length:', url.length);
// if claim id is full 40 chars, retrieve the shortest possible url
if (url.length === 40) {
getShortUrlByClaimId(name, url)
.then(result => {
claimId = url;
shortUrl = result;
logger.debug('short url', shortUrl);
resolve({ claimId, shortUrl });
})
.catch(error => {
reject(error);
});
// if the claim id is shorter than 40, retrieve the full claim id & shortest possible url
} else if (url.length < 40) {
getClaimIdByShortUrl(name, url)
.then(result => {
claimId = result;
resolve({claimId, shortUrl: url});
})
.catch(error => {
reject(error);
});
} else {
reject(new Error('That Claim Id is not valid.'));
}
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;
},
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 }) => {
const shortUrl = determineShortUrl(claimId, claims);
resolve(shortUrl);
})
.catch(error => {
reject(error);
});
});
return deferred;
},

View file

@ -1,15 +1,30 @@
const { getClaimByClaimId, getClaimByName } = require('../controllers/serveController.js');
const { getClaimByClaimId, getClaimByShortUrl, getClaimByName } = require('../controllers/serveController.js');
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const { serveFile } = require('../helpers/libraries/serveHelpers.js');
function validateClaimId (name, claimId) {
const deferred = new Promise((resolve, reject) => {
// if claim id is full 40 chars, retrieve the shortest possible url
if (claimId.length === 40) {
resolve(getClaimByClaimId(name, claimId));
// if the claim id is shorter than 40, retrieve the full claim id & shortest possible url
} else if (claimId.length < 40) {
resolve(getClaimByShortUrl(name, claimId));
} else {
reject(new Error('That Claim Id is longer than 40 characters.'));
}
});
return deferred;
}
module.exports = (app) => {
// route to serve a specific asset
app.get('/:name/:claim_id', ({ headers, ip, originalUrl, params }, res) => {
// google analytics
sendGoogleAnalytics('serve', headers, ip, originalUrl);
// begin image-serve processes
getClaimByClaimId(params.name, params.claim_id)
validateClaimId(params.name, params.claim_id)
.then((fileInfo) => {
// check to make sure a file was found
if (!fileInfo) {

View file

@ -1,7 +1,22 @@
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const { getClaimByClaimId, getClaimByName, getAllClaims } = require('../controllers/serveController.js');
const { getClaimByClaimId, getClaimByShortUrl, getClaimByName, getAllClaims } = require('../controllers/serveController.js');
const { postToStats, getStatsSummary, getTrendingClaims } = require('../controllers/statsController.js');
function validateClaimId (name, claimId) {
const deferred = new Promise((resolve, reject) => {
// if claim id is full 40 chars, retrieve the shortest possible url
if (claimId.length === 40) {
resolve(getClaimByClaimId(name, claimId));
// if the claim id is shorter than 40, retrieve the full claim id & shortest possible url
} else if (claimId.length < 40) {
resolve(getClaimByShortUrl(name, claimId));
} else {
reject(new Error('That Claim Id is longer than 40 characters.'));
}
});
return deferred;
}
module.exports = (app) => {
// route to show 'about' page for spee.ch
app.get('/about', ({ ip, originalUrl }, res) => {
@ -65,7 +80,7 @@ module.exports = (app) => {
// route to show a specific asset
app.get('/show/:name/:claim_id', ({ ip, originalUrl, params }, res) => {
// begin image-serve processes
getClaimByClaimId(params.name, params.claim_id)
validateClaimId(params.name, params.claim_id)
.then((fileInfo) => {
console.log('SHORT URL:', fileInfo.shortUrl);
// check to make sure a file was found