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

feat(gatsby-source-filesystem): keep original name of remote files along with hash #9777

Merged
merged 6 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 9 additions & 6 deletions packages/gatsby-source-filesystem/src/__tests__/utils.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
const { getRemoteFileExtension } = require(`../utils`)
const { getRemoteFileExtension, getRemoteFileName } = require(`../utils`)

describe(`create remote file node`, () => {
it(`can correctly retrieve files extensions`, () => {
it(`can correctly retrieve file name and extensions`, () => {
;[
[
`https://scontent.xx.fbcdn.net/v/t51.2885-15/42078503_294439751160571_1602896118583132160_n.jpg?_nc_cat=101&oh=e30490a47409051c45dc3daacf616bc0&oe=5C5EA8EB`,
`42078503_294439751160571_1602896118583132160_n`,
`.jpg`,
],
[
`https://facebook.com/hello,_world_asdf12341234.jpeg?test=true&other_thing=also-true`,
`hello,_world_asdf12341234`,
`.jpeg`,
],
[`https://test.com/asdf.png`, `.png`],
[`./path/to/relative/file.tiff`, `.tiff`],
[`/absolutely/this/will/work.bmp`, `.bmp`],
].forEach(([url, ext]) => {
[`https://test.com/asdf.png`, `asdf`, `.png`],
[`./path/to/relative/file.tiff`, `file`, `.tiff`],
[`/absolutely/this/will/work.bmp`, `work`, `.bmp`],
].forEach(([url, name, ext]) => {
expect(getRemoteFileName(url)).toBe(name)
expect(getRemoteFileExtension(url)).toBe(ext)
})
})
Expand Down
22 changes: 16 additions & 6 deletions packages/gatsby-source-filesystem/src/create-remote-file-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const readChunk = require(`read-chunk`)
const fileType = require(`file-type`)

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

/********************
Expand Down Expand Up @@ -75,7 +75,7 @@ const FS_PLUGIN_DIR = `gatsby-source-filesystem`
* @return {String}
*/
const createFilePath = (directory, filename, ext) =>
path.join(directory, CACHE_DIR, FS_PLUGIN_DIR, `${filename}${ext}`)
path.join(directory, `${filename}${ext}`)

/********************
* Queue Management *
Expand Down Expand Up @@ -178,8 +178,12 @@ async function processRemoteNode({
ext,
}) {
// Ensure our cache directory exists.
const programDir = store.getState().program.directory
await fs.ensureDir(path.join(programDir, CACHE_DIR, FS_PLUGIN_DIR))
const pluginCacheDir = path.join(
store.getState().program.directory,
CACHE_DIR,
FS_PLUGIN_DIR
)
await fs.ensureDir(pluginCacheDir)

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

// Create the temp and permanent file names for the url.
const digest = createHash(url)
const name = getRemoteFileName(url)
if (!ext) {
ext = getRemoteFileExtension(url)
}

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

// Fetch the file.
try {
Expand All @@ -218,7 +223,12 @@ async function processRemoteNode({
ext = `.${filetype.ext}`
}
}
const filename = createFilePath(programDir, digest, ext)

const filename = createFilePath(
path.join(pluginCacheDir, digest),
name,
ext
)
// If the status code is 200, move the piped temp file to the real name.
if (response.statusCode === 200) {
await fs.move(tmpFilename, filename, { overwrite: true })
Expand Down
28 changes: 27 additions & 1 deletion packages/gatsby-source-filesystem/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
const path = require(`path`)
const Url = require(`url`)

/**
* getParsedPath
* --
* Parses remote url to a path object
*
*
* @param {String} url
* @return {Object} path
*/
function getParsedPath(url) {
return path.parse(Url.parse(url).pathname)
}

/**
* getRemoteFileExtension
* --
Expand All @@ -11,5 +24,18 @@ const Url = require(`url`)
* @return {String} extension
*/
export function getRemoteFileExtension(url) {
return path.parse(Url.parse(url).pathname).ext
return getParsedPath(url).ext
}

/**
* getRemoteFileName
* --
* Parses remote url to retrieve remote file name
*
*
* @param {String} url
* @return {String} filename
*/
export function getRemoteFileName(url) {
return getParsedPath(url).name
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9122,7 +9122,7 @@ graphql-type-json@^0.2.1:
resolved "https://registry.yarnpkg.com/graphql-type-json/-/graphql-type-json-0.2.1.tgz#d2c177e2f1b17d87f81072cd05311c0754baa420"
integrity sha1-0sF34vGxfYf4EHLNBTEcB1S6pCA=

graphql@0.13.2, graphql@^0.13.0, graphql@^0.13.2:
graphql@^0.13.0, graphql@^0.13.2:
version "0.13.2"
resolved "http://registry.npmjs.org/graphql/-/graphql-0.13.2.tgz#4c740ae3c222823e7004096f832e7b93b2108270"
integrity sha512-QZ5BL8ZO/B20VA8APauGBg3GyEgZ19eduvpLWoq5x7gMmWnHoy8rlQWPLmWgFvo1yNgjSEFMesmS4R6pPr7xog==
Expand Down