got basic serving on long and short claim ids working
This commit is contained in:
parent
db1813cd08
commit
a6788886fd
5 changed files with 141 additions and 90 deletions
|
@ -3,7 +3,7 @@ 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 { getClaimIdandShortUrl } = require('../helpers/libraries/serveHelpers.js');
|
const { getShortUrlByClaimId, getClaimIdByShortUrl } = require('../helpers/libraries/serveHelpers.js');
|
||||||
|
|
||||||
function updateFileIfNeeded (uri, localOutpoint, localHeight) {
|
function updateFileIfNeeded (uri, localOutpoint, localHeight) {
|
||||||
logger.debug(`Initiating resolve to check outpoint for ${uri}`);
|
logger.debug(`Initiating resolve to check outpoint for ${uri}`);
|
||||||
|
@ -189,17 +189,72 @@ module.exports = {
|
||||||
});
|
});
|
||||||
return deferred;
|
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) => {
|
const deferred = new Promise((resolve, reject) => {
|
||||||
let uri;
|
let uri;
|
||||||
let shortUrl;
|
let claimId;
|
||||||
// 1. validate the claim id & retrieve the full claim id if needed
|
// 1. validate the claim id & retrieve the full claim id if needed
|
||||||
getClaimIdandShortUrl(name, providedClaimId)
|
getClaimIdByShortUrl(name, shortUrl)
|
||||||
.then(result => {
|
.then(result => {
|
||||||
// 2. check locally for the claim
|
// 2. check locally for the claim
|
||||||
uri = `${name}#${result.claimId}`;
|
uri = `${name}#${result}`;
|
||||||
shortUrl = result.shortUrl;
|
claimId = result;
|
||||||
return db.File.findOne({ where: { name, claimId: result.claimId } });
|
return db.File.findOne({ where: { name, claimId } });
|
||||||
})
|
})
|
||||||
.then(result => {
|
.then(result => {
|
||||||
// 3. if a match is found locally, serve that claim
|
// 3. if a match is found locally, serve that claim
|
||||||
|
|
|
@ -91,12 +91,13 @@ module.exports = {
|
||||||
params: { uri },
|
params: { uri },
|
||||||
})
|
})
|
||||||
.then(({ data }) => {
|
.then(({ data }) => {
|
||||||
|
logger.debug('resolved successfully', data);
|
||||||
// check for errors
|
// check for errors
|
||||||
if (data.result[uri].error) {
|
if (data.result[uri].error) {
|
||||||
reject(data.result[uri].error);
|
reject(data.result[uri].error);
|
||||||
return;
|
} else {
|
||||||
|
resolve(data.result[uri]);
|
||||||
}
|
}
|
||||||
resolve(data.result[uri]);
|
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
reject(error);
|
reject(error);
|
||||||
|
|
|
@ -18,54 +18,6 @@ function determineShortUrl (claimId, claimList) {
|
||||||
return (shortUrl);
|
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 = {
|
module.exports = {
|
||||||
serveFile ({ fileName, fileType, filePath }, res) {
|
serveFile ({ fileName, fileType, filePath }, res) {
|
||||||
logger.info(`serving file ${fileName}`);
|
logger.info(`serving file ${fileName}`);
|
||||||
|
@ -94,36 +46,49 @@ module.exports = {
|
||||||
// send the file
|
// send the file
|
||||||
res.status(200).sendFile(filePath, options);
|
res.status(200).sendFile(filePath, options);
|
||||||
},
|
},
|
||||||
getClaimIdandShortUrl (name, url) {
|
getClaimIdByShortUrl (name, shortUrl) {
|
||||||
const deferred = new Promise((resolve, reject) => {
|
const deferred = new Promise((resolve, reject) => {
|
||||||
let claimId;
|
getClaimsList(name)
|
||||||
let shortUrl;
|
.then(({ claims }) => {
|
||||||
logger.debug('claim url length:', url.length);
|
const regex = new RegExp(`^${shortUrl}`);
|
||||||
// if claim id is full 40 chars, retrieve the shortest possible url
|
logger.debug('regex:', regex);
|
||||||
if (url.length === 40) {
|
const filteredClaimsList = claims.filter(claim => {
|
||||||
getShortUrlByClaimId(name, url)
|
return regex.test(claim.claim_id);
|
||||||
.then(result => {
|
});
|
||||||
claimId = url;
|
logger.debug('filtered claims list', filteredClaimsList);
|
||||||
shortUrl = result;
|
switch (filteredClaimsList.length) {
|
||||||
logger.debug('short url', shortUrl);
|
case 0:
|
||||||
resolve({ claimId, shortUrl });
|
reject(new Error('That is an invalid short url'));
|
||||||
})
|
break;
|
||||||
.catch(error => {
|
case 1:
|
||||||
reject(error);
|
resolve(filteredClaimsList[0].claim_id);
|
||||||
});
|
break;
|
||||||
// if the claim id is shorter than 40, retrieve the full claim id & shortest possible url
|
default:
|
||||||
} else if (url.length < 40) {
|
const sortedClaimsList = filteredClaimsList.sort((a, b) => {
|
||||||
getClaimIdByShortUrl(name, url)
|
return a.height > b.height;
|
||||||
.then(result => {
|
});
|
||||||
claimId = result;
|
resolve(sortedClaimsList[0].claim_id);
|
||||||
resolve({claimId, shortUrl: url});
|
break;
|
||||||
})
|
}
|
||||||
.catch(error => {
|
})
|
||||||
reject(error);
|
.catch(error => {
|
||||||
});
|
reject(error);
|
||||||
} else {
|
});
|
||||||
reject(new Error('That Claim Id is not valid.'));
|
});
|
||||||
}
|
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;
|
return deferred;
|
||||||
},
|
},
|
||||||
|
|
|
@ -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 { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
|
||||||
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
|
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
|
||||||
const { serveFile } = require('../helpers/libraries/serveHelpers.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) => {
|
module.exports = (app) => {
|
||||||
// route to serve a specific asset
|
// route to serve a specific asset
|
||||||
app.get('/:name/:claim_id', ({ headers, ip, originalUrl, params }, res) => {
|
app.get('/:name/:claim_id', ({ headers, ip, originalUrl, params }, res) => {
|
||||||
// google analytics
|
// google analytics
|
||||||
sendGoogleAnalytics('serve', headers, ip, originalUrl);
|
sendGoogleAnalytics('serve', headers, ip, originalUrl);
|
||||||
// begin image-serve processes
|
// begin image-serve processes
|
||||||
getClaimByClaimId(params.name, params.claim_id)
|
validateClaimId(params.name, params.claim_id)
|
||||||
.then((fileInfo) => {
|
.then((fileInfo) => {
|
||||||
// check to make sure a file was found
|
// check to make sure a file was found
|
||||||
if (!fileInfo) {
|
if (!fileInfo) {
|
||||||
|
|
|
@ -1,7 +1,22 @@
|
||||||
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
|
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');
|
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) => {
|
module.exports = (app) => {
|
||||||
// route to show 'about' page for spee.ch
|
// route to show 'about' page for spee.ch
|
||||||
app.get('/about', ({ ip, originalUrl }, res) => {
|
app.get('/about', ({ ip, originalUrl }, res) => {
|
||||||
|
@ -65,7 +80,7 @@ module.exports = (app) => {
|
||||||
// route to show a specific asset
|
// route to show a specific asset
|
||||||
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)
|
validateClaimId(params.name, params.claim_id)
|
||||||
.then((fileInfo) => {
|
.then((fileInfo) => {
|
||||||
console.log('SHORT URL:', fileInfo.shortUrl);
|
console.log('SHORT URL:', fileInfo.shortUrl);
|
||||||
// check to make sure a file was found
|
// check to make sure a file was found
|
||||||
|
|
Loading…
Reference in a new issue