diff --git a/packages/gatsby-source-contentful/src/cache-image.js b/packages/gatsby-source-contentful/src/cache-image.js deleted file mode 100644 index 3386eedb65be6..0000000000000 --- a/packages/gatsby-source-contentful/src/cache-image.js +++ /dev/null @@ -1,85 +0,0 @@ -const crypto = require(`crypto`) -const { resolve, parse } = require(`path`) - -const { pathExists, createWriteStream } = require(`fs-extra`) - -const downloadWithRetry = require(`./download-with-retry`).default - -const inFlightImageCache = new Map() - -module.exports = async function cacheImage(store, image, options, reporter) { - 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 = Math.round(userWidth || 800) - const resultingHeight = Math.round(userHeight || resultingWidth * aspectRatio) - - const params = [`w=${resultingWidth}`, `h=${resultingHeight}`] - if (resizingBehavior) { - params.push(`fit=${resizingBehavior}`) - } - if (cropFocus) { - params.push(`f=${cropFocus}`) - } - if (background) { - params.push(`bg=${background}`) - } - - const optionsHash = crypto - .createHash(`md5`) - .update(JSON.stringify([url, ...params])) - .digest(`hex`) - - const { name, ext } = parse(fileName) - const absolutePath = resolve(CACHE_DIR, `${name}-${optionsHash}${ext}`) - - // Query the filesystem for file existence - const alreadyExists = await pathExists(absolutePath) - // Whether the file exists or not, if we are downloading it then await - const inFlight = inFlightImageCache.get(absolutePath) - if (inFlight) { - await inFlight - } else if (!alreadyExists) { - // File doesn't exist and is not being download yet - const downloadPromise = new Promise((resolve, reject) => { - const previewUrl = `http:${url}?${params.join(`&`)}` - - downloadWithRetry( - { - url: previewUrl, - responseType: `stream`, - }, - reporter - ) - .then(response => { - const file = createWriteStream(absolutePath) - response.data.pipe(file) - file.on(`finish`, resolve) - file.on(`error`, reject) - }) - .catch(reject) - }) - inFlightImageCache.set(absolutePath, downloadPromise) - await downloadPromise - // When the file is downloaded, remove the promise from the cache - inFlightImageCache.delete(absolutePath) - } - - // Now the file should be completely downloaded - return absolutePath -} diff --git a/packages/gatsby-source-contentful/src/extend-node-type.js b/packages/gatsby-source-contentful/src/extend-node-type.js index f7b899356af6b..f857f93e53dc8 100644 --- a/packages/gatsby-source-contentful/src/extend-node-type.js +++ b/packages/gatsby-source-contentful/src/extend-node-type.js @@ -14,10 +14,11 @@ const { GraphQLJSON, GraphQLList, } = require(`gatsby/graphql`) -const qs = require(`qs`) +const { fetchRemoteFile } = require(`gatsby-core-utils`) + const { stripIndent } = require(`common-tags`) +const qs = require(`qs`) -const cacheImage = require(`./cache-image`) const downloadWithRetry = require(`./download-with-retry`).default const { ImageFormatType, @@ -666,7 +667,7 @@ const fluidNodeType = ({ name, getTracedSVG, reporter }) => { } } -exports.extendNodeType = ({ type, store, reporter }) => { +exports.extendNodeType = ({ type, cache, reporter }) => { if (type.name !== `ContentfulAsset`) { return {} } @@ -676,15 +677,21 @@ exports.extendNodeType = ({ type, store, reporter }) => { const { image, options } = args const { - file: { contentType }, + file: { contentType, url: imgUrl, fileName: name }, } = image if (contentType.indexOf(`image/`) !== 0) { return null } - const absolutePath = await cacheImage(store, image, options, reporter) - const extension = path.extname(absolutePath) + const url = createUrl(imgUrl, options) + const extension = path.extname(imgUrl) + const absolutePath = await fetchRemoteFile({ + url, + name, + cache, + ext: extension, + }) return traceSVG({ file: { @@ -712,7 +719,23 @@ exports.extendNodeType = ({ type, store, reporter }) => { } try { - const absolutePath = await cacheImage(store, image, options, reporter) + const { + file: { contentType, url: imgUrl, fileName: name }, + } = image + + if (contentType.indexOf(`image/`) !== 0) { + return null + } + + const url = createUrl(imgUrl, options) + const extension = path.extname(imgUrl) + + const absolutePath = await fetchRemoteFile({ + url, + name, + cache, + ext: extension, + }) if (!(`getDominantColor` in pluginSharp)) { console.error( diff --git a/packages/gatsby-transformer-sqip/src/extend-node-type.js b/packages/gatsby-transformer-sqip/src/extend-node-type.js index 217f408d27f98..e87806ee998b0 100644 --- a/packages/gatsby-transformer-sqip/src/extend-node-type.js +++ b/packages/gatsby-transformer-sqip/src/extend-node-type.js @@ -1,22 +1,23 @@ -const { resolve } = require(`path`) -const md5File = require(`md5-file`) - -const { - DuotoneGradientType, - ImageCropFocusType, -} = require(`gatsby-transformer-sharp/types`) -const { queueImageResizing } = require(`gatsby-plugin-sharp`) +const path = require(`path`) const Debug = require(`debug`) const fs = require(`fs-extra`) +const sharp = require(`sharp`) +const md5File = require(`md5-file`) + const { GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLBoolean, } = require(`gatsby/graphql`) -const sharp = require(`sharp`) -const { ensureDir } = require(`fs-extra`) +const { queueImageResizing } = require(`gatsby-plugin-sharp`) +const { fetchRemoteFile } = require(`gatsby-core-utils`) +const { createUrl } = require(`gatsby-source-contentful/extend-node-type`) +const { + DuotoneGradientType, + ImageCropFocusType, +} = require(`gatsby-transformer-sharp/types`) const generateSqip = require(`./generate-sqip`) @@ -44,11 +45,11 @@ module.exports = async args => { async function sqipSharp({ type, cache, getNodeAndSavePathDependency, store }) { const program = store.getState().program - const cacheDir = resolve( + const cacheDir = path.resolve( `${program.directory}/node_modules/.cache/gatsby-transformer-sqip/` ) - await ensureDir(cacheDir) + await fs.ensureDir(cacheDir) return { sqip: { @@ -140,14 +141,12 @@ async function sqipContentful({ type, cache, store }) { schemes: { ImageResizingBehavior, ImageCropFocusType }, } = require(`gatsby-source-contentful`) - const cacheImage = require(`gatsby-source-contentful/cache-image`) - const program = store.getState().program - const cacheDir = resolve( + const cacheDir = path.resolve( `${program.directory}/node_modules/.cache/gatsby-transformer-sqip/` ) - await ensureDir(cacheDir) + await fs.ensureDir(cacheDir) return { sqip: { @@ -192,7 +191,7 @@ async function sqipContentful({ type, cache, store }) { }, async resolve(asset, fieldArgs, context) { const { - file: { contentType }, + file: { contentType, url: imgUrl }, } = asset if (!contentType.includes(`image/`)) { @@ -223,7 +222,13 @@ async function sqipContentful({ type, cache, store }) { background, } - const absolutePath = await cacheImage(store, asset, options) + const url = createUrl(imgUrl, options) + const extension = path.extname(imgUrl) + const absolutePath = await fetchRemoteFile({ + url, + cache, + ext: extension, + }) const contentDigest = await md5File(absolutePath)