Skip to content

Commit

Permalink
determine image extension by mime type
Browse files Browse the repository at this point in the history
  • Loading branch information
axe312ger committed Jun 29, 2021
1 parent 6b81b6a commit 5f941d6
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions packages/gatsby-source-contentful/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ const {
// cache is more likely to go stale than the images (which never go stale)
// Note that the same image might be requested multiple times in the same run

// Supported Image Formats from https://www.contentful.com/developers/docs/references/images-api/#/reference/changing-formats/image-format
const validImageFormats = new Set([`jpg`, `png`, `webp`, `gif`])

if (process.env.GATSBY_REMOTE_CACHE) {
console.warn(
`Note: \`GATSBY_REMOTE_CACHE\` will be removed soon because it has been renamed to \`GATSBY_CONTENTFUL_EXPERIMENTAL_REMOTE_CACHE\``
Expand All @@ -61,10 +58,18 @@ const resolvedBase64Cache = new Map()
// @see https://www.contentful.com/developers/docs/references/images-api/#/reference/resizing-&-cropping/specify-width-&-height
const CONTENTFUL_IMAGE_MAX_SIZE = 4000

const isImage = image =>
[`image/jpeg`, `image/jpg`, `image/png`, `image/webp`, `image/gif`].includes(
image?.file?.contentType
)
// Supported Image Formats from https://www.contentful.com/developers/docs/references/images-api/#/reference/changing-formats/image-format
const validImageFormats = new Set([`jpg`, `png`, `webp`, `gif`])

const mimeTypeExtensions = new Map([
[`image/jpeg`, `jpg`],
[`image/jpg`, `jpg`],
[`image/gif`, `gif`],
[`image/png`, `png`],
[`image/webp`, `webp`],
])

const isImage = image => mimeTypeExtensions.has(image?.file?.contentType)

// Note: this may return a Promise<body>, body (sync), or null
const getBase64Image = (imageProps, reporter) => {
Expand Down Expand Up @@ -684,8 +689,9 @@ exports.extendNodeType = ({ type, cache, reporter }) => {
return null
}

const extension = mimeTypeExtensions.get(contentType)

const url = createUrl(imgUrl, options)
const extension = path.extname(imgUrl)
const absolutePath = await fetchRemoteFile({
url,
name,
Expand Down Expand Up @@ -728,7 +734,7 @@ exports.extendNodeType = ({ type, cache, reporter }) => {
}

const url = createUrl(imgUrl, options)
const extension = path.extname(imgUrl)
const extension = mimeTypeExtensions.get(contentType)

const absolutePath = await fetchRemoteFile({
url,
Expand Down

0 comments on commit 5f941d6

Please sign in to comment.