changed short url serve route to serve earliest-in-time shmatching claim id
This commit is contained in:
parent
5a342371b5
commit
de379beec9
2 changed files with 46 additions and 21 deletions
|
@ -1,5 +1,6 @@
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
const db = require('../../models');
|
// const db = require('../../models');
|
||||||
|
const { getClaimsList } = require('./lbryApi');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
serveFile ({ fileName, fileType, filePath }, res) {
|
serveFile ({ fileName, fileType, filePath }, res) {
|
||||||
|
@ -32,34 +33,59 @@ module.exports = {
|
||||||
validateClaimId (name, claimId) {
|
validateClaimId (name, claimId) {
|
||||||
const deferred = new Promise((resolve, reject) => {
|
const deferred = new Promise((resolve, reject) => {
|
||||||
logger.debug('claim id length:', claimId.length);
|
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) {
|
if (claimId.length === 40) {
|
||||||
logger.debug('Claim Id length is valid.');
|
logger.debug('Full 40-character claim id was provided.');
|
||||||
resolve(claimId);
|
resolve(claimId);
|
||||||
// if the claim id is shorter than 40, check the db for the full claim id
|
// if the claim id is shorter than 40, check the db for the full claim id
|
||||||
} else if (claimId.length === 1) {
|
} else if (claimId.length < 40) {
|
||||||
logger.debug(`Finding claim id for "${name}" "${claimId}"`);
|
getClaimsList(name)
|
||||||
db.File
|
.then(({ claims }) => {
|
||||||
.findOne({
|
const regex = new RegExp(`^${claimId}`);
|
||||||
where: {
|
const filteredClaimsList = claims.filter(claim => {
|
||||||
name,
|
return regex.test(claim.claim_id);
|
||||||
claimId: { $like: `${claimId}%` },
|
});
|
||||||
},
|
logger.debug('filtered claims list', filteredClaimsList);
|
||||||
})
|
switch (filteredClaimsList.length) {
|
||||||
.then(file => {
|
case 0:
|
||||||
// if no results were found, throw an error
|
reject(new Error('That is an invalid short url'));
|
||||||
if (!file) {
|
break;
|
||||||
reject(new Error('That is not a valid short URL.'));
|
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 => {
|
.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 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.'));
|
reject(new Error('That Claim Id is not valid.'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -78,7 +78,6 @@ module.exports = (app, hostedContentPath) => {
|
||||||
res.status(400).send(error.message);
|
res.status(400).send(error.message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepare the publish parameters
|
// prepare the publish parameters
|
||||||
const fileName = file.name;
|
const fileName = file.name;
|
||||||
const filePath = file.path;
|
const filePath = file.path;
|
||||||
|
|
Loading…
Reference in a new issue