Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gatsby-source-filesystem] First parse url and then path to retrieve extension #9011

Merged
merged 4 commits into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { getRemoteFileExtension } = require(`../utils`)

describe(`create remote file node`, () => {
it(`can correctly retrieve files extensions`, () => {
expect([
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a cool way to write these tests :)

// note: you may need to remove the dot from extension
[
  [`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)
  })

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty, didnt occur to me like that :)

`https://scontent.xx.fbcdn.net/v/t51.2885-15/42078503_294439751160571_1602896118583132160_n.jpg?_nc_cat=101&oh=e30490a47409051c45dc3daacf616bc0&oe=5C5EA8EB`,
`https://facebook.com/hello,_world_asdf12341234.jpeg?test=true&other_thing=also-true`,
`https://test.com/asdf.png`,
`./path/to/relative/file.tiff`,
`/absolutely/this/will/work.bmp`,
].map(file => getRemoteFileExtension(file)).join(`\n`))
.toBe(`.jpg\n.jpeg\n.png\n.tiff\n.bmp`)
})
})
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
}