From 9b2002c79086073baed22a1a27ab264538794681 Mon Sep 17 00:00:00 2001 From: Orestis Ioannou Date: Wed, 23 Jan 2019 16:31:00 +0000 Subject: [PATCH] feat(gatsby-source-filesystem): add optional name parameter to createRemoteFileNode (#11054) Fix: #11037 ## Description Take an optional name parameter to fix issue with temporary urls having no "guessable" name ## Related Issues --- README.md | 5 +++-- src/create-remote-file-node.js | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c2ac4b0dca0d1..d0c5b664a8dd3 100644 --- a/README.md +++ b/README.md @@ -227,9 +227,9 @@ exports.downloadMediaFiles = ({ The file node can then be queried using GraphQL. See an example of this in the [gatsby-source-wordpress README](/packages/gatsby-source-wordpress/#image-processing) where downloaded images are queried using [gatsby-transformer-sharp](/packages/gatsby-transformer-sharp/) to use in the component [gatsby-image](/packages/gatsby-image/). -#### Retrieving the remote file extension +#### Retrieving the remote file name and extension -The helper tries first to retrieve the file extension by parsing the url and the path provided (e.g. if the url is https://example.com/image.jpg, the extension will be inferred as `.jpg`). If the url does not contain an extension, we use the [`file-type`](https://www.npmjs.com/package/file-type) package to infer the file type. Finally, the extension _can_ be explicitly passed, like so: +The helper tries first to retrieve the file name and extension by parsing the url and the path provided (e.g. if the url is https://example.com/image.jpg, the extension will be inferred as `.jpg` and the name as `image`). If the url does not contain an extension, we use the [`file-type`](https://www.npmjs.com/package/file-type) package to infer the file type. Finally, the name and the extension _can_ be explicitly passed, like so: ```javascript createRemoteFileNode({ @@ -241,5 +241,6 @@ createRemoteFileNode({ createNodeId, // if necessary! ext: ".jpg", + name: "image", }) ``` diff --git a/src/create-remote-file-node.js b/src/create-remote-file-node.js index 3bc24b977304d..608b00906b245 100644 --- a/src/create-remote-file-node.js +++ b/src/create-remote-file-node.js @@ -185,6 +185,7 @@ async function processRemoteNode({ auth = {}, createNodeId, ext, + name, }) { // Ensure our cache directory exists. const pluginCacheDir = path.join( @@ -211,7 +212,9 @@ async function processRemoteNode({ // Create the temp and permanent file names for the url. const digest = createHash(url) - const name = getRemoteFileName(url) + if (!name) { + name = getRemoteFileName(url) + } if (!ext) { ext = getRemoteFileExtension(url) } @@ -313,6 +316,7 @@ module.exports = ({ auth = {}, createNodeId, ext = null, + name = null, }) => { // validation of the input // without this it's notoriously easy to pass in the wrong `createNodeId` @@ -355,6 +359,7 @@ module.exports = ({ createNodeId, auth, ext, + name, }) fileDownloadPromise.then(() => bar.tick())