diff --git a/ghost/image-transform/lib/transform.js b/ghost/image-transform/lib/transform.js index b5a547368a..4cc96802f8 100644 --- a/ghost/image-transform/lib/transform.js +++ b/ghost/image-transform/lib/transform.js @@ -1,6 +1,7 @@ const Promise = require('bluebird'); const errors = require('@tryghost/errors'); const fs = require('fs-extra'); +const path = require('path'); /** * @NOTE: Sharp cannot operate on the same image path, that's why we have to use in & out paths. @@ -80,6 +81,12 @@ const makeSafe = fn => (...args) => { }); }; +const generateOriginalImageName = (originalPath) => { + const parsedFileName = path.parse(originalPath); + return path.join(parsedFileName.dir, `${parsedFileName.name}_o${parsedFileName.ext}`); +}; + +module.exports.generateOriginalImageName = generateOriginalImageName; module.exports.canTransformFileExtension = canTransformFileExtension; 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 0e6e0cb93e..b9909aa00a 100644 --- a/ghost/image-transform/test/transform.test.js +++ b/ghost/image-transform/test/transform.test.js @@ -132,4 +132,13 @@ describe('Transform', function () { }); }); }); + + describe('generateOriginalImageName', function () { + it('correctly adds suffix', function () { + transform.generateOriginalImageName('test.jpg').should.eql('test_o.jpg'); + transform.generateOriginalImageName('content/images/test.jpg').should.eql('content/images/test_o.jpg'); + transform.generateOriginalImageName('content/images/test_o.jpg').should.eql('content/images/test_o_o.jpg'); + transform.generateOriginalImageName('content/images/test-1.jpg').should.eql('content/images/test-1_o.jpg'); + }); + }); });