salix/services/mailer/application/router.js

50 lines
1.2 KiB
JavaScript

var express = require('express');
var router = new express.Router();
var fs = require('fs');
var path = require('path');
// Mailer default page
router.get('/', function(request, response) {
response.json({});
});
// Notifications
router.use('/notification', require('./route/notification.js'));
// Serve static images
router.use('/static/:template/:image', function(request, response) {
let imagePath = path.join(__dirname, '/template/', request.params.template, '/image/', request.params.image);
fs.stat(imagePath, function(error) {
if (error)
return response.json({message: 'Image not found'});
let readStream = fs.createReadStream(imagePath);
readStream.on('open', function() {
let contentType = getContentType(imagePath);
if (contentType)
response.setHeader('Content-type', getContentType(imagePath));
readStream.pipe(response);
});
});
});
function getContentType(path) {
let types = {
png: 'image/png',
svg: 'image/svg+xml',
gif: 'image/gif',
jpeg: 'image/jpeg',
jpg: 'image/jpeg'
};
let extension = path.split('.')[1];
return types[extension];
}
module.exports = router;