mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-21 02:56:40 -08:00
⚡ List operation now can download attachaments automatically (#1199)
* ⚡ List operation now can download attachaments automatically https://community.n8n.io/t/how-to-send-attachments-using-mailgun-node/2979/4 * ⚡ Minor improvements to Airtable node Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
parent
1b0dda6ddc
commit
8111fea470
|
@ -1,6 +1,7 @@
|
|||
import {
|
||||
IExecuteFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
IDataObject,
|
||||
INodeExecutionData,
|
||||
|
@ -11,19 +12,20 @@ import {
|
|||
import {
|
||||
apiRequest,
|
||||
apiRequestAllItems,
|
||||
downloadRecordAttachments,
|
||||
} from './GenericFunctions';
|
||||
|
||||
export class Airtable implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Airtable',
|
||||
name: 'airtable',
|
||||
icon: 'file:airtable.png',
|
||||
icon: 'file:airtable.svg',
|
||||
group: ['input'],
|
||||
version: 1,
|
||||
description: 'Read, update, write and delete data from Airtable',
|
||||
defaults: {
|
||||
name: 'Airtable',
|
||||
color: '#445599',
|
||||
color: '#000000',
|
||||
},
|
||||
inputs: ['main'],
|
||||
outputs: ['main'],
|
||||
|
@ -188,7 +190,38 @@ export class Airtable implements INodeType {
|
|||
default: 100,
|
||||
description: 'Number of results to return.',
|
||||
},
|
||||
|
||||
{
|
||||
displayName: 'Download Attachments',
|
||||
name: 'downloadAttachments',
|
||||
type: 'boolean',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'list',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: false,
|
||||
description: `When set to true the attachment fields define in 'Download Fields' will be downloaded.`,
|
||||
},
|
||||
{
|
||||
displayName: 'Download Fields',
|
||||
name: 'downloadFieldNames',
|
||||
type: 'string',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'list',
|
||||
],
|
||||
downloadAttachments: [
|
||||
true,
|
||||
],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
description: `Name of the fields of type 'attachment' that should be downloaded. Multiple ones can be defined separated by comma. Case sensitive.`,
|
||||
},
|
||||
{
|
||||
displayName: 'Additional Options',
|
||||
name: 'additionalOptions',
|
||||
|
@ -489,6 +522,8 @@ export class Airtable implements INodeType {
|
|||
|
||||
returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
||||
|
||||
const downloadAttachments = this.getNodeParameter('downloadAttachments', 0) as boolean;
|
||||
|
||||
const additionalOptions = this.getNodeParameter('additionalOptions', 0, {}) as IDataObject;
|
||||
|
||||
for (const key of Object.keys(additionalOptions)) {
|
||||
|
@ -508,6 +543,12 @@ export class Airtable implements INodeType {
|
|||
|
||||
returnData.push.apply(returnData, responseData.records);
|
||||
|
||||
if (downloadAttachments === true) {
|
||||
const downloadFieldNames = (this.getNodeParameter('downloadFieldNames', 0) as string).split(',');
|
||||
const data = await downloadRecordAttachments.call(this, responseData.records, downloadFieldNames);
|
||||
return [data];
|
||||
}
|
||||
|
||||
} else if (operation === 'read') {
|
||||
// ----------------------------------
|
||||
// read
|
||||
|
|
|
@ -4,9 +4,28 @@ import {
|
|||
ILoadOptionsFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import { OptionsWithUri } from 'request';
|
||||
import { IDataObject } from 'n8n-workflow';
|
||||
import {
|
||||
OptionsWithUri,
|
||||
} from 'request';
|
||||
|
||||
import {
|
||||
IBinaryKeyData,
|
||||
IDataObject,
|
||||
INodeExecutionData,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
|
||||
interface IAttachment {
|
||||
url: string;
|
||||
filename: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
export interface IRecord {
|
||||
fields: {
|
||||
[key: string]: string | IAttachment[],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Make an API request to Airtable
|
||||
|
@ -17,7 +36,7 @@ import { IDataObject } from 'n8n-workflow';
|
|||
* @param {object} body
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: object, query?: IDataObject): Promise<any> { // tslint:disable-line:no-any
|
||||
export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: object, query?: IDataObject, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
||||
const credentials = this.getCredentials('airtableApi');
|
||||
|
||||
if (credentials === undefined) {
|
||||
|
@ -37,10 +56,18 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa
|
|||
method,
|
||||
body,
|
||||
qs: query,
|
||||
uri: `https://api.airtable.com/v0/${endpoint}`,
|
||||
uri: uri || `https://api.airtable.com/v0/${endpoint}`,
|
||||
json: true,
|
||||
};
|
||||
|
||||
if (Object.keys(option).length !== 0) {
|
||||
Object.assign(options, option);
|
||||
}
|
||||
|
||||
if (Object.keys(body).length === 0) {
|
||||
delete options.body;
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.helpers.request!(options);
|
||||
} catch (error) {
|
||||
|
@ -101,3 +128,28 @@ export async function apiRequestAllItems(this: IHookFunctions | IExecuteFunction
|
|||
records: returnData,
|
||||
};
|
||||
}
|
||||
|
||||
export async function downloadRecordAttachments(this: IExecuteFunctions, records: IRecord[], fieldNames: string[]): Promise<INodeExecutionData[]> {
|
||||
const elements: INodeExecutionData[] = [];
|
||||
for (const record of records) {
|
||||
const element: INodeExecutionData = { json: {}, binary: {} };
|
||||
element.json = record as unknown as IDataObject;
|
||||
for (const fieldName of fieldNames) {
|
||||
if (record.fields[fieldName] !== undefined) {
|
||||
for (const [index, attachment] of (record.fields[fieldName] as IAttachment[]).entries()) {
|
||||
const file = await apiRequest.call(this, 'GET', '', {}, {}, attachment.url, { json: false, encoding: null });
|
||||
element.binary![`${fieldName}_${index}`] = {
|
||||
data: Buffer.from(file).toString('base64'),
|
||||
fileName: attachment.filename,
|
||||
mimeType: attachment.type,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Object.keys(element.binary as IBinaryKeyData).length === 0) {
|
||||
delete element.binary;
|
||||
}
|
||||
elements.push(element);
|
||||
}
|
||||
return elements;
|
||||
}
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 1.4 KiB |
21
packages/nodes-base/nodes/Airtable/airtable.svg
Normal file
21
packages/nodes-base/nodes/Airtable/airtable.svg
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Generator: Adobe Illustrator 22.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
|
||||
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
|
||||
viewBox="0 0 200 170" style="enable-background:new 0 0 200 170;" xml:space="preserve">
|
||||
<style type="text/css">
|
||||
.st0{fill:#FCB400;}
|
||||
.st1{fill:#18BFFF;}
|
||||
.st2{fill:#F82B60;}
|
||||
.st3{fill:#BA1E45;}
|
||||
</style>
|
||||
<g>
|
||||
<path class="st0" d="M89,4.8L16.2,34.9c-4.1,1.7-4,7.4,0.1,9.1l73.2,29c6.4,2.6,13.6,2.6,20,0l73.2-29c4.1-1.6,4.1-7.4,0.1-9.1
|
||||
L109.8,4.8C103.2,2,95.7,2,89,4.8"/>
|
||||
<path class="st1" d="M105.9,88.9v72.5c0,3.4,3.5,5.8,6.7,4.5l81.6-31.7c1.9-0.7,3.1-2.5,3.1-4.5V57.2c0-3.4-3.5-5.8-6.7-4.5
|
||||
L109,84.3C107.1,85.1,105.9,86.9,105.9,88.9"/>
|
||||
<path class="st2" d="M86.9,92.6l-24.2,11.7l-2.5,1.2L9.1,130c-3.2,1.6-7.4-0.8-7.4-4.4V57.5c0-1.3,0.7-2.4,1.6-3.3
|
||||
c0.4-0.4,0.8-0.7,1.2-0.9c1.2-0.7,3-0.9,4.4-0.3l77.5,30.7C90.4,85.2,90.7,90.8,86.9,92.6"/>
|
||||
<path class="st3" d="M86.9,92.6l-24.2,11.7L3.3,54.3c0.4-0.4,0.8-0.7,1.2-0.9c1.2-0.7,3-0.9,4.4-0.3l77.5,30.7
|
||||
C90.4,85.2,90.7,90.8,86.9,92.6"/>
|
||||
</g>
|
||||
</svg>
|
After Width: | Height: | Size: 1.1 KiB |
Loading…
Reference in a new issue