gutted the serve route and started rebuilding /identifier/name route

This commit is contained in:
bill bittner 2017-07-31 17:02:39 -07:00
parent d9276d1a35
commit 75d4164af9
5 changed files with 96 additions and 173 deletions

View file

@ -6,6 +6,15 @@ const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js');
const serveHelpers = require('../helpers/libraries/serveHelpers.js'); const serveHelpers = require('../helpers/libraries/serveHelpers.js');
module.exports = { module.exports = {
getAssetByChannel (channelName, name) {
},
getAssetByShortUrl (shortUrl, name) {
},
getAssetByClaimId (fullClaimId, name) {
},
serveClaimByName (claimName) { serveClaimByName (claimName) {
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
// 1. get the top free, public claims // 1. get the top free, public claims

View file

@ -1,6 +1,7 @@
const logger = require('winston'); const logger = require('winston');
const db = require('../../models'); const db = require('../../models');
const lbryApi = require('./lbryApi'); const lbryApi = require('./lbryApi');
const { postToStats } = require('../controllers/statsController.js');
function determineShortUrl (claimId, claimList) { function determineShortUrl (claimId, claimList) {
logger.debug('determining short url based on claim id and claim list'); logger.debug('determining short url based on claim id and claim list');
@ -106,11 +107,8 @@ module.exports = {
// adjust default options as needed // adjust default options as needed
switch (fileType) { switch (fileType) {
case 'image/jpeg': case 'image/jpeg':
break;
case 'image/gif': case 'image/gif':
break;
case 'image/png': case 'image/png':
break;
case 'video/mp4': case 'video/mp4':
break; break;
default: default:
@ -121,6 +119,14 @@ module.exports = {
// send the file // send the file
res.status(200).sendFile(filePath, options); res.status(200).sendFile(filePath, options);
}, },
showFile (originalUrl, ip, fileInfo, res) {
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
res.status(200).render('show', { layout: 'show', fileInfo });
},
showFileLite (originalUrl, ip, fileInfo, res) {
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
res.status(200).render('showLite', { layout: 'show', fileInfo });
},
getClaimIdByShortUrl (name, shortUrl) { getClaimIdByShortUrl (name, shortUrl) {
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
lbryApi.getClaimsList(name) // use the daemon to check for claims list lbryApi.getClaimsList(name) // use the daemon to check for claims list

View file

@ -1,184 +1,92 @@
const { serveClaimByName, serveClaimByClaimId, serveClaimByShortUrl } = require('../controllers/serveController.js'); const { serveFile, showFile, showFileLite } = require('../helpers/libraries/serveHelpers.js');
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js'); const { getAssetByChannel, getAssetByShortUrl, getAssetByClaimId } = require('../controllers/serveController.js');
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const { serveFile } = require('../helpers/libraries/serveHelpers.js');
const { showClaimByName, showClaimByClaimId, showClaimByShortUrl } = require('../controllers/showController.js');
const logger = require('winston'); const logger = require('winston');
function retrieveAssetServeInfo (name, claimId) { const SERVE = 'SERVE';
const deferred = new Promise((resolve, reject) => { const SHOW = 'SHOW';
// if claim id is full 40 chars, retrieve the shortest possible url const SHOWLITE = 'SHOWLITE';
if (claimId.length === 40) { const CHANNEL = 'CHANNEL';
resolve(serveClaimByClaimId(name, claimId)); const SHORTURL = 'SHORTURL';
// if the claim id is shorter than 40, retrieve the full claim id & shortest possible url const CLAIMID = 'CLAIMID';
} else if (claimId.length < 40) {
resolve(serveClaimByShortUrl(name, claimId));
} else {
reject(new Error('That Claim Id is longer than 40 characters.'));
}
});
return deferred;
}
function retrieveAssetShowInfo (name, claimId) { function getAsset (claimType, channelName, shortUrl, fullClaimId, name) {
const deferred = new Promise((resolve, reject) => { switch (claimType) {
// if claim id is full 40 chars, retrieve the shortest possible url case CHANNEL:
if (claimId.length === 40) { return getAssetByChannel(channelName, name);
resolve(showClaimByClaimId(name, claimId)); case SHORTURL:
// if the claim id is shorter than 40, retrieve the full claim id & shortest possible url return getAssetByShortUrl(shortUrl, name);
} else if (claimId.length < 40) { case CLAIMID:
resolve(showClaimByShortUrl(name, claimId)); return getAssetByClaimId(fullClaimId, name);
} else { default:
reject(new Error('That Claim Id is longer than 40 characters.')); return new Error('that claim type was not found');
} }
});
return deferred;
}
function serveClaimByNameWrapper (name, headers, originalUrl, ip, res) {
// begin image-serve processes
serveClaimByName(name)
.then(fileInfo => {
// check to make sure a file was found
if (!fileInfo) {
res.status(307).render('noClaims');
return;
}
// serve the file or the show route
if (headers['accept']) { // note: added b/c some requests errored out due to no accept param in header
const mimetypes = headers['accept'].split(',');
if (mimetypes.includes('text/html')) {
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
res.status(200).render('showLite', { layout: 'show', fileInfo });
} else {
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
serveFile(fileInfo, res);
}
} else {
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
serveFile(fileInfo, res);
}
})
.catch(error => {
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
});
}
function serveAssetByClaimIdWrapper (name, claimId, headers, originalUrl, ip, res) {
retrieveAssetServeInfo(name, claimId)
.then((fileInfo) => {
// check to make sure a file was found
if (!fileInfo) {
res.status(307).render('noClaims');
return;
}
// serve the file or the show route
if (headers['accept']) {
const mimetypes = headers['accept'].split(',');
if (mimetypes.includes('text/html')) {
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
res.status(200).render('showLite', { layout: 'show', fileInfo });
} else {
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
serveFile(fileInfo, res);
}
} else {
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
serveFile(fileInfo, res);
}
})
.catch(error => {
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
});
} }
module.exports = (app) => { module.exports = (app) => {
// route to serve a specific asset // route to serve a specific asset
app.get('/:name/:claim_id', ({ headers, ip, originalUrl, params }, res) => { app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => {
// decide to serve or show const identifier = params.identifier;
const dotIndex = params.claim_id.lastIndexOf('.'); let name = params.name;
if (dotIndex === 0) { // parse identifier for whether it is a channel, short url, or claim_id
logger.error('a file extension with no name was submitted'); let claimType;
errorHandlers.handleRequestError('serve', originalUrl, ip, new Error('no claim id provided'), res); let channelName = null;
// if an image extension was given, serve the image directly let shortUrl = null;
} else if (dotIndex > 0) { let fullClaimId = null;
// google analytics if (identifier.charAt(0) === '@') {
sendGoogleAnalytics('serve', headers, ip, originalUrl); channelName = identifier.substring(1);
// parse params logger.debug('channel name =', channelName);
const fileExtension = params.claim_id.substring(dotIndex); claimType = CHANNEL;
const claimId = params.claim_id.substring(0, dotIndex); } else if (identifier.length < 40) {
logger.debug('file extension is:', fileExtension); fullClaimId = identifier;
// start image-serve processes logger.debug('full claim id =', fullClaimId);
serveAssetByClaimIdWrapper(params.name, claimId, headers, originalUrl, ip, res); claimType = CLAIMID;
// if no image extension was given, show the asset with details } else {
} else if (dotIndex === -1) { shortUrl = identifier;
// google analytics logger.debug('short url =', shortUrl);
sendGoogleAnalytics('show', headers, ip, originalUrl); claimType = SHORTURL;
// for backwards compatability: make sure the client can acept html, if not serve the file. };
// parse the name
let method;
let desiredExtension;
if (name.lastIndexOf('.') === -1) {
method = SERVE;
if (headers['accept'] && headers['accept'].split(',').includes('text/html')) { if (headers['accept'] && headers['accept'].split(',').includes('text/html')) {
// begin image-show processes method = SHOWLITE;
retrieveAssetShowInfo(params.name, params.claim_id)
.then((fileInfo) => {
// check to make sure a file was found
if (!fileInfo) {
res.status(307).render('noClaims');
return;
}
// serve the file or the show route
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
res.status(200).render('show', { layout: 'show', fileInfo });
})
.catch(error => {
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
});
} else {
// start image-serve processes
serveAssetByClaimIdWrapper(params.name, params.claim_id, headers, originalUrl, ip, res);
} }
} else {
method = SHOW;
if (headers['accept'] && !headers['accept'].split(',').includes('text/html')) {
method = SERVE;
}
desiredExtension = name.substring(name.lastIndexOf('.'));
name = name.substring(0, name.lastIndexOf('.'));
logger.debug('file extension =', desiredExtension);
} }
logger.debug('claim name = ', name);
logger.debug('method =', method);
getAsset(claimType, channelName, shortUrl, fullClaimId, name)
.then(result => {
switch (method) {
case SERVE:
serveFile(result, res);
break;
case SHOWLITE:
showFileLite(result, res);
break;
case SHOW:
showFile(result, res);
break;
default:
logger.error('I did not recognize that method');
break;
}
})
.catch(error => {
logger.error(error);
});
}); });
// route to serve the winning asset at a claim // route to serve the winning asset at a claim
app.get('/:name', ({ headers, ip, originalUrl, params }, res) => { app.get('/:name', ({ headers, ip, originalUrl, params }, res) => {
// decide to serve or show
const dotIndex = params.name.lastIndexOf('.');
if (dotIndex === 0) {
logger.error('a file extension with no name was submitted');
errorHandlers.handleRequestError('serve', originalUrl, ip, new Error('no name provided'), res);
// if an image extension was given, serve the image directly
} else if (dotIndex > 0) {
// google analytics
sendGoogleAnalytics('serve', headers, ip, originalUrl);
// parse name param
const fileExtension = params.name.substring(dotIndex);
const claimName = params.name.substring(0, dotIndex);
logger.debug('file extension is:', fileExtension);
// start image-serve processes
serveClaimByNameWrapper(claimName, headers, originalUrl, ip, res);
// if no image extension was given, show the asset with details
} else if (dotIndex === -1) {
// google analytics
sendGoogleAnalytics('show', headers, ip, originalUrl);
// for backwards compatability: make sure the client can receive text/html, or else serve the asset directly
if (headers['accept'] && headers['accept'].split(',').includes('text/html')) {
// begin image-show process
showClaimByName(params.name)
.then(fileInfo => {
// check to make sure a file was found
if (!fileInfo) {
res.status(307).render('noClaims');
return;
}
// serve the show route
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
res.status(200).render('show', { layout: 'show', fileInfo });
})
.catch(error => {
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
});
} else {
// start image serve process
serveClaimByNameWrapper(params.name, headers, originalUrl, ip, res);
}
}
}); });
}; };

View file

@ -89,7 +89,7 @@ db.sequelize
app.use('/media', express.static(hostedContentPath)); app.use('/media', express.static(hostedContentPath));
// require routes & wrap in socket.io // require routes & wrap in socket.io
require('./routes/api-routes.js')(app, hostedContentPath); require('./routes/api-routes.js')(app, hostedContentPath);
require('./routes/show-routes.js')(app); require('./routes/page-routes.js')(app);
require('./routes/serve-routes.js')(app); require('./routes/serve-routes.js')(app);
require('./routes/home-routes.js')(app); require('./routes/home-routes.js')(app);
return require('./routes/sockets-routes.js')(app, siofu, hostedContentPath); return require('./routes/sockets-routes.js')(app, siofu, hostedContentPath);