Skip to content

Commit

Permalink
fix: first parse url and then path to retrieve extension (#9011)
Browse files Browse the repository at this point in the history
* First parse url and then path to retrieve extension

* Move out extension parsing to unittest

* Prettify test

* Rename test file to match source file
  • Loading branch information
oorestisime authored and pieh committed Oct 15, 2018
1 parent ccc3082 commit eb7648c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/gatsby-source-filesystem/src/__tests__/utils.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -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}`

/********************
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions packages/gatsby-source-filesystem/src/utils.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit eb7648c

Please sign in to comment.