diff --git a/.changes/fix-airtable-filenames.md b/.changes/fix-airtable-filenames.md new file mode 100644 index 00000000..fd3f98a0 --- /dev/null +++ b/.changes/fix-airtable-filenames.md @@ -0,0 +1,9 @@ +--- +"gatsby-source-airtable": minor +--- + +This makes the fileNode pull `name` from the airtable metadata instead of the +remote file, because when an airtable user changes the file name, airtable does +not rename the original url. This change makes file name changes in airtable +usable in the fileNode instead of needing to download, rename, and re-upload the +file. diff --git a/gatsby-node.js b/gatsby-node.js index bdd512b2..919d28a8 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -3,6 +3,7 @@ const crypto = require("crypto"); const { createRemoteFileNode } = require("gatsby-source-filesystem"); const { map } = require("bluebird"); const mime = require("mime/lite"); +const path = require("path"); exports.sourceNodes = async ( { actions, createNodeId, store, cache }, @@ -304,9 +305,17 @@ const localFileCheck = async ( // `data` is direct from Airtable so we don't use // the cleanKey here data[key].forEach((attachment) => { - const ext = mime.getExtension(attachment.type); // unknown type returns null + // get the filename from the airtable metadata instead of the remote file + const airtableFile = path.parse(attachment.filename); + // console.log(airtableFile); + let ext = airtableFile.ext; + if (!ext) { + const deriveExt = mime.getExtension(attachment.type); // unknown type returns null + ext = !!deriveExt ? `.${deriveExt}` : undefined; + } let attachmentNode = createRemoteFileNode({ url: attachment.url, + name: airtableFile.name.replace(/[/\\?%*:|"<>]/g, ""), store, cache, createNode,