From 657dd62e5bb29d5c259852e192c14c86dfccb289 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Wed, 2 Aug 2017 13:16:39 -0700 Subject: [PATCH] finished basic /shortUrl/claim route conversion --- controllers/serveController.js | 192 +++++++++------------------- helpers/libraries/serveHelpers.js | 169 ++---------------------- routes/serve-routes.js | 79 +++++++----- views/partials/assetInfo.handlebars | 4 +- 4 files changed, 123 insertions(+), 321 deletions(-) diff --git a/controllers/serveController.js b/controllers/serveController.js index 502cca99..6569be11 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -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,37 +40,70 @@ 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 +function getAssetByClaimId (fullClaimId, name) { + return new Promise((resolve, reject) => { + // 1. check locally for claim + checkForLocalAssetByClaimId(fullClaimId, name) + .then(dataValues => { + // 2. if a result was found, resolve the result + 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 + } else { + logger.debug('no local file for this claimId'); + // 3. resolve the claim + lbryApi.resolveUri(`${name}#${fullClaimId}`) + .then(resolveResult => { + // if the claim is free and public, then get it + if (resolveResult.claim && isFreePublicClaim(resolveResult.claim)) { + lbryApi.getClaim(`${name}#${fullClaimId}`) + .then(getResult => { + let fileInfo = formatGetResultsToFileInfo(getResult); + fileInfo['address'] = resolveResult.claim.address; + fileInfo['height'] = resolveResult.claim.height; + resolve(fileInfo); + }) + .catch(error => { + reject(error); + }); + // if not, resolve with no claims } else { - resolve(result); + resolve(null); } }) .catch(error => { reject(error); }); + } + }) + .catch(error => { + reject(error); + }); + }); +} + +module.exports = { + getAssetByChannel (channelName, name) { + return new Promise((resolve, reject) => { + // get the claim id + + // get teh asset by claim Id + }); }, - getAssetByShortUrl (shortUrl, name) { + getAssetByShortUrl: function (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); - } + // get the full claimId + serveHelpers.getClaimIdByShortUrl(shortUrl, name) + // get the asset by the claimId + .then(claimId => { + resolve(getAssetByClaimId(claimId, name)); }) .catch(error => { reject(error); @@ -95,49 +111,7 @@ module.exports = { }); }, getAssetByClaimId (fullClaimId, name) { - return new Promise((resolve, reject) => { - // 1. check locally for claim - checkForLocalAssetByClaimId(fullClaimId, name) - .then(dataValues => { - // 2. if a result was found, resolve the result - 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 - } else { - logger.debug('no local file for this claimId'); - // 3. resolve the claim - lbryApi.resolveUri(`${name}#${fullClaimId}`) - .then(resolveResult => { - // if the claim is free and public, then get it - if (resolveResult.claim && isFreePublicClaim(resolveResult.claim)) { - lbryApi.getClaim(`${name}#${fullClaimId}`) - .then(getResult => { - let fileInfo = formatGetResultsToFileInfo(getResult); - fileInfo['address'] = resolveResult.claim.address; - fileInfo['height'] = resolveResult.claim.height; - resolve(fileInfo); - }) - .catch(error => { - reject(error); - }); - // if not, resolve with no claims - } else { - resolve(null); - } - }) - .catch(error => { - reject(error); - }); - } - }) - .catch(error => { - reject(error); - }); - }); + 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; - }, }; diff --git a/helpers/libraries/serveHelpers.js b/helpers/libraries/serveHelpers.js index 363d777b..9e80a55d 100644 --- a/helpers/libraries/serveHelpers.js +++ b/helpers/libraries/serveHelpers.js @@ -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; }, }; diff --git a/routes/serve-routes.js b/routes/serve-routes.js index db027dce..3eb86af0 100644 --- a/routes/serve-routes.js +++ b/routes/serve-routes.js @@ -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,38 +70,54 @@ module.exports = (app) => { } logger.debug('claim name = ', name); logger.debug('method =', method); - + // 1. retrieve the asset and information getAsset(claimType, channelName, shortUrl, fullClaimId, name) - .then(fileInfo => { - // add file extension to the file info - fileInfo['fileExt'] = fileInfo.fileName.substring(fileInfo.fileName.lastIndexOf('.')); - // test logging - logger.debug(fileInfo); - // serve or show - if (!fileInfo) { - res.status(200).render('noClaims'); - return; - } - switch (method) { - case SERVE: - serveFile(fileInfo, res); - break; - case SHOWLITE: - postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success'); - showFileLite(fileInfo, res); - break; - case SHOW: - postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success'); + // 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 + logger.debug(fileInfo); + // serve or show + if (!fileInfo) { + res.status(200).render('noClaims'); + return; + } + switch (method) { + case SERVE: + serveFile(fileInfo, res); + break; + case SHOWLITE: + postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success'); + showFileLite(fileInfo, res); + 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); - break; - default: - logger.error('I did not recognize that method'); - break; - } - }) - .catch(error => { - logger.error(error); - }); + }) + .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 => { + handleRequestError('serve', originalUrl, ip, error, res); + }); }); // route to serve the winning asset at a claim app.get('/:name', ({ headers, ip, originalUrl, params }, res) => { diff --git a/views/partials/assetInfo.handlebars b/views/partials/assetInfo.handlebars index b1c6c021..6cf78eda 100644 --- a/views/partials/assetInfo.handlebars +++ b/views/partials/assetInfo.handlebars @@ -6,11 +6,11 @@

Links

{{!--short direct link to asset--}}
- Permanent Short Link + Permanent Short Link (most convenient)
- +
{{!-- link to show route for asset--}}