Short urls 2 #93

Merged
bones7242 merged 8 commits from short-urls-2 into master 2017-07-20 08:16:30 +02:00
2 changed files with 46 additions and 21 deletions
Showing only changes of commit de379beec9 - Show all commits

View file

@ -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.'));
}
});

View file

@ -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;