2017-06-19 18:37:35 +02:00
|
|
|
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
|
|
|
|
const serveController = require('../controllers/serveController.js');
|
2017-06-19 22:10:06 +02:00
|
|
|
const logger = require('winston');
|
2017-06-29 23:34:23 +02:00
|
|
|
const { postToStats } = require('../helpers/libraries/statsHelpers.js');
|
2017-06-17 02:48:51 +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}`);
|
2017-06-17 22:51:30 +02:00
|
|
|
// set default options
|
|
|
|
const options = {
|
|
|
|
headers: {
|
|
|
|
'X-Content-Type-Options': 'nosniff',
|
2017-06-20 00:25:14 +02:00
|
|
|
'Content-Type' : fileType,
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|
2017-06-17 22:51:30 +02:00
|
|
|
// adjust default options as needed
|
2017-06-18 11:43:57 +02:00
|
|
|
// eslint-disable-next-line camelcase
|
2017-06-20 00:25:14 +02:00
|
|
|
switch (fileType) {
|
2017-06-17 22:51:30 +02:00
|
|
|
case 'image/jpeg':
|
2017-06-19 18:37:35 +02:00
|
|
|
break;
|
2017-06-17 22:51:30 +02:00
|
|
|
case 'image/gif':
|
2017-06-19 18:37:35 +02:00
|
|
|
break;
|
2017-06-17 22:51:30 +02:00
|
|
|
case 'image/png':
|
2017-06-19 18:37:35 +02:00
|
|
|
break;
|
2017-06-17 22:51:30 +02:00
|
|
|
case 'video/mp4':
|
2017-06-19 18:37:35 +02:00
|
|
|
break;
|
2017-06-17 22:51:30 +02:00
|
|
|
default:
|
2017-06-19 22:10:06 +02:00
|
|
|
logger.warn('sending unknown file type as .jpeg');
|
2017-06-19 18:37:35 +02:00
|
|
|
options['headers']['Content-Type'] = 'image/jpeg';
|
|
|
|
break;
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
|
|
|
// send file
|
2017-06-20 00:25:14 +02:00
|
|
|
res.status(200).sendFile(filePath, options);
|
2017-06-14 21:53:55 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 00:53:53 +02:00
|
|
|
module.exports = (app) => {
|
2017-06-17 22:51:30 +02:00
|
|
|
// route to fetch one free public claim
|
2017-06-29 23:48:27 +02:00
|
|
|
app.get('/:name/:claim_id', ({ originalUrl, params, ip }, res) => {
|
2017-06-29 21:35:36 +02:00
|
|
|
logger.verbose(`GET request on ${originalUrl} from ${ip}`);
|
2017-06-17 22:51:30 +02:00
|
|
|
// begin image-serve processes
|
|
|
|
serveController
|
2017-06-28 08:04:47 +02:00
|
|
|
.getClaimByClaimId(params.name, params.claim_id)
|
2017-06-17 22:51:30 +02:00
|
|
|
.then(fileInfo => {
|
2017-06-29 23:48:27 +02:00
|
|
|
postToStats('serve', originalUrl, ip, 'success');
|
2017-06-19 18:37:35 +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-06-17 22:51:30 +02:00
|
|
|
// route to fetch one free public claim
|
2017-06-29 23:48:27 +02:00
|
|
|
app.get('/:name', ({ originalUrl, params, ip }, res) => {
|
2017-06-29 21:35:36 +02:00
|
|
|
logger.verbose(`GET request on ${originalUrl} from ${ip}`);
|
2017-06-17 22:51:30 +02:00
|
|
|
// begin image-serve processes
|
|
|
|
serveController
|
|
|
|
.getClaimByName(params.name)
|
|
|
|
.then(fileInfo => {
|
2017-06-29 23:48:27 +02:00
|
|
|
postToStats('serve', originalUrl, ip, 'success');
|
2017-06-19 18:37:35 +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
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|