diff --git a/packages/gatsby-source-filesystem/src/__tests__/utils.js b/packages/gatsby-source-filesystem/src/__tests__/utils.js new file mode 100644 index 0000000000000..85d08342f0fa0 --- /dev/null +++ b/packages/gatsby-source-filesystem/src/__tests__/utils.js @@ -0,0 +1,15 @@ +const { getRemoteFileExtension } = require(`../utils`) + +describe(`create remote file node`, () => { + it(`can correctly retrieve files extensions`, () => { + [ + [`https://scontent.xx.fbcdn.net/v/t51.2885-15/42078503_294439751160571_1602896118583132160_n.jpg?_nc_cat=101&oh=e30490a47409051c45dc3daacf616bc0&oe=5C5EA8EB`, `.jpg`], + [`https://facebook.com/hello,_world_asdf12341234.jpeg?test=true&other_thing=also-true`, `.jpeg`], + [`https://test.com/asdf.png`, `.png`], + [`./path/to/relative/file.tiff`, `.tiff`], + [`/absolutely/this/will/work.bmp`, `.bmp`], + ].forEach(([url, ext]) => { + expect(getRemoteFileExtension(url)).toBe(ext) + }) + }) +}) diff --git a/packages/gatsby-source-filesystem/src/create-remote-file-node.js b/packages/gatsby-source-filesystem/src/create-remote-file-node.js index 3b65876b1acdc..fc29a6fbdae59 100644 --- a/packages/gatsby-source-filesystem/src/create-remote-file-node.js +++ b/packages/gatsby-source-filesystem/src/create-remote-file-node.js @@ -6,6 +6,7 @@ const { isWebUri } = require(`valid-url`) const Queue = require(`better-queue`) const { createFileNode } = require(`./create-file-node`) +const { getRemoteFileExtension } = require(`./utils`) const cacheId = url => `create-remote-file-node-${url}` /******************** @@ -195,7 +196,7 @@ async function processRemoteNode({ // Create the temp and permanent file names for the url. const digest = createHash(url) - const ext = path.parse(url).ext + const ext = getRemoteFileExtension(url) const tmpFilename = createFilePath(programDir, `tmp-${digest}`, ext) const filename = createFilePath(programDir, digest, ext) diff --git a/packages/gatsby-source-filesystem/src/utils.js b/packages/gatsby-source-filesystem/src/utils.js new file mode 100644 index 0000000000000..a543af4fd96cc --- /dev/null +++ b/packages/gatsby-source-filesystem/src/utils.js @@ -0,0 +1,15 @@ +const path = require(`path`) +const Url = require(`url`) + +/** + * getRemoteFileExtension + * -- + * Parses remote url to retrieve remote file extension + * + * + * @param {String} url + * @return {String} extension + */ +export function getRemoteFileExtension(url) { + return path.parse(Url.parse(url).pathname).ext +}