2017-06-19 22:10:06 +02:00
|
|
|
const logger = require('winston');
|
2017-07-06 22:37:03 +02:00
|
|
|
const { getClaimByClaimId, getClaimByName } = require('../controllers/serveController.js');
|
2017-06-30 07:26:29 +02:00
|
|
|
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
|
2017-07-06 22:37:03 +02:00
|
|
|
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
|
2017-07-17 22:16:11 +02:00
|
|
|
const { serveFile } = require('../helpers/libraries/serveHelpers.js');
|
2017-06-30 02:10:14 +02:00
|
|
|
|
|
|
|
module.exports = (app) => {
|
2017-07-06 22:37:03 +02:00
|
|
|
// route to serve a specific asset
|
2017-07-05 18:39:32 +02:00
|
|
|
app.get('/:name/:claim_id', ({ headers, ip, originalUrl, params }, res) => {
|
2017-07-17 22:16:11 +02:00
|
|
|
// google analytics
|
|
|
|
sendGoogleAnalytics('serve', headers, ip, originalUrl);
|
2017-06-17 22:51:30 +02:00
|
|
|
// begin image-serve processes
|
2017-07-06 22:37:03 +02:00
|
|
|
getClaimByClaimId(params.name, params.claim_id)
|
2017-06-17 22:51:30 +02:00
|
|
|
.then(fileInfo => {
|
2017-07-17 22:16:11 +02:00
|
|
|
logger.debug('file info:', fileInfo);
|
2017-07-01 00:31:23 +02:00
|
|
|
// check to make sure a file was found
|
|
|
|
if (!fileInfo) {
|
|
|
|
res.status(307).render('noClaims');
|
|
|
|
return;
|
|
|
|
}
|
2017-07-05 21:23:55 +02:00
|
|
|
// serve the file or the show route
|
2017-07-07 03:36:42 +02:00
|
|
|
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')) {
|
2017-07-13 00:30:31 +02:00
|
|
|
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
2017-07-07 03:36:42 +02:00
|
|
|
res.status(200).render('showLite', { fileInfo });
|
|
|
|
} else {
|
2017-07-13 00:30:31 +02:00
|
|
|
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
2017-07-07 03:36:42 +02:00
|
|
|
serveFile(fileInfo, res);
|
|
|
|
}
|
2017-07-05 21:23:55 +02:00
|
|
|
} else {
|
2017-07-13 00:30:31 +02:00
|
|
|
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
2017-07-05 21:23:55 +02:00
|
|
|
serveFile(fileInfo, res);
|
|
|
|
}
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-29 23:48:27 +02:00
|
|
|
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
});
|
2017-07-17 22:16:11 +02:00
|
|
|
// route to serve the winning asset at a claim
|
2017-07-05 18:26:22 +02:00
|
|
|
app.get('/:name', ({ headers, ip, originalUrl, params }, res) => {
|
2017-07-17 22:16:11 +02:00
|
|
|
// google analytics
|
|
|
|
sendGoogleAnalytics('serve', headers, ip, originalUrl);
|
2017-06-17 22:51:30 +02:00
|
|
|
// begin image-serve processes
|
2017-07-06 22:37:03 +02:00
|
|
|
getClaimByName(params.name)
|
2017-06-17 22:51:30 +02:00
|
|
|
.then(fileInfo => {
|
2017-07-01 00:31:23 +02:00
|
|
|
// check to make sure a file was found
|
|
|
|
if (!fileInfo) {
|
|
|
|
res.status(307).render('noClaims');
|
|
|
|
return;
|
|
|
|
}
|
2017-07-05 21:23:55 +02:00
|
|
|
// serve the file or the show route
|
2017-07-07 03:36:42 +02:00
|
|
|
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')) {
|
2017-07-13 00:30:31 +02:00
|
|
|
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
2017-07-07 03:36:42 +02:00
|
|
|
res.status(200).render('showLite', { fileInfo });
|
|
|
|
} else {
|
2017-07-13 00:30:31 +02:00
|
|
|
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
2017-07-07 03:36:42 +02:00
|
|
|
serveFile(fileInfo, res);
|
|
|
|
}
|
2017-07-05 21:23:55 +02:00
|
|
|
} else {
|
2017-07-13 00:30:31 +02:00
|
|
|
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
2017-07-05 21:23:55 +02:00
|
|
|
serveFile(fileInfo, res);
|
|
|
|
}
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-29 23:48:27 +02:00
|
|
|
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|