From 1da6db3782776ad9f6b3bf8565aec98fa72cbec8 Mon Sep 17 00:00:00 2001 From: Dhenain Ambroise Date: Wed, 25 Aug 2021 00:10:08 +0200 Subject: [PATCH] Adapt Airtable sanitizer due to a recent change in the Airtable REST API (which now returns width/height for all attachments, which broke our dynamic property lookup by overriding our own width/height props) --- src/modules/core/airtable/sanitizeRawAirtableDS.ts | 11 +++++++++-- .../airtable/types/RawAirtableAttachmentBaseFields.ts | 2 ++ src/modules/core/data/types/AirtableAttachment.ts | 2 ++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/modules/core/airtable/sanitizeRawAirtableDS.ts b/src/modules/core/airtable/sanitizeRawAirtableDS.ts index 90622f06c..6239c80b9 100644 --- a/src/modules/core/airtable/sanitizeRawAirtableDS.ts +++ b/src/modules/core/airtable/sanitizeRawAirtableDS.ts @@ -281,10 +281,17 @@ export const sanitizeRawAirtableDS = (airtableSchema: AirtableSchema, airtableDa delete attachmentFields?.['id']; // Delete the id field so that it won't override the record id (it'll still be available as "attachmentId") delete sanitizedRecord[tableSchema?.attachmentFieldName]; // Delete the attachment altogether to avoid noise (all props will be copied to the sanitized record) - // Add the native Airtable attachment fields + // Add the native Airtable attachment fields as properties of the record map(attachmentFields, (attachmentFieldValue: any, attachmentFieldName: string) => { - sanitizedRecord[attachmentFieldName] = attachmentFieldValue; + // Except for width/height which shouldn't be + if (attachmentFieldName !== 'width' && attachmentFieldName !== 'height') { + sanitizedRecord[attachmentFieldName] = attachmentFieldValue; + } }); + + // Copy auto-detected width/height properties using aliases (they represent the dimensions of the uploaded/origin attachment, not the dimensions it's meant to be resized to on the screen) + sanitizedRecord['attachmentWidth'] = attachmentFields?.width; + sanitizedRecord['attachmentHeight'] = attachmentFields?.height; } else { // When there is no attachment in the attachment field, then do nothing } diff --git a/src/modules/core/airtable/types/RawAirtableAttachmentBaseFields.ts b/src/modules/core/airtable/types/RawAirtableAttachmentBaseFields.ts index 81c4cafcb..ba86350db 100644 --- a/src/modules/core/airtable/types/RawAirtableAttachmentBaseFields.ts +++ b/src/modules/core/airtable/types/RawAirtableAttachmentBaseFields.ts @@ -8,6 +8,8 @@ import { RawAirtableAttachmentThumbnails } from './RawAirtableAttachmentThumbnai export type RawAirtableAttachmentBaseFields = { id: string; url: string; + width: number; + height: number; filename: string; size?: number; // TODO Not sure if it's always present, gotta confirm behaviour type: string; diff --git a/src/modules/core/data/types/AirtableAttachment.ts b/src/modules/core/data/types/AirtableAttachment.ts index 940569163..856c2a4b2 100644 --- a/src/modules/core/data/types/AirtableAttachment.ts +++ b/src/modules/core/data/types/AirtableAttachment.ts @@ -8,5 +8,7 @@ import { AirtableRecordBaseFields } from './AirtableRecordBaseFields'; */ export type AirtableAttachment = { attachmentId: string; // The airtable attachment id, can't be stored as "id" because it's already taken by the Asset record id + attachmentWidth: number; // The airtable attachment original width, as it was detected by Airtable when the file was first uploaded to their service + attachmentHeight: number; // The airtable attachment original height, as it was detected by Airtable when the file was first uploaded to their service } & Omit & AirtableRecordBaseFields;