diff --git a/src/factories/media.js b/src/factories/media.js index ba25ab6..84816cf 100644 --- a/src/factories/media.js +++ b/src/factories/media.js @@ -4,6 +4,7 @@ const sharp = require('sharp') const mkdirp = require('mkdirp') const crypto = require('crypto') const slugify = require('slugify') +const assign = require('assign-deep') const configStore = require('./../config.js') @@ -142,16 +143,34 @@ class Media { */ _writeFile(file, process, options) { - // resize without options and with options - if (file.sizes && Object.getOwnPropertyNames(options).length === 0 && Object.getOwnPropertyNames(file.sizes).length > 0) { + // if sizes have height or width with no optional parameters then check for merge of options + if (file.sizes && + (((!file.sizes.height || !file.sizes.width) && Object.getOwnPropertyNames(file.sizes).length === 1) || + (file.sizes.height && file.sizes.width && Object.getOwnPropertyNames(file.sizes).length === 2))) { + process.resize(this._mergeOptions(file.sizes, options)) + + // if already options in sizes ignore options + } else if (file.sizes) { process.resize(file.sizes) - } else if (file.sizes && Object.getOwnPropertyNames(options).length > 0 && Object.getOwnPropertyNames(file.sizes).length > 0) { - process.resize(file.sizes, options) } process.toFile(configStore.get('destination') + file.path) } + /** + * if options are exists merge them with sizes + * + * @param {object} sizes + * @param {Object} options + */ + _mergeOptions(sizes, options) { + if (Object.getOwnPropertyNames(options).length > 0) { + sizes = assign(options, sizes) + } + + return sizes + } + /** * generate destination path from hash of file *