diff --git a/ghost/image-transform/lib/transform.js b/ghost/image-transform/lib/transform.js index 4cc96802f8..8c5af59ba9 100644 --- a/ghost/image-transform/lib/transform.js +++ b/ghost/image-transform/lib/transform.js @@ -3,6 +3,28 @@ const errors = require('@tryghost/errors'); const fs = require('fs-extra'); const path = require('path'); +/** + * Check if this tool can handle any file transformations as Sharp is an optional dependency + */ +const canTransformFiles = () => { + try { + require('sharp'); + return true; + } catch (err) { + return false; + } +}; + +/** + * Check if this tool can handle a particular extension + * NOTE: .gif optimization is currently not supported by sharp but will be soon + * as there has been support added in underlying libvips library https://github.com/lovell/sharp/issues/1372 + * As for .svg files, sharp only supports conversion to png, and this does not + * play well with animated svg files + * @param {String} ext the extension to check, including the leading dot + */ +const canTransformFileExtension = ext => !['.gif', '.svg', '.svgz', '.ico'].includes(ext); + /** * @NOTE: Sharp cannot operate on the same image path, that's why we have to use in & out paths. * @@ -46,16 +68,6 @@ const unsafeResizeFromBuffer = (originalBuffer, {width, height} = {}) => { }); }; -/** - * Check if this tool can handle a particular extension - * NOTE: .gif optimization is currently not supported by sharp but will be soon - * as there has been support added in underlying libvips library https://github.com/lovell/sharp/issues/1372 - * As for .svg files, sharp only supports conversion to png, and this does not - * play well with animated svg files - * @param {String} ext the extension to check, including the leading dot - */ -const canTransformFileExtension = ext => !['.gif', '.svg', '.svgz', '.ico'].includes(ext); - /** * Internal utility to wrap all transform functions in error handling * Allows us to keep Sharp as an optional dependency @@ -86,7 +98,8 @@ const generateOriginalImageName = (originalPath) => { return path.join(parsedFileName.dir, `${parsedFileName.name}_o${parsedFileName.ext}`); }; -module.exports.generateOriginalImageName = generateOriginalImageName; +module.exports.canTransformFiles = canTransformFiles; module.exports.canTransformFileExtension = canTransformFileExtension; +module.exports.generateOriginalImageName = generateOriginalImageName; module.exports.resizeFromPath = makeSafe(unsafeResizeFromPath); module.exports.resizeFromBuffer = makeSafe(unsafeResizeFromBuffer); diff --git a/ghost/image-transform/test/transform.test.js b/ghost/image-transform/test/transform.test.js index b9909aa00a..cb8dbbc195 100644 --- a/ghost/image-transform/test/transform.test.js +++ b/ghost/image-transform/test/transform.test.js @@ -11,6 +11,17 @@ describe('Transform', function () { testUtils.modules.unmockNonExistentModule(); }); + describe('canTransformFiles', function () { + it('returns true when sharp is available', function () { + transform.canTransformFiles().should.be.true; + }); + + it('returns false when sharp is not available', function () { + testUtils.modules.mockNonExistentModule('sharp', new Error(), true); + transform.canTransformFiles().should.be.false; + }); + }); + describe('canTransformFileExtension', function () { it('returns false for ".gif"', function () { should.equal(