spee.ch/helpers/serveHelpers.js

67 lines
2.3 KiB
JavaScript
Raw Normal View History

const logger = require('winston');
2017-11-21 21:53:43 +01:00
const SERVE = 'SERVE';
const SHOW = 'SHOW';
const SHOWLITE = 'SHOWLITE';
// const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
2017-11-21 21:53:43 +01:00
function createOpenGraphInfo ({ claimId, name, fileExt }) {
2017-08-23 21:21:15 +02:00
return {
embedUrl : `https://spee.ch/embed/${claimId}/${name}`,
showUrl : `https://spee.ch/${claimId}/${name}`,
2017-10-19 17:49:02 +02:00
source : `https://spee.ch/${claimId}/${name}.${fileExt}`,
directFileUrl: `https://spee.ch/${claimId}/${name}.${fileExt}`,
2017-08-23 21:21:15 +02:00
};
}
module.exports = {
2017-11-21 21:53:43 +01:00
serveOrShowAsset (method, fileInfo, claimInfo, shortId, res) {
// add file extension to the file info
if (fileInfo.fileName) {
claimInfo['fileExt'] = fileInfo.fileName.substring(fileInfo.fileName.lastIndexOf('.') + 1);
} else {
claimInfo['fileExt'] = null;
}
// serve or show
2017-11-21 21:53:43 +01:00
switch (method) {
case SERVE:
module.exports.serveFile(fileInfo, claimInfo, shortId, res);
return fileInfo;
case SHOWLITE:
module.exports.showFileLite(fileInfo, claimInfo, shortId, res);
return fileInfo;
case SHOW:
module.exports.showFile(fileInfo, claimInfo, shortId, res);
return fileInfo;
default:
logger.error('I did not recognize that method');
break;
}
},
serveFile ({ filePath }, { claimId, name, contentType }, shortId, res) {
logger.verbose(`serving ${name}#${claimId}`);
// set response options
const headerContentType = contentType || 'image/jpeg';
const options = {
headers: {
'X-Content-Type-Options': 'nosniff',
2017-11-21 21:53:43 +01:00
'Content-Type' : headerContentType,
},
};
// send the file
2017-11-21 21:53:43 +01:00
if (filePath) {
res.status(200).sendFile(filePath, options);
} else {
// res.status(307).redirect(`/api/get/${name}/${claimId}`);
res.status(400).json({success: false, message: 'that claim is not hosted locally by Spee<ch yet'});
2017-11-21 21:53:43 +01:00
}
},
2017-11-21 21:53:43 +01:00
showFile (fileInfo, claimInfo, shortId, res) {
const openGraphInfo = createOpenGraphInfo(claimInfo);
res.status(200).render('show', { layout: 'show', claimInfo, shortId, openGraphInfo });
},
2017-11-21 21:53:43 +01:00
showFileLite (fileInfo, claimInfo, shortId, res) {
const openGraphInfo = createOpenGraphInfo(claimInfo);
res.status(200).render('showLite', { layout: 'showlite', claimInfo, shortId, openGraphInfo });
},
};