From 8edd2e278eb025c0184dcc3c1e422493dae344be Mon Sep 17 00:00:00 2001 From: bill bittner Date: Thu, 3 Aug 2017 11:14:50 -0700 Subject: [PATCH] extrapolated serve/show --- controllers/serveController.js | 8 +++ routes/serve-routes.js | 115 +++++++++++++++++++++++---------- 2 files changed, 89 insertions(+), 34 deletions(-) diff --git a/controllers/serveController.js b/controllers/serveController.js index ae737b38..de39afb9 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -113,6 +113,14 @@ module.exports = { getAssetByClaimId (fullClaimId, name) { return getAssetByClaimId(fullClaimId, name); }, + 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 diff --git a/routes/serve-routes.js b/routes/serve-routes.js index e36e7d5a..2fba9073 100644 --- a/routes/serve-routes.js +++ b/routes/serve-routes.js @@ -1,6 +1,6 @@ const logger = require('winston'); const { serveFile, showFile, showFileLite, getShortUrlFromClaimId } = require('../helpers/serveHelpers.js'); -const { getAssetByChannel, getAssetByShortUrl, getAssetByClaimId } = require('../controllers/serveController.js'); +const { getAssetByChannel, getAssetByShortUrl, getAssetByClaimId, getAssetByName } = require('../controllers/serveController.js'); const { postToStats } = require('../controllers/statsController.js'); const { handleRequestError } = require('../helpers/errorHandlers.js'); @@ -10,6 +10,7 @@ const SHOWLITE = 'SHOWLITE'; const CHANNEL = 'CHANNEL'; const SHORTURL = 'SHORTURL'; const CLAIMID = 'CLAIMID'; +const NAME = 'NAME'; function getAsset (claimType, channelName, shortUrl, fullClaimId, name) { switch (claimType) { @@ -19,11 +20,46 @@ function getAsset (claimType, channelName, shortUrl, fullClaimId, name) { return getAssetByShortUrl(shortUrl, name); case CLAIMID: return getAssetByClaimId(fullClaimId, name); + case NAME: + return getAssetByName(name); default: return new Error('that claim type was not found'); } } +function serveOrShowAsset (fileInfo, method, originalUrl, ip, res) { + // add file extension to the file info + fileInfo['fileExt'] = fileInfo.fileName.substring(fileInfo.fileName.lastIndexOf('.')); + // test logging + logger.debug(fileInfo); + // serve or show + switch (method) { + case SERVE: + serveFile(fileInfo, res); + postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success'); + return fileInfo; + case SHOWLITE: + showFileLite(fileInfo, res); + postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success'); + return fileInfo; + case SHOW: + return getShortUrlFromClaimId(fileInfo.claimId, fileInfo.name) + .then(shortUrl => { + fileInfo['shortUrl'] = shortUrl; + showFile(fileInfo, res); + postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success'); + return fileInfo; + }) + .catch(error => { + console.log('thowing error...'); + throw error; + }); + default: + logger.error('I did not recognize that method'); + break; + } +} + module.exports = (app) => { // route to serve a specific asset app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => { @@ -74,44 +110,15 @@ module.exports = (app) => { 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 - 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); - }) - .catch(error => { - console.log('thowing error...'); - throw error; - }); - break; - default: - logger.error('I did not recognize that method'); - break; + } else { + return serveOrShowAsset(fileInfo, method, originalUrl, ip, res); } }) // 3. update the database - .then(() => { - logger.debug('update db / create new record'); + .then(({channel, claimId, name}) => { + logger.debug('update db / create new record for:', channel, claimId, name); // 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 }) @@ -122,5 +129,45 @@ module.exports = (app) => { }); // route to serve the winning asset at a claim app.get('/:name', ({ headers, ip, originalUrl, params }, res) => { + // parse name param + let name = params.name; + let method; + let desiredExtension; + if (name.indexOf('.') !== -1) { + method = SERVE; + if (headers['accept'] && headers['accept'].split(',').includes('text/html')) { + method = SHOWLITE; + } + desiredExtension = name.substring(name.indexOf('.')); + name = name.substring(0, name.indexOf('.')); + logger.debug('file extension =', desiredExtension); + } else { + method = SHOW; + if (headers['accept'] && !headers['accept'].split(',').includes('text/html')) { + method = SERVE; + } + } + logger.debug('claim name = ', name); + logger.debug('method =', method); + // 1. retrieve the asset and information + getAsset(NAME, null, null, null, name) + // 2. serve or show + .then(fileInfo => { + if (!fileInfo) { + res.status(200).render('noClaims'); + } else { + return serveOrShowAsset(fileInfo, method, originalUrl, ip, res); + } + }) + // 3. update the database + .then(({channel, claimId, name}) => { + 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 => { + console.log('...error was caught'); + handleRequestError('serve', originalUrl, ip, error, res); + }); }); };