Skip to content

Commit dfc069d

Browse files
hbishpieh
authored andcommitted
feat(gatsby-source-filesystem): keep original name of remote files (#9777)
1 parent 7609e9e commit dfc069d

File tree

4 files changed

+53
-14
lines changed

4 files changed

+53
-14
lines changed

packages/gatsby-source-filesystem/src/__tests__/utils.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
const { getRemoteFileExtension } = require(`../utils`)
1+
const { getRemoteFileExtension, getRemoteFileName } = require(`../utils`)
22

33
describe(`create remote file node`, () => {
4-
it(`can correctly retrieve files extensions`, () => {
4+
it(`can correctly retrieve file name and extensions`, () => {
55
;[
66
[
77
`https://scontent.xx.fbcdn.net/v/t51.2885-15/42078503_294439751160571_1602896118583132160_n.jpg?_nc_cat=101&oh=e30490a47409051c45dc3daacf616bc0&oe=5C5EA8EB`,
8+
`42078503_294439751160571_1602896118583132160_n`,
89
`.jpg`,
910
],
1011
[
1112
`https://facebook.com/hello,_world_asdf12341234.jpeg?test=true&other_thing=also-true`,
13+
`hello,_world_asdf12341234`,
1214
`.jpeg`,
1315
],
14-
[`https://test.com/asdf.png`, `.png`],
15-
[`./path/to/relative/file.tiff`, `.tiff`],
16-
[`/absolutely/this/will/work.bmp`, `.bmp`],
17-
].forEach(([url, ext]) => {
16+
[`https://test.com/asdf.png`, `asdf`, `.png`],
17+
[`./path/to/relative/file.tiff`, `file`, `.tiff`],
18+
[`/absolutely/this/will/work.bmp`, `work`, `.bmp`],
19+
].forEach(([url, name, ext]) => {
20+
expect(getRemoteFileName(url)).toBe(name)
1821
expect(getRemoteFileExtension(url)).toBe(ext)
1922
})
2023
})

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

+16-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const readChunk = require(`read-chunk`)
88
const fileType = require(`file-type`)
99

1010
const { createFileNode } = require(`./create-file-node`)
11-
const { getRemoteFileExtension } = require(`./utils`)
11+
const { getRemoteFileExtension, getRemoteFileName } = require(`./utils`)
1212
const cacheId = url => `create-remote-file-node-${url}`
1313

1414
/********************
@@ -75,7 +75,7 @@ const FS_PLUGIN_DIR = `gatsby-source-filesystem`
7575
* @return {String}
7676
*/
7777
const createFilePath = (directory, filename, ext) =>
78-
path.join(directory, CACHE_DIR, FS_PLUGIN_DIR, `${filename}${ext}`)
78+
path.join(directory, `${filename}${ext}`)
7979

8080
/********************
8181
* Queue Management *
@@ -178,8 +178,12 @@ async function processRemoteNode({
178178
ext,
179179
}) {
180180
// Ensure our cache directory exists.
181-
const programDir = store.getState().program.directory
182-
await fs.ensureDir(path.join(programDir, CACHE_DIR, FS_PLUGIN_DIR))
181+
const pluginCacheDir = path.join(
182+
store.getState().program.directory,
183+
CACHE_DIR,
184+
FS_PLUGIN_DIR
185+
)
186+
await fs.ensureDir(pluginCacheDir)
183187

184188
// See if there's response headers for this url
185189
// from a previous request.
@@ -198,11 +202,12 @@ async function processRemoteNode({
198202

199203
// Create the temp and permanent file names for the url.
200204
const digest = createHash(url)
205+
const name = getRemoteFileName(url)
201206
if (!ext) {
202207
ext = getRemoteFileExtension(url)
203208
}
204209

205-
const tmpFilename = createFilePath(programDir, `tmp-${digest}`, ext)
210+
const tmpFilename = createFilePath(pluginCacheDir, `tmp-${digest}`, ext)
206211

207212
// Fetch the file.
208213
try {
@@ -218,7 +223,12 @@ async function processRemoteNode({
218223
ext = `.${filetype.ext}`
219224
}
220225
}
221-
const filename = createFilePath(programDir, digest, ext)
226+
227+
const filename = createFilePath(
228+
path.join(pluginCacheDir, digest),
229+
name,
230+
ext
231+
)
222232
// If the status code is 200, move the piped temp file to the real name.
223233
if (response.statusCode === 200) {
224234
await fs.move(tmpFilename, filename, { overwrite: true })

packages/gatsby-source-filesystem/src/utils.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
const path = require(`path`)
22
const Url = require(`url`)
33

4+
/**
5+
* getParsedPath
6+
* --
7+
* Parses remote url to a path object
8+
*
9+
*
10+
* @param {String} url
11+
* @return {Object} path
12+
*/
13+
function getParsedPath(url) {
14+
return path.parse(Url.parse(url).pathname)
15+
}
16+
417
/**
518
* getRemoteFileExtension
619
* --
@@ -11,5 +24,18 @@ const Url = require(`url`)
1124
* @return {String} extension
1225
*/
1326
export function getRemoteFileExtension(url) {
14-
return path.parse(Url.parse(url).pathname).ext
27+
return getParsedPath(url).ext
28+
}
29+
30+
/**
31+
* getRemoteFileName
32+
* --
33+
* Parses remote url to retrieve remote file name
34+
*
35+
*
36+
* @param {String} url
37+
* @return {String} filename
38+
*/
39+
export function getRemoteFileName(url) {
40+
return getParsedPath(url).name
1541
}

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -9122,7 +9122,7 @@ graphql-type-json@^0.2.1:
91229122
resolved "https://registry.yarnpkg.com/graphql-type-json/-/graphql-type-json-0.2.1.tgz#d2c177e2f1b17d87f81072cd05311c0754baa420"
91239123
integrity sha1-0sF34vGxfYf4EHLNBTEcB1S6pCA=
91249124

9125-
graphql@0.13.2, graphql@^0.13.0, graphql@^0.13.2:
9125+
graphql@^0.13.0, graphql@^0.13.2:
91269126
version "0.13.2"
91279127
resolved "http://registry.npmjs.org/graphql/-/graphql-0.13.2.tgz#4c740ae3c222823e7004096f832e7b93b2108270"
91289128
integrity sha512-QZ5BL8ZO/B20VA8APauGBg3GyEgZ19eduvpLWoq5x7gMmWnHoy8rlQWPLmWgFvo1yNgjSEFMesmS4R6pPr7xog==

0 commit comments

Comments
 (0)