-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Benedikt Rötsch
committed
Jun 8, 2018
1 parent
b4f9363
commit 54abaf1
Showing
4 changed files
with
94 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const crypto = require(`crypto`) | ||
const { extname, resolve, basename } = require(`path`) | ||
|
||
const axios = require(`axios`) | ||
const { pathExists, createWriteStream } = require(`fs-extra`) | ||
|
||
module.exports = async function cacheImage (store, image, options) { | ||
const program = store.getState().program | ||
const CACHE_DIR = resolve(`${program.directory}/.cache/contentful/assets/`) | ||
const { file: { url, fileName, details } } = image | ||
const { width, height, maxWidth, maxHeight, resizingBehavior, cropFocus, background } = options | ||
const userWidth = maxWidth || width | ||
const userHeight = maxHeight || height | ||
|
||
const aspectRatio = details.image.height / details.image.width | ||
const resultingWidth = userWidth || 800 | ||
const resultingHeight = userHeight || Math.round(resultingWidth * aspectRatio) | ||
|
||
const params = [`w=${resultingWidth}`, `h=${resultingHeight}`] | ||
if (resizingBehavior) { | ||
params.push(`fit=${resizingBehavior}`) | ||
} | ||
if (cropFocus) { | ||
params.push(`crop=${cropFocus}`) | ||
} | ||
if (background) { | ||
params.push(`bg=${background}`) | ||
} | ||
|
||
const optionsHash = crypto | ||
.createHash(`md5`) | ||
.update(JSON.stringify([ | ||
url, | ||
...params, | ||
])) | ||
.digest(`hex`) | ||
|
||
const base = basename(fileName) | ||
const extension = extname(fileName) | ||
const absolutePath = resolve(CACHE_DIR, `${base}-${optionsHash}${extension}`) | ||
|
||
const alreadyExists = await pathExists(absolutePath) | ||
|
||
if (!alreadyExists) { | ||
const previewUrl = `http:${url}?${params.join(`&`)}` | ||
|
||
const response = await axios({ | ||
method: `get`, | ||
url: previewUrl, | ||
responseType: `stream`, | ||
}) | ||
|
||
await new Promise((resolve, reject) => { | ||
const file = createWriteStream(absolutePath) | ||
response.data.pipe(file) | ||
file.on(`finish`, resolve) | ||
file.on(`error`, reject) | ||
}) | ||
} | ||
|
||
return absolutePath | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters