finished basic /shortUrl/claim route conversion
This commit is contained in:
parent
238f2af63f
commit
657dd62e5b
4 changed files with 123 additions and 321 deletions
|
@ -5,28 +5,11 @@ const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicCla
|
|||
const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js');
|
||||
const serveHelpers = require('../helpers/libraries/serveHelpers.js');
|
||||
|
||||
function checkForLocalAssetByShortUrl (channel, name) {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.File
|
||||
.findOne({where: { name, channel }})
|
||||
.then(result => {
|
||||
if (result) {
|
||||
resolve(result.dataValues);
|
||||
} else {
|
||||
resolve(null);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
// function checkForLocalAssetByShortUrl (shortUrl, name) {
|
||||
// }
|
||||
|
||||
function checkForLocalAssetByChannel (channelName, name) {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
});
|
||||
}
|
||||
// function checkForLocalAssetByChannel (channelName, name) {
|
||||
// }
|
||||
|
||||
function checkForLocalAssetByClaimId (claimId, name) {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
@ -57,44 +40,7 @@ function formatGetResultsToFileInfo ({ name, claim_id, outpoint, file_name, down
|
|||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getAssetByChannel (channelName, name) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// check locally for claim
|
||||
checkForLocalAssetByChannel(channelName, name)
|
||||
.then(result => {
|
||||
// if not found locally, make a get request
|
||||
if (!result) {
|
||||
resolve();
|
||||
// if a result was found, resolve the result
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
},
|
||||
getAssetByShortUrl (shortUrl, name) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// check locally for claim
|
||||
checkForLocalAssetByShortUrl(shortUrl, name)
|
||||
.then(result => {
|
||||
// if not found locally, make a get request
|
||||
if (!result) {
|
||||
resolve();
|
||||
// if a result was found, resolve the result
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
},
|
||||
getAssetByClaimId (fullClaimId, name) {
|
||||
function getAssetByClaimId (fullClaimId, name) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 1. check locally for claim
|
||||
checkForLocalAssetByClaimId(fullClaimId, name)
|
||||
|
@ -103,7 +49,9 @@ module.exports = {
|
|||
if (dataValues) {
|
||||
logger.debug('found a local file for this claimId');
|
||||
// trigger an update check for the file
|
||||
/*
|
||||
serveHelpers.updateFileIfNeeded(dataValues.name, dataValues.claimId, dataValues.outpoint, dataValues.height);
|
||||
*/
|
||||
// resolve promise
|
||||
resolve(dataValues);
|
||||
// 2. if not found locally, make a get request
|
||||
|
@ -138,6 +86,32 @@ module.exports = {
|
|||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getAssetByChannel (channelName, name) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// get the claim id
|
||||
|
||||
// get teh asset by claim Id
|
||||
|
||||
});
|
||||
},
|
||||
getAssetByShortUrl: function (shortUrl, name) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// get the full claimId
|
||||
serveHelpers.getClaimIdByShortUrl(shortUrl, name)
|
||||
// get the asset by the claimId
|
||||
.then(claimId => {
|
||||
resolve(getAssetByClaimId(claimId, name));
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
},
|
||||
getAssetByClaimId (fullClaimId, name) {
|
||||
return getAssetByClaimId(fullClaimId, name);
|
||||
},
|
||||
serveClaimByName (claimName) {
|
||||
const deferred = new Promise((resolve, reject) => {
|
||||
|
@ -180,50 +154,4 @@ module.exports = {
|
|||
});
|
||||
return deferred;
|
||||
},
|
||||
|
||||
serveClaimByShortUrl (name, shortUrl) {
|
||||
const deferred = new Promise((resolve, reject) => {
|
||||
let uri;
|
||||
let claimId;
|
||||
// 1. validate the claim id & retrieve the full claim id if needed
|
||||
serveHelpers
|
||||
.getClaimIdByShortUrl(name, shortUrl)
|
||||
.then(result => {
|
||||
// 2. check locally for the claim
|
||||
uri = `${name}#${result}`;
|
||||
claimId = result;
|
||||
return db.File.findOne({ where: { name, claimId } });
|
||||
})
|
||||
.then(result => {
|
||||
// 3. if a match is found locally, serve that claim
|
||||
if (result) {
|
||||
// return the data for the file to be served
|
||||
resolve(result.dataValues);
|
||||
// update the file, as needed
|
||||
serveHelpers.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 {
|
||||
lbryApi
|
||||
.resolveUri(uri)
|
||||
.then(result => {
|
||||
if (result.claim && isFreePublicClaim(result.claim)) { // check to see if the claim is free & public
|
||||
// get claim and serve
|
||||
serveHelpers
|
||||
.getClaimAndReturnResponse(uri, result.claim.address, result.claim.height)
|
||||
.then(result => {
|
||||
resolve(result.dataValues);
|
||||
})
|
||||
.catch(error => reject(error));
|
||||
} else {
|
||||
logger.debug('Resolve did not return a free, public claim');
|
||||
resolve(null);
|
||||
}
|
||||
})
|
||||
.catch(error => reject(error));
|
||||
}
|
||||
})
|
||||
.catch(error => reject(error));
|
||||
});
|
||||
return deferred;
|
||||
},
|
||||
};
|
||||
|
|
|
@ -59,40 +59,6 @@ function checkLocalDbForClaims (name, shortUrl) {
|
|||
});
|
||||
}
|
||||
|
||||
// function getClaimAndUpdate (uri, address, height) {
|
||||
// // 1. get the claim
|
||||
// lbryApi
|
||||
// .getClaim(uri)
|
||||
// .then(({ name, claim_id, outpoint, file_name, download_path, mime_type, metadata }) => {
|
||||
// logger.debug(' Get returned outpoint: ', outpoint);
|
||||
// // 2. update the entry in db
|
||||
// db.File
|
||||
// .update({
|
||||
// outpoint,
|
||||
// height, // note: height is coming from the 'resolve', not 'get'.
|
||||
// address, // note: address is coming from the 'resolve', not 'get'.
|
||||
// fileName: file_name,
|
||||
// filePath: download_path,
|
||||
// fileType: mime_type,
|
||||
// nsfw : metadata.stream.metadata.nsfw,
|
||||
// }, {
|
||||
// where: {
|
||||
// name,
|
||||
// claimId: claim_id,
|
||||
// },
|
||||
// })
|
||||
// .then(result => {
|
||||
// logger.debug('successfully updated mysql record', result);
|
||||
// })
|
||||
// .catch(error => {
|
||||
// logger.error('sequelize error', error);
|
||||
// });
|
||||
// })
|
||||
// .catch(error => {
|
||||
// logger.error(`error while getting claim for ${uri} >> `, error);
|
||||
// });
|
||||
// }
|
||||
|
||||
module.exports = {
|
||||
serveFile ({ fileName, fileType, filePath }, res) {
|
||||
logger.info(`serving file ${fileName}`);
|
||||
|
@ -122,25 +88,27 @@ module.exports = {
|
|||
res.status(200).render('show', { layout: 'show', fileInfo });
|
||||
},
|
||||
showFileLite (fileInfo, res) {
|
||||
logger.debug('showing file lite');
|
||||
res.status(200).render('showLite', { layout: 'show', fileInfo });
|
||||
},
|
||||
getClaimIdByShortUrl (name, shortUrl) {
|
||||
const deferred = new Promise((resolve, reject) => {
|
||||
lbryApi.getClaimsList(name) // use the daemon to check for claims list
|
||||
.then(({ claims }) => { // if no claims were found, check locally for possible claims
|
||||
getClaimIdByShortUrl (shortUrl, name) {
|
||||
return new Promise((resolve, reject) => {
|
||||
logger.debug('getting claims list from lbrynet');
|
||||
// use the daemon to check for claims list
|
||||
lbryApi.getClaimsList(name)
|
||||
.then(({ claims }) => {
|
||||
logger.debug('Number of claims from getClaimsList:', claims.length);
|
||||
// if no claims were found, check locally for possible claims
|
||||
if (claims.length === 0) {
|
||||
logger.debug('no claims found with getClaimsList');
|
||||
return checkLocalDbForClaims(name, shortUrl);
|
||||
} else {
|
||||
logger.debug('claims were found by getClaimsList');
|
||||
return claims;
|
||||
}
|
||||
})
|
||||
.then(claims => { // handle the claims list
|
||||
logger.debug('Claims ready for filtering:', claims);
|
||||
// handle the claims list
|
||||
.then(claims => {
|
||||
logger.debug('Claims ready for filtering');
|
||||
const regex = new RegExp(`^${shortUrl}`);
|
||||
logger.debug('regex:', regex);
|
||||
const filteredClaimsList = claims.filter(claim => {
|
||||
return regex.test(claim.claim_id);
|
||||
});
|
||||
|
@ -164,10 +132,9 @@ module.exports = {
|
|||
reject(error);
|
||||
});
|
||||
});
|
||||
return deferred;
|
||||
},
|
||||
getShortUrlByClaimId (name, claimId) {
|
||||
const deferred = new Promise((resolve, reject) => {
|
||||
getShortUrlFromClaimId (name, claimId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// get a list of all the claims
|
||||
lbryApi.getClaimsList(name)
|
||||
// find the smallest possible unique url for this claim
|
||||
|
@ -179,115 +146,5 @@ module.exports = {
|
|||
reject(error);
|
||||
});
|
||||
});
|
||||
return deferred;
|
||||
},
|
||||
determineShortUrl (claimId, claimList) {
|
||||
return determineShortUrl(claimId, claimList);
|
||||
},
|
||||
updateFileIfNeeded (name, claimId, localOutpoint, localHeight) {
|
||||
// const uri = `${name}#${claimId}`;
|
||||
// logger.debug(`Initiating resolve to check outpoint for ${uri}`);
|
||||
// // 1. resolve claim
|
||||
// lbryApi
|
||||
// .resolveUri(uri)
|
||||
// .then(result => {
|
||||
// // check to make sure the result is a claim
|
||||
// if (!result.claim) {
|
||||
// logger.debug('resolve did not return a claim');
|
||||
// return;
|
||||
// }
|
||||
// // logger.debug('resolved result:', result);
|
||||
// const resolvedOutpoint = `${result.claim.txid}:${result.claim.nout}`;
|
||||
// const resolvedHeight = result.claim.height;
|
||||
// const resolvedAddress = result.claim.address;
|
||||
// logger.debug('database outpoint:', localOutpoint);
|
||||
// logger.debug('resolved outpoint:', resolvedOutpoint);
|
||||
// // 2. if the outpoint's match, no further work needed
|
||||
// if (localOutpoint === resolvedOutpoint) {
|
||||
// logger.debug('local outpoint matched');
|
||||
// // 2. if the outpoints don't match, check the height
|
||||
// } else if (localHeight > resolvedHeight) {
|
||||
// logger.debug('local height was greater than resolved height');
|
||||
// // 2. get the resolved claim
|
||||
// } else {
|
||||
// logger.debug(`local outpoint did not match for ${uri}. Initiating update.`);
|
||||
// getClaimAndUpdate(uri, resolvedAddress, resolvedHeight);
|
||||
// }
|
||||
// })
|
||||
// .catch(error => {
|
||||
// logger.error(error);
|
||||
// });
|
||||
},
|
||||
getClaimAndHandleResponse (uri, address, height, resolve, reject) {
|
||||
lbryApi
|
||||
.getClaim(uri)
|
||||
.then(({ name, claim_id, outpoint, file_name, download_path, mime_type, metadata }) => {
|
||||
// create entry in the db
|
||||
logger.silly(`creating "${name}" record in File db`);
|
||||
db.File
|
||||
.create({
|
||||
name,
|
||||
claimId : claim_id,
|
||||
address, // note: comes from parent 'resolve,' not this 'get' call
|
||||
outpoint,
|
||||
height, // note: comes from parent 'resolve,' not this 'get' call
|
||||
fileName: file_name,
|
||||
filePath: download_path,
|
||||
fileType: mime_type,
|
||||
nsfw : metadata.stream.metadata.nsfw,
|
||||
})
|
||||
.then(result => {
|
||||
logger.debug('successfully created mysql record');
|
||||
})
|
||||
.catch(error => {
|
||||
logger.error('sequelize create error', error);
|
||||
});
|
||||
// resolve the request
|
||||
resolve({
|
||||
name,
|
||||
claimId : claim_id,
|
||||
fileName: file_name,
|
||||
filePath: download_path,
|
||||
fileType: mime_type,
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
},
|
||||
getClaimAndReturnResponse (uri, address, height) {
|
||||
const deferred = new Promise((resolve, reject) => {
|
||||
lbryApi
|
||||
.getClaim(uri)
|
||||
.then(({ name, claim_id, outpoint, file_name, download_path, mime_type, metadata }) => {
|
||||
// create entry in the db
|
||||
logger.silly(`Creating new File record`);
|
||||
db.File
|
||||
.create({
|
||||
name,
|
||||
claimId : claim_id,
|
||||
address, // note: passed as an arguent, not from this 'get' call
|
||||
outpoint,
|
||||
height, // note: passed as an arguent, not from this 'get' call
|
||||
fileName: file_name,
|
||||
filePath: download_path,
|
||||
fileType: mime_type,
|
||||
nsfw : metadata.stream.metadata.nsfw,
|
||||
})
|
||||
.then(result => {
|
||||
logger.debug('Successfully created File record');
|
||||
resolve(result); // note: result.dataValues ?
|
||||
})
|
||||
.catch(error => {
|
||||
logger.debug('db.File.create error');
|
||||
reject(error);
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
logger.debug('lbryApi.getClaim error');
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
return deferred;
|
||||
},
|
||||
};
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
const logger = require('winston');
|
||||
const { serveFile, showFile, showFileLite } = require('../helpers/libraries/serveHelpers.js');
|
||||
const { serveFile, showFile, showFileLite, getShortUrlFromClaimId } = require('../helpers/libraries/serveHelpers.js');
|
||||
const { getAssetByChannel, getAssetByShortUrl, getAssetByClaimId } = require('../controllers/serveController.js');
|
||||
const { postToStats } = require('../controllers/statsController.js');
|
||||
const { handleRequestError } = require('../helpers/libraries/errorHandlers.js');
|
||||
|
||||
const SERVE = 'SERVE';
|
||||
const SHOW = 'SHOW';
|
||||
|
@ -69,9 +70,11 @@ module.exports = (app) => {
|
|||
}
|
||||
logger.debug('claim name = ', name);
|
||||
logger.debug('method =', method);
|
||||
|
||||
// 1. retrieve the asset and information
|
||||
getAsset(claimType, channelName, shortUrl, fullClaimId, name)
|
||||
// 2. serve or show
|
||||
.then(fileInfo => {
|
||||
logger.debug('asset was retrieved');
|
||||
// add file extension to the file info
|
||||
fileInfo['fileExt'] = fileInfo.fileName.substring(fileInfo.fileName.lastIndexOf('.'));
|
||||
// test logging
|
||||
|
@ -91,15 +94,29 @@ module.exports = (app) => {
|
|||
break;
|
||||
case SHOW:
|
||||
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
||||
getShortUrlFromClaimId(fileInfo.claimId, fileInfo.name)
|
||||
.then(shortUrl => {
|
||||
fileInfo['shortUrl'] = shortUrl;
|
||||
showFile(fileInfo, res);
|
||||
})
|
||||
.catch(error => {
|
||||
console.log('thowing error...');
|
||||
throw error;
|
||||
});
|
||||
break;
|
||||
default:
|
||||
logger.error('I did not recognize that method');
|
||||
break;
|
||||
}
|
||||
})
|
||||
// 3. update the database
|
||||
.then(() => {
|
||||
logger.debug('update db / create new record');
|
||||
// if asset was found locally, update db (resolve the claim to see if a newer version exists and then get && update db if needed)
|
||||
// if asset was retrieved from lbrynet, create db record
|
||||
})
|
||||
.catch(error => {
|
||||
logger.error(error);
|
||||
handleRequestError('serve', originalUrl, ip, error, res);
|
||||
});
|
||||
});
|
||||
// route to serve the winning asset at a claim
|
||||
|
|
|
@ -6,11 +6,11 @@
|
|||
<h2 class="subheader">Links</h2>
|
||||
{{!--short direct link to asset--}}
|
||||
<div class="share-option">
|
||||
<a href="/{{fileInfo.name}}/{{fileInfo.shortUrl}}{{fileInfo.fileExt}}">Permanent Short Link</a>
|
||||
<a href="/{{fileInfo.shortUrl}}/{{fileInfo.name}}{{fileInfo.fileExt}}">Permanent Short Link</a>
|
||||
(most convenient)
|
||||
<div class="input-error" id="input-error-copy-short-link" hidden="true"></div>
|
||||
<br/>
|
||||
<input type="text" id="short-link" class="link" readonly spellcheck="false" value="https://spee.ch/{{fileInfo.name}}/{{fileInfo.shortUrl}}{{fileInfo.fileExt}}" onclick="select()"/>
|
||||
<input type="text" id="short-link" class="link" readonly spellcheck="false" value="https://spee.ch/{{fileInfo.shortUrl}}/{{fileInfo.name}}{{fileInfo.fileExt}}" onclick="select()"/>
|
||||
<button class="copy-button" data-elementtocopy="short-link" onclick="copyToClipboard(event)">copy</button>
|
||||
</div>
|
||||
{{!-- link to show route for asset--}}
|
||||
|
|
Loading…
Add table
Reference in a new issue