Added tool for generating original filename

- this allows us to have a shared tool for generating _o filenames
This commit is contained in:
Hannah Wolfe 2020-03-25 20:49:55 +00:00
parent 6d6785ae99
commit 46d95ed306
2 changed files with 16 additions and 0 deletions

View File

@ -1,6 +1,7 @@
const Promise = require('bluebird'); const Promise = require('bluebird');
const errors = require('@tryghost/errors'); const errors = require('@tryghost/errors');
const fs = require('fs-extra'); 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. * @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.canTransformFileExtension = canTransformFileExtension;
module.exports.resizeFromPath = makeSafe(unsafeResizeFromPath); module.exports.resizeFromPath = makeSafe(unsafeResizeFromPath);
module.exports.resizeFromBuffer = makeSafe(unsafeResizeFromBuffer); module.exports.resizeFromBuffer = makeSafe(unsafeResizeFromBuffer);

View File

@ -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');
});
});
}); });