From 299eddcdadf6f0fb00482d7c8140eb56888b4248 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 25 Jul 2017 01:52:31 -0700 Subject: [PATCH] added fallback to serve image direct on show routes (backwards compatability --- routes/serve-routes.js | 187 +++++++++++++++++-------------- views/partials/topBar.handlebars | 1 - 2 files changed, 105 insertions(+), 83 deletions(-) diff --git a/routes/serve-routes.js b/routes/serve-routes.js index d1bcf2b0..0deb1905 100644 --- a/routes/serve-routes.js +++ b/routes/serve-routes.js @@ -35,6 +35,63 @@ function retrieveAssetShowInfo (name, claimId) { 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) => { // route to serve a specific asset app.get('/:name/:claim_id', ({ headers, ip, originalUrl, params }, res) => { @@ -47,54 +104,37 @@ module.exports = (app) => { } else if (dotIndex > 0) { // google analytics sendGoogleAnalytics('serve', headers, ip, originalUrl); + // parse params const fileExtension = params.claim_id.substring(dotIndex); const claimId = params.claim_id.substring(0, dotIndex); logger.debug('file extension is:', fileExtension); - // begin image-serve processes - retrieveAssetServeInfo(params.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); - }); + // start image-serve processes + serveAssetByClaimIdWrapper(params.name, claimId, 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); - // begin image-show processes - 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); - }); + // for backwards compatability: make sure the client can acept html, if not serve the file. + if (headers['accept'] && headers['accept'].split(',').includes('text/html')) { + // begin image-show processes + 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); + } } }); // route to serve the winning asset at a claim @@ -108,54 +148,37 @@ module.exports = (app) => { } 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); - // begin image-serve processes - serveClaimByName(claimName) - .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); - }); + // 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); - // 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); - }); + // 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); + } } }); }; diff --git a/views/partials/topBar.handlebars b/views/partials/topBar.handlebars index 7ce1ab5e..4b9ee5ea 100644 --- a/views/partials/topBar.handlebars +++ b/views/partials/topBar.handlebars @@ -3,5 +3,4 @@

Spee.ch

(beta) contribute help - \ No newline at end of file