@bluecadet/launchpad-content
Advanced tools
Comparing version 1.5.0 to 1.6.0
# @bluecadet/launchpad-content | ||
## 1.6.0 | ||
### Minor Changes | ||
- [#62](https://github.com/bluecadet/launchpad/pull/62) [`a4132da`](https://github.com/bluecadet/launchpad/commit/a4132da0187f669ad95251e2f3903229e87d6123) Thanks [@benjaminbojko](https://github.com/benjaminbojko)! - Added caching and blur to image transforms | ||
## 1.5.0 | ||
@@ -4,0 +10,0 @@ |
@@ -127,2 +127,8 @@ /** | ||
/** | ||
* Set to true to always re-generate transformed images, even if cached versions of the original and transformed image already exist. Off by default. | ||
* @type {boolean} | ||
*/ | ||
this.ignoreImageTransformCache = false; | ||
/** | ||
* Set to false if you want to abort a content source from downloading if any of the image transforms fail. Leaving this to true will allow for non-image files to fail quietly. | ||
@@ -129,0 +135,0 @@ * @type {boolean} |
@@ -260,3 +260,10 @@ import fs from 'fs-extra'; | ||
// apply optional transforms | ||
await this._transformImage(tempFilePath, options.imageTransforms, options.ignoreImageTransformErrors); | ||
const ignoreTransformCache = options.ignoreImageTransformCache || !isCached; | ||
await this._transformImage( | ||
tempFilePath, | ||
destPath, | ||
options.imageTransforms, | ||
options.ignoreImageTransformErrors, | ||
ignoreTransformCache | ||
); | ||
@@ -274,11 +281,14 @@ return Promise.resolve(tempFilePath); | ||
* | ||
* @param {string} tempFilePath | ||
* @param {Array<Object>} imageTransforms | ||
* @param {boolean} ignoreErrors | ||
* @param {string} tempFilePath The file path of the image to transform | ||
* @param {string} cachedFilePath The path where the previous image would be cached | ||
* @param {Array<Object>} transforms Array of image transform objects | ||
* @param {boolean} ignoreErrors Silently fails if set to true (helpful if your media folder contains non-image files like 3D models) | ||
* @param {boolean} ignoreCache Set to true to force creating a new image for each transform, even if it already exists under that name. | ||
*/ | ||
async _transformImage(tempFilePath, imageTransforms = [], ignoreErrors = true) { | ||
for (const transform of imageTransforms) { | ||
async _transformImage(tempFilePath, cachedFilePath, transforms = [], ignoreErrors = true, ignoreCache = false) { | ||
for (const transform of transforms) { | ||
try { | ||
const image = sharp(tempFilePath); | ||
const metadata = await image.metadata(); | ||
const operations = []; | ||
let suffix = ''; | ||
@@ -288,3 +298,3 @@ | ||
suffix += `@${transform.scale}x`; | ||
await this._scaleImage(image, metadata, transform.scale); | ||
operations.push(async () => this._scaleImage(image, metadata, transform.scale)); | ||
} | ||
@@ -297,14 +307,31 @@ | ||
} | ||
await this._resizeImage(image, metadata, transform.resize); | ||
operations.push(async () => this._resizeImage(image, metadata, transform.resize)); | ||
} | ||
if (transform.blur) { | ||
suffix += `@blur_${transform.blur}`; | ||
operations.push(async () => this._blurImage(image, metadata, transform.blur)); | ||
} | ||
const outputPath = FileUtils.addFilenameSuffix(tempFilePath, suffix); | ||
await image.toFile(outputPath); | ||
const cachedPath = FileUtils.addFilenameSuffix(cachedFilePath, suffix); | ||
if (!ignoreCache && suffix.length > 0 && fs.existsSync(cachedPath)) { | ||
this.logger.debug(`Using cached trasnformed image ${path.basename(outputPath)}`); | ||
await fs.copyFile(cachedPath, outputPath); | ||
} else { | ||
this.logger.debug(`Saving new transformed image ${path.basename(outputPath)}`); | ||
for (const operation of operations) { | ||
await operation(); | ||
} | ||
await image.toFile(outputPath); | ||
} | ||
} catch (err) { | ||
if (!ignoreErrors) { | ||
// if (!ignoreErrors) { | ||
this.logger.error(`Couldn't transform image ${tempFilePath}`); | ||
this.logger.error(err); | ||
throw err; | ||
} | ||
// } | ||
} | ||
@@ -332,11 +359,20 @@ } | ||
* @param {sharp.Metadata} metadata | ||
* @param {object} resize, obtions object for Sharp resize() | ||
* @param {object} options Options for Sharp resize() | ||
* @returns {Promise<sharp.Sharp>} | ||
*/ | ||
_resizeImage(image, metadata, resize) { | ||
return image.resize( | ||
resize | ||
); | ||
_resizeImage(image, metadata, options) { | ||
return image.resize(options); | ||
} | ||
/** | ||
* | ||
* @param {sharp.Sharp} image | ||
* @param {sharp.Metadata} metadata | ||
* @param {object} amount Amount of blur in px | ||
* @returns {Promise<sharp.Sharp>} | ||
*/ | ||
_blurImage(image, metadata, amount) { | ||
return image.blur(amount); | ||
} | ||
_getRequestHeaders(filePath) { | ||
@@ -343,0 +379,0 @@ if (fs.existsSync(filePath)) { |
{ | ||
"name": "@bluecadet/launchpad-content", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "Content syncing pipeline for various sources", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
96722
26
2731