0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Fixed serving of binary public files

no issue

- serving of our public asset images was broken
  - we were reading the binary file in as a string so we could do url transforms, this meant data was lost/corrupted and browsers could not display the served data
  - we were using the wrong mime-type for pngs which meant browsers were triggering downloads rather than displaying images (at least when accessed directly)
- updates uses of `servePublicFile` to have the correct png mimetype
- adjusts `servePublicFile` to treat any mime type starting with `image` as a binary file, passing the file directly through express using `res.sendFile` and skipping the in-memory content caching which is mostly only useful for text files with URL transforms
This commit is contained in:
Kevin Ansfield 2020-02-10 09:51:19 +00:00
parent 42f4518a63
commit 830610d243
2 changed files with 21 additions and 2 deletions

View file

@ -3,6 +3,7 @@ const fs = require('fs-extra');
const path = require('path');
const config = require('../../../config');
const urlUtils = require('../../../lib/url-utils');
const common = require('../../../lib/common');
function createPublicFileMiddleware(file, type, maxAge) {
let content;
@ -16,6 +17,24 @@ function createPublicFileMiddleware(file, type, maxAge) {
res.writeHead(200, content.headers);
return res.end(content.body);
}
// send image files directly and let express handle content-length, etag, etc
if (type.match(/^image/)) {
return res.sendFile(filePath, (err) => {
if (err && err.status === 404) {
// ensure we're triggering basic asset 404 and not a templated 404
return next(new common.errors.NotFoundError({
message: common.i18n.t('errors.errors.imageNotFound'),
code: 'STATIC_FILE_NOT_FOUND',
property: err.path
}));
}
return next(err);
});
}
// modify text files before caching+serving to ensure URL placeholders are transformed
fs.readFile(filePath, (err, buf) => {
if (err) {
return next(err);

View file

@ -118,8 +118,8 @@ module.exports = function setupSiteApp(options = {}) {
siteApp.use(shared.middlewares.servePublicFile('public/ghost.min.css', 'text/css', constants.ONE_YEAR_S));
// Serve images for default templates
siteApp.use(shared.middlewares.servePublicFile('public/404-ghost@2x.png', 'png', constants.ONE_HOUR_S));
siteApp.use(shared.middlewares.servePublicFile('public/404-ghost.png', 'png', constants.ONE_HOUR_S));
siteApp.use(shared.middlewares.servePublicFile('public/404-ghost@2x.png', 'image/png', constants.ONE_HOUR_S));
siteApp.use(shared.middlewares.servePublicFile('public/404-ghost.png', 'image/png', constants.ONE_HOUR_S));
// Serve blog images using the storage adapter
siteApp.use(STATIC_IMAGE_URL_PREFIX, shared.middlewares.image.handleImageSizes, storage.getStorage().serve());