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 52232679a5eb0..0000000000000 --- a/packages/gatsby-source-contentful/src/cache-image.js +++ /dev/null @@ -1,82 +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) { - 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(`crop=${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`, - }) - .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 11aaa30e412b4..834ee46d6a6bb 100644 --- a/packages/gatsby-source-contentful/src/extend-node-type.js +++ b/packages/gatsby-source-contentful/src/extend-node-type.js @@ -14,14 +14,15 @@ const { GraphQLJSON, GraphQLList, } = require(`gatsby/graphql`) -const qs = require(`qs`) +const { fetchRemoteFile } = require(`gatsby-core-utils`) const { generateImageData } = require(`gatsby-plugin-image`) const { getGatsbyImageFieldConfig, } = require(`gatsby-plugin-image/graphql-utils`) + const { stripIndent } = require(`common-tags`) +const qs = require(`qs`) -const cacheImage = require(`./cache-image`) const downloadWithRetry = require(`./download-with-retry`).default const { ImageFormatType, @@ -664,7 +665,7 @@ const fluidNodeType = ({ name, getTracedSVG }) => { } } -exports.extendNodeType = ({ type, store }) => { +exports.extendNodeType = ({ type, cache }) => { if (type.name !== `ContentfulAsset`) { return {} } @@ -674,15 +675,21 @@ exports.extendNodeType = ({ type, store }) => { 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) - 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: { @@ -698,7 +705,23 @@ exports.extendNodeType = ({ type, store }) => { const getDominantColor = async ({ image, options }) => { try { - const absolutePath = await cacheImage(store, image, options) + 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, + }) const pluginSharp = require(`gatsby-plugin-sharp`) if (!(`getDominantColor` in pluginSharp)) { diff --git a/packages/gatsby-transformer-sqip/src/extend-node-type.js b/packages/gatsby-transformer-sqip/src/extend-node-type.js index 217f408d27f98..25fe975552f6b 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`) +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)