mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
refs #8221, closes #7688, refs #7558 🙇 Improve meta data publisher logo behaviour This is a follow-up PR for #8285. Reasons: The code changes of #8285 caused error messages when falling back to the default `favicon.ico`, as the `image-size` tool doesn't support `ico` files. This PR takes the logic to decide which logo needs to be listed in our schema into a new fn `blog_logo.js`. There we have now three decisions: 1. If we have a publication **logo**, we'll take that one 2. If we have no publication logo, but an **icon** we'll use this one. 3. If we have none of the above things, we fall back to our default `favicon.ico` Additional, we're hard coding image dimensions for whenever the logo is an `.ico` file and built and extra decision to not call `image-size` when the dimension are already given. I will create another follow-up PR, which checks the extension type for the file and offers it as a util. 🛠 Blog icon util refs #7688 Serve functionality around the blog icon in its own util: - getIconDimensions -> async function that takes the filepath of on ico file and returns its dimensions - isIcoImageType -> returns true if file has `.ico` extension - getIconType -> returns icon-type (`x-icon` or `png`) - getIconUrl -> returns the absolut or relativ URL for the favicon: `[subdirectory or not]favicon.[ico or png]` 📖 Get .ico sizes for meta data & logo improvement refs #7558 refs #8221 Use the new `blogIconUtil` in meta data to fetch the dimensions of `.ico` files. Improvements for `publisher.logo`: We're now returning a hard-coded 'faked' image dimensions value to render an `imageObject` and prevent error our schema (Google structured data). As soon as an image (`.ico` or non-`.ico`) is too large, but - in case of non-`.ico` - a square format, be set the image-dimensions to 60px width and height. This reduces the chances of getting constantly error messages from Googles' webmaster tools. - add getIconPath util
88 lines
3.2 KiB
JavaScript
88 lines
3.2 KiB
JavaScript
var fs = require('fs'),
|
|
path = require('path'),
|
|
crypto = require('crypto'),
|
|
storage = require('../storage'),
|
|
utils = require('../utils'),
|
|
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=' + utils.ONE_DAY_S
|
|
},
|
|
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, utils.url.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(function (err) {
|
|
next(err);
|
|
});
|
|
} else {
|
|
originalExtension = path.extname(filePath).toLowerCase();
|
|
|
|
// CASE: always redirect to .ico for default icon
|
|
if (originalExtension !== requestedExtension) {
|
|
return res.redirect(302, utils.url.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 {
|
|
next();
|
|
}
|
|
};
|
|
}
|
|
|
|
module.exports = serveFavicon;
|