From de379beec93df2247ea03c202afe91cb48b95cd4 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 18 Jul 2017 16:13:01 -0700 Subject: [PATCH] changed short url serve route to serve earliest-in-time shmatching claim id --- helpers/libraries/serveHelpers.js | 66 +++++++++++++++++++++---------- routes/api-routes.js | 1 - 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/helpers/libraries/serveHelpers.js b/helpers/libraries/serveHelpers.js index dfaa663d..656a0750 100644 --- a/helpers/libraries/serveHelpers.js +++ b/helpers/libraries/serveHelpers.js @@ -1,5 +1,6 @@ const logger = require('winston'); -const db = require('../../models'); +// const db = require('../../models'); +const { getClaimsList } = require('./lbryApi'); module.exports = { serveFile ({ fileName, fileType, filePath }, res) { @@ -32,34 +33,59 @@ module.exports = { validateClaimId (name, claimId) { const deferred = new Promise((resolve, reject) => { logger.debug('claim id length:', claimId.length); - // make sure the claim id is 40 characters + // see if claim id is the full 40 characters if (claimId.length === 40) { - logger.debug('Claim Id length is valid.'); + logger.debug('Full 40-character claim id was provided.'); resolve(claimId); // if the claim id is shorter than 40, check the db for the full claim id - } else if (claimId.length === 1) { - logger.debug(`Finding claim id for "${name}" "${claimId}"`); - db.File - .findOne({ - 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.')); + } else if (claimId.length < 40) { + getClaimsList(name) + .then(({ claims }) => { + const regex = new RegExp(`^${claimId}`); + 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; } - // 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); }); + // 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 { - logger.error('The Claim Id was neither 40 nor 1 character in length'); + logger.error('The Claim Id was larger than 40 characters'); reject(new Error('That Claim Id is not valid.')); } }); diff --git a/routes/api-routes.js b/routes/api-routes.js index ad5ec602..bbaccffb 100644 --- a/routes/api-routes.js +++ b/routes/api-routes.js @@ -78,7 +78,6 @@ module.exports = (app, hostedContentPath) => { res.status(400).send(error.message); return; } - // prepare the publish parameters const fileName = file.name; const filePath = file.path;