diff --git a/controllers/serveController.js b/controllers/serveController.js index de39afb9..3aaa735c 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -5,12 +5,6 @@ const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicCla const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js'); const serveHelpers = require('../helpers/serveHelpers.js'); -// function checkForLocalAssetByShortUrl (shortUrl, name) { -// } - -// function checkForLocalAssetByChannel (channelName, name) { -// } - function checkForLocalAssetByClaimId (claimId, name) { return new Promise((resolve, reject) => { db.File @@ -115,50 +109,23 @@ module.exports = { }, getAssetByName (name) { return new Promise((resolve, reject) => { - // temporarily throw error - reject(new Error('get by name is not currently supported')); - // get the claim id - // get teh asset by claim Id - }); - }, - serveClaimByName (claimName) { - return new Promise((resolve, reject) => { - // 1. get the top free, public claims - getAllFreePublicClaims(claimName) - .then(freePublicClaimList => { - // check to make sure some claims were found - if (!freePublicClaimList) { - resolve(null); - return; - } - const name = freePublicClaimList[0].name; - const claimId = freePublicClaimList[0].claim_id; - const uri = `${name}#${claimId}`; - const height = freePublicClaimList[0].height; - const address = freePublicClaimList[0].address; - // 2. check to see if the file is available locally - db.File - .findOne({ where: { name, claimId } }) - .then(claim => { - // 3. if a matching record is found locally, serve it - if (claim) { - // serve the file - resolve(claim.dataValues); - // trigger update if needed - serveHelpers.updateFileIfNeeded(uri, claim.dataValues.outpoint, claim.dataValues.height); - // 3. otherwise use daemon to retrieve it - } else { - // get the claim and serve it - serveHelpers.getClaimAndHandleResponse(uri, address, height, resolve, reject); - } - }) - .catch(error => { - reject(error); - }); - }) - .catch(error => { - reject(error); - }); + // 1. get a list of the free public claims + getAllFreePublicClaims(name) + // 2. check locally for the top claim + .then(freePublicClaimList => { + // if no claims were found, return null + if (!freePublicClaimList) { + resolve(null); + return; + } + // parse the result + const claimId = freePublicClaimList[0].claim_id; + // get the asset + resolve(getAssetByClaimId(claimId, name)); + }) + .catch(error => { + reject(error); + }); }); }, }; diff --git a/helpers/lbryApi.js b/helpers/lbryApi.js index e4d529a5..4f2f655f 100644 --- a/helpers/lbryApi.js +++ b/helpers/lbryApi.js @@ -3,7 +3,7 @@ const logger = require('winston'); module.exports = { getWalletList () { - logger.debug('getting wallet list'); + logger.debug('lbryApi >> getting wallet list'); return new Promise((resolve, reject) => { axios .post('http://localhost:5279/lbryapi', { @@ -19,7 +19,7 @@ module.exports = { }); }, publishClaim (publishParams) { - logger.debug(`Publishing claim to "${publishParams.name}"`); + logger.debug(`lbryApi >> Publishing claim to "${publishParams.name}"`); return new Promise((resolve, reject) => { axios .post('http://localhost:5279/lbryapi', { @@ -36,7 +36,7 @@ module.exports = { }); }, getClaim (uri) { - logger.debug(`Getting Claim for "${uri}"`); + logger.debug(`lbryApi >> Getting Claim for "${uri}"`); return new Promise((resolve, reject) => { axios .post('http://localhost:5279/lbryapi', { @@ -57,13 +57,12 @@ module.exports = { resolve(data.result); }) .catch(error => { - logger.debug("axios.post 'get' error"); reject(error); }); }); }, getClaimsList (claimName) { - logger.debug(`Getting Claim List for "${claimName}"`); + logger.debug(`lbryApi >> Getting Claim List for "${claimName}"`); return new Promise((resolve, reject) => { axios .post('http://localhost:5279/lbryapi', { @@ -79,7 +78,7 @@ module.exports = { }); }, resolveUri (uri) { - logger.debug(`Resolving URI for "${uri}"`); + logger.debug(`lbryApi >> Resolving URI for "${uri}"`); return new Promise((resolve, reject) => { axios .post('http://localhost:5279/lbryapi', { @@ -99,7 +98,7 @@ module.exports = { }); }, getDownloadDirectory () { - logger.debug('Retrieving the download directory path from lbry daemon...'); + logger.debug('lbryApi >> Retrieving the download directory path from lbry daemon...'); return new Promise((resolve, reject) => { axios .post('http://localhost:5279/lbryapi', { diff --git a/helpers/serveHelpers.js b/helpers/serveHelpers.js index 8b7bdc7c..98b28bff 100644 --- a/helpers/serveHelpers.js +++ b/helpers/serveHelpers.js @@ -3,8 +3,6 @@ const db = require('../models'); const lbryApi = require('./lbryApi'); function determineShortUrl (claimId, claimList) { - console.log('claimid =', claimId); - console.log('claimlist = ', claimList); logger.debug('determining short url based on claim id and claim list'); const thisClaim = claimList.filter(claim => { // find this claim in the list & store it return claim.claim_id === claimId; diff --git a/routes/serve-routes.js b/routes/serve-routes.js index 2fba9073..a9c581e4 100644 --- a/routes/serve-routes.js +++ b/routes/serve-routes.js @@ -123,7 +123,6 @@ module.exports = (app) => { // if asset was retrieved from lbrynet, create db record }) .catch(error => { - console.log('...error was caught'); handleRequestError('serve', originalUrl, ip, error, res); }); }); @@ -166,7 +165,6 @@ module.exports = (app) => { // if asset was retrieved from lbrynet, create db record }) .catch(error => { - console.log('...error was caught'); handleRequestError('serve', originalUrl, ip, error, res); }); });