spee.ch/routes/serve-routes.js

101 lines
3.9 KiB
JavaScript
Raw Normal View History

const logger = require('winston');
const { getClaimByClaimId, getClaimByName } = require('../controllers/serveController.js');
2017-06-30 07:26:29 +02:00
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
2017-06-30 01:10:06 +02:00
2017-06-20 00:25:14 +02:00
function serveFile ({ fileName, fileType, filePath }, res) {
2017-06-20 05:22:23 +02:00
logger.info(`serving file ${fileName}`);
// set default options
2017-07-05 22:51:25 +02:00
let options = {
headers: {
'X-Content-Type-Options': 'nosniff',
2017-06-20 00:25:14 +02:00
'Content-Type' : fileType,
},
};
// adjust default options as needed
2017-06-20 00:25:14 +02:00
switch (fileType) {
case 'image/jpeg':
break;
case 'image/gif':
break;
case 'image/png':
break;
case 'video/mp4':
break;
default:
2017-07-06 03:26:33 +02:00
logger.warn('sending file with unknown type as .jpeg');
options['headers']['Content-Type'] = 'image/jpeg';
break;
}
// send file
2017-06-20 00:25:14 +02:00
res.status(200).sendFile(filePath, options);
}
2017-07-05 18:26:22 +02:00
function sendAnalyticsAndLog (headers, ip, originalUrl) {
2017-06-30 02:10:14 +02:00
// google analytics
2017-07-05 18:26:22 +02:00
sendGoogleAnalytics('serve', headers, ip, originalUrl);
2017-06-30 02:10:14 +02:00
}
module.exports = (app) => {
// 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-05 18:26:22 +02:00
sendAnalyticsAndLog(headers, ip, originalUrl);
// begin image-serve processes
getClaimByClaimId(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
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, fileInfo.fileName, fileInfo.fileType, fileInfo.nsfw, 'success');
res.status(200).render('showLite', { fileInfo });
} else {
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, fileInfo.fileName, fileInfo.fileType, fileInfo.nsfw, 'success');
serveFile(fileInfo, res);
}
} else {
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, fileInfo.fileName, fileInfo.fileType, fileInfo.nsfw, 'success');
serveFile(fileInfo, res);
}
})
.catch(error => {
2017-06-29 23:48:27 +02:00
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
});
});
// route to serve the winning claim
2017-07-05 18:26:22 +02:00
app.get('/:name', ({ headers, ip, originalUrl, params }, res) => {
sendAnalyticsAndLog(headers, ip, originalUrl);
// begin image-serve processes
getClaimByName(params.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, fileInfo.fileName, fileInfo.fileType, fileInfo.nsfw, 'success');
res.status(200).render('showLite', { fileInfo });
} else {
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, fileInfo.fileName, fileInfo.fileType, fileInfo.nsfw, 'success');
serveFile(fileInfo, res);
}
} else {
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, fileInfo.fileName, fileInfo.fileType, fileInfo.nsfw, 'success');
serveFile(fileInfo, res);
}
})
.catch(error => {
2017-06-29 23:48:27 +02:00
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
});
});
};