Skip to content

Commit 303fe17

Browse files
bors[bot]ryan-talusjbolda
authored
Merge #374
374: Fix: Pull fileNode file name from airtable metadata r=jbolda a=ryan-talus 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 file. This change makes file name changes in airtable usable in the fileNode instead of needing to download, rename, and re-upload the file. Co-authored-by: Ryan <rzimmerman@talusanalytics.com> Co-authored-by: Ryan Zimmerman <61983809+ryan-talus@users.noreply.github.com> Co-authored-by: Jacob Bolda <me@jacobbolda.com>
2 parents 37def78 + d7200c0 commit 303fe17

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

.changes/fix-airtable-filenames.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"gatsby-source-airtable": minor
3+
---
4+
5+
This makes the fileNode pull `name` from the airtable metadata instead of the
6+
remote file, because when an airtable user changes the file name, airtable does
7+
not rename the original url. This change makes file name changes in airtable
8+
usable in the fileNode instead of needing to download, rename, and re-upload the
9+
file.

gatsby-node.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const crypto = require("crypto");
33
const { createRemoteFileNode } = require("gatsby-source-filesystem");
44
const { map } = require("bluebird");
55
const mime = require("mime/lite");
6+
const path = require("path");
67

78
exports.sourceNodes = async (
89
{ actions, createNodeId, store, cache },
@@ -304,9 +305,17 @@ const localFileCheck = async (
304305
// `data` is direct from Airtable so we don't use
305306
// the cleanKey here
306307
data[key].forEach((attachment) => {
307-
const ext = mime.getExtension(attachment.type); // unknown type returns null
308+
// get the filename from the airtable metadata instead of the remote file
309+
const airtableFile = path.parse(attachment.filename);
310+
// console.log(airtableFile);
311+
let ext = airtableFile.ext;
312+
if (!ext) {
313+
const deriveExt = mime.getExtension(attachment.type); // unknown type returns null
314+
ext = !!deriveExt ? `.${deriveExt}` : undefined;
315+
}
308316
let attachmentNode = createRemoteFileNode({
309317
url: attachment.url,
318+
name: airtableFile.name.replace(/[/\\?%*:|"<>]/g, ""),
310319
store,
311320
cache,
312321
createNode,

0 commit comments

Comments
 (0)