Ghost/core/server/web/middleware/serve-favicon.js
kirrg001 4265afe580 Moved utils/url.js to UrlService
refs #9178

- we have to take care that we don't end up in circular dependencies
  - e.g. API requires UrlService and UrlService needs to require the API (for requesting data)
- update the references
- we would like to get rid of the utils folder, this is/was the most complicated change
2017-12-11 20:05:33 +01:00

88 lines
3.3 KiB
JavaScript

var fs = require('fs'),
path = require('path'),
crypto = require('crypto'),
storage = require('../../adapters/storage'),
urlService = require('../../services/url'),
config = require('../../config'),
settingsCache = require('../../settings/cache'),
blogIconUtils = require('../../utils/blog-icon'),
buildContentResponse,
content;
buildContentResponse = function buildContentResponse(ext, buf) {
content = {
headers: {
'Content-Type': 'image/' + ext,
'Content-Length': buf.length,
ETag: '"' + crypto.createHash('md5').update(buf, 'utf8').digest('hex') + '"',
'Cache-Control': 'public, max-age=' + config.get('caching:favicon:maxAge')
},
body: buf
};
return content;
};
// ### serveFavicon Middleware
// Handles requests to favicon.png and favicon.ico
function serveFavicon() {
var iconType,
filePath;
return function serveFavicon(req, res, next) {
if (req.path.match(/^\/favicon\.(ico|png)/i)) {
// CASE: favicon is default
// confusing: if you upload an icon, it's same logic as storing images
// we store as /content/images, because this is the url path images get requested via the browser
// we are using an express route to skip /content/images and the result is a image path
// based on config.getContentPath('images') + req.path
// in this case we don't use path rewrite, that's why we have to make it manually
filePath = blogIconUtils.getIconPath();
var originalExtension = path.extname(filePath).toLowerCase(),
requestedExtension = path.extname(req.path).toLowerCase();
// CASE: custom favicon exists, load it from local file storage
if (settingsCache.get('icon')) {
// depends on the uploaded icon extension
if (originalExtension !== requestedExtension) {
return res.redirect(302, urlService.utils.urlFor({relativeUrl: '/favicon' + originalExtension}));
}
storage.getStorage()
.read({path: filePath})
.then(function readFile(buf) {
iconType = blogIconUtils.getIconType();
content = buildContentResponse(iconType, buf);
res.writeHead(200, content.headers);
res.end(content.body);
})
.catch(next);
} else {
originalExtension = path.extname(filePath).toLowerCase();
// CASE: always redirect to .ico for default icon
if (originalExtension !== requestedExtension) {
return res.redirect(302, urlService.utils.urlFor({relativeUrl: '/favicon.ico'}));
}
fs.readFile(filePath, function readFile(err, buf) {
if (err) {
return next(err);
}
content = buildContentResponse('x-icon', buf);
res.writeHead(200, content.headers);
res.end(content.body);
});
}
} else {
return next();
}
};
}
module.exports = serveFavicon;