Skip to content

Commit eb7648c

Browse files
oorestisimepieh
authored andcommitted
fix: first parse url and then path to retrieve extension (#9011)
* First parse url and then path to retrieve extension * Move out extension parsing to unittest * Prettify test * Rename test file to match source file
1 parent ccc3082 commit eb7648c

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { getRemoteFileExtension } = require(`../utils`)
2+
3+
describe(`create remote file node`, () => {
4+
it(`can correctly retrieve files extensions`, () => {
5+
[
6+
[`https://scontent.xx.fbcdn.net/v/t51.2885-15/42078503_294439751160571_1602896118583132160_n.jpg?_nc_cat=101&oh=e30490a47409051c45dc3daacf616bc0&oe=5C5EA8EB`, `.jpg`],
7+
[`https://facebook.com/hello,_world_asdf12341234.jpeg?test=true&other_thing=also-true`, `.jpeg`],
8+
[`https://test.com/asdf.png`, `.png`],
9+
[`./path/to/relative/file.tiff`, `.tiff`],
10+
[`/absolutely/this/will/work.bmp`, `.bmp`],
11+
].forEach(([url, ext]) => {
12+
expect(getRemoteFileExtension(url)).toBe(ext)
13+
})
14+
})
15+
})

packages/gatsby-source-filesystem/src/create-remote-file-node.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { isWebUri } = require(`valid-url`)
66
const Queue = require(`better-queue`)
77

88
const { createFileNode } = require(`./create-file-node`)
9+
const { getRemoteFileExtension } = require(`./utils`)
910
const cacheId = url => `create-remote-file-node-${url}`
1011

1112
/********************
@@ -195,7 +196,7 @@ async function processRemoteNode({
195196

196197
// Create the temp and permanent file names for the url.
197198
const digest = createHash(url)
198-
const ext = path.parse(url).ext
199+
const ext = getRemoteFileExtension(url)
199200

200201
const tmpFilename = createFilePath(programDir, `tmp-${digest}`, ext)
201202
const filename = createFilePath(programDir, digest, ext)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const path = require(`path`)
2+
const Url = require(`url`)
3+
4+
/**
5+
* getRemoteFileExtension
6+
* --
7+
* Parses remote url to retrieve remote file extension
8+
*
9+
*
10+
* @param {String} url
11+
* @return {String} extension
12+
*/
13+
export function getRemoteFileExtension(url) {
14+
return path.parse(Url.parse(url).pathname).ext
15+
}

0 commit comments

Comments
 (0)