spee.ch/helpers/serveHelpers.js

46 lines
1.4 KiB
JavaScript
Raw Normal View History

const logger = require('winston');
2017-08-23 21:21:15 +02:00
function createOpenGraphInfo ({ fileType, claimId, name, fileName, fileExt }) {
return {
embedUrl : `https://spee.ch/embed/${claimId}/${name}`,
showUrl : `https://spee.ch/${claimId}/${name}`,
source : `https://spee.ch/${claimId}/${name}${fileExt}`,
directFileUrl: `https://spee.ch/media/${fileName}`,
};
}
module.exports = {
serveFile ({ fileName, fileType, filePath }, res) {
logger.verbose(`serving file ${fileName}`);
// set default options
let options = {
headers: {
'X-Content-Type-Options': 'nosniff',
'Content-Type' : fileType,
},
};
// adjust default options as needed
switch (fileType) {
case 'image/jpeg':
case 'image/gif':
case 'image/png':
case 'video/mp4':
break;
default:
logger.warn('sending file with unknown type as .jpeg');
options['headers']['Content-Type'] = 'image/jpeg';
break;
}
// send the file
res.status(200).sendFile(filePath, options);
},
showFile (fileInfo, res) {
2017-08-05 02:41:06 +02:00
const openGraphInfo = createOpenGraphInfo(fileInfo);
res.status(200).render('show', { layout: 'show', fileInfo, openGraphInfo });
},
showFileLite (fileInfo, res) {
2017-08-05 02:41:06 +02:00
const openGraphInfo = createOpenGraphInfo(fileInfo);
2017-10-11 23:16:28 +02:00
res.status(200).render('showLite', { layout: 'showlite', fileInfo, openGraphInfo });
},
};