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

Conversation

oorestisime
Copy link
Contributor

I was working on a plugin that uses the createRemoteFileNode and my urls had the following form (comes from instagram api)
https://scontent.xx.fbcdn.net/v/t51.2885-15/42078503_294439751160571_1602896118583132160_n.jpg?_nc_cat=101&oh=e30490a47409051c45dc3daacf616bc0&oe=5C5EA8EB

It was creating the following file nodes

 internal: 
   { contentDigest: '961029972dbcc230aa2d81a006e48a82',
     type: 'File',
     mediaType: 'application/octet-stream',
     description: 'File "https://scontent.xx.fbcdn.net/v/t51.2885-15/40837333_345458009525698_761616599160455168_n.jpg?_nc_cat=105&oh=37800f1dc756dcd31f00482fd1a789eb&oe=5C5216F7"',
     owner: 'gatsby-source-filesystem' },
  sourceInstanceName: '__PROGRAMATTIC__',
  absolutePath: '/home/grok/personal/oasome/.cache/gatsby-source-filesystem/4899d60e50c2d4af9c4da15f2792a9dd.jpg?_nc_cat=105&oh=37800f1dc756dcd31f00482fd1a789eb&oe=5C5216F7',
  relativePath: '.cache/gatsby-source-filesystem/4899d60e50c2d4af9c4da15f2792a9dd.jpg?_nc_cat=105&oh=37800f1dc756dcd31f00482fd1a789eb&oe=5C5216F7',
  extension: 'jpg?_nc_cat=105&oh=37800f1dc756dcd31f00482fd1a789eb&oe=5c5216f7',

And the extension was basically broken.

This lead to weird bugs in gatsby image. I.e querying for localfile like:

localFile {
          childImageSharp {
            fixed(width: 150, height:150, quality: 100) {
              src
              srcSet
              srcWebp
            }
          }
        }

was giving

"localFile": {
     "childImageSharp": null
}

Haven't really checked why with that extension this breaks (maybe something to do with &), i can dig in more if you want, but with that fix everything works again perfectly (plus i think its normal to first parse the url and then the path)

@oorestisime oorestisime changed the title First parse url and then path to retrieve extension [gatsby-source-filesystem] First parse url and then path to retrieve extension Oct 10, 2018
@pieh
Copy link
Contributor

pieh commented Oct 11, 2018

Haven't really checked why with that extension this breaks (maybe something to do with &), i can dig in more if you want, but with that fix everything works again perfectly (plus i think its normal to first parse the url and then the path)

We test extensions in gatsby-transformer-sharp and this weird extension isn't in allowed map: packages/gatsby/src/bootstrap/load-plugins/index.js

@@ -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 = path.parse(Url.parse(url).pathname).ext
Copy link
Contributor

Choose a reason for hiding this comment

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

This is looking good to me! The basic idea is that it's going to make parsing the extension a little more robust, which is great.

How about some tests validating the approach? Also just to sanity check this, I created a Runkit to check this out!

Copy link
Contributor

Choose a reason for hiding this comment

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

let's move path.parse(Url.parse(url).pathname).ext to separate function, that we can export and unit test against couple of urls including https://scontent.xx.fbcdn.net/v/t51.2885-15/42078503_294439751160571_1602896118583132160_n.jpg?_nc_cat=101&oh=e30490a47409051c45dc3daacf616bc0&oe=5C5EA8EB

Copy link
Contributor

Choose a reason for hiding this comment

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

@oorestisime this is actually what I was going to suggest. If we move to a separate file/function, we can add some unit tests sorta like what I've done in the Runkit!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes sounds perfect. added this with latest commit. thanks


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 :)

@pieh
Copy link
Contributor

pieh commented Oct 14, 2018

I think this looks great - one NIT: please rename test file to utils.js as we are testing only utils there right now

@oorestisime
Copy link
Contributor Author

Oups sorry! corrected

Copy link
Contributor

@pieh pieh left a comment

Choose a reason for hiding this comment

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

Nice! Thanks @oorestisime!

@pieh pieh merged commit eb7648c into gatsbyjs:master Oct 15, 2018
jedrichards pushed a commit to jedrichards/gatsby that referenced this pull request Nov 1, 2018
* First parse url and then path to retrieve extension

* Move out extension parsing to unittest

* Prettify test

* Rename test file to match source file
mwfrost pushed a commit to mwfrost/gatsby that referenced this pull request Apr 20, 2023
* First parse url and then path to retrieve extension

* Move out extension parsing to unittest

* Prettify test

* Rename test file to match source file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants