Skip to content

Commit

Permalink
⚡ Add file support
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoE105 committed Nov 16, 2021
1 parent d430134 commit 837f799
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 7 deletions.
112 changes: 110 additions & 2 deletions packages/nodes-base/nodes/Notion/DatabasePageDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const databasePageOperations = [
export const databasePageFields = [

/* -------------------------------------------------------------------------- */
/* databasePage:create */
/* databasePage:create */
/* -------------------------------------------------------------------------- */
{
displayName: 'Database ID',
Expand Down Expand Up @@ -422,13 +422,57 @@ export const databasePageFields = [
default: 'default',
description: 'Time zone to use. By default n8n timezone is used.',
},
{
displayName: 'File URLs',
name: 'fileUrls',
placeholder: 'Add File',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
sortable: true,
},
displayOptions: {
show: {
'/version': [
2,
],
type: [
'files',
],
},
},
description: 'The operations to perform.',
default: {},
options: [
{
name: 'fileUrl',
displayName: 'File',
values: [
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
description: 'File Name',
},
{
displayName: 'File URL',
name: 'url',
type: 'string',
default: '',
description: 'Link to externally hosted file',
},
],
},
],
},
],
},
],
},
...blocks('databasePage', 'create'),
/* -------------------------------------------------------------------------- */
/* databasePage:update */
/* databasePage:update */
/* -------------------------------------------------------------------------- */
{
displayName: 'Page ID',
Expand Down Expand Up @@ -799,6 +843,50 @@ export const databasePageFields = [
default: 'default',
description: 'Time zone to use. By default n8n timezone is used.',
},
{
displayName: 'File URLs',
name: 'fileUrls',
placeholder: 'Add File',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
sortable: true,
},
displayOptions: {
show: {
'/version': [
2,
],
type: [
'files',
],
},
},
description: 'The operations to perform.',
default: {},
options: [
{
name: 'fileUrl',
displayName: 'File',
values: [
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
description: 'File Name',
},
{
displayName: 'File URL',
name: 'url',
type: 'string',
default: '',
description: 'Link to externally hosted file',
},
],
},
],
},
],
},
],
Expand Down Expand Up @@ -901,6 +989,26 @@ export const databasePageFields = [
default: {},
placeholder: 'Add Field',
options: [
{
displayName: 'Download Files',
name: 'downloadFiles',
type: 'boolean',
displayOptions: {
show: {
'/version': [
2,
],
'/resource': [
'databasePage',
],
'/operation': [
'getAll',
],
},
},
default: false,
description: 'If a database field contains a file, whether to download it',
},
{
displayName: 'Filters',
name: 'filter',
Expand Down
55 changes: 50 additions & 5 deletions packages/nodes-base/nodes/Notion/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import {
} from 'n8n-core';

import {
IBinaryKeyData,
ICredentialDataDecryptedObject,
ICredentialTestFunctions,
IDataObject,
IDisplayOptions,
INodeExecutionData,
INodeProperties,
IPollFunctions,
NodeApiError,
Expand Down Expand Up @@ -49,8 +51,10 @@ export async function notionApiRequest(this: IHookFunctions | IExecuteFunctions

options = Object.assign({}, options, option);
const credentials = await this.getCredentials('notionApi') as IDataObject;
options!.headers!['Authorization'] = `Bearer ${credentials.apiKey}`;

if (!uri) {
//do not include the API Key when downloading files, else the request fails
options!.headers!['Authorization'] = `Bearer ${credentials.apiKey}`;
}
if (Object.keys(body).length === 0) {
delete options.body;
}
Expand Down Expand Up @@ -314,6 +318,12 @@ function getPropertyKeyValue(value: any, type: string, timezone: string) {
};
}
break;
case 'files':
result = {
type: 'files', files: value.fileUrls.fileUrl
.map((file: { name: string, url: string }) => ({ name: file.name, type: 'external', external: { url: file.url } })),
};
break;
default:
}
return result;
Expand Down Expand Up @@ -441,18 +451,21 @@ export function simplifyProperties(properties: any) {
} else if (['rollup'].includes(properties[key].type)) {
//TODO figure how to resolve rollup field type
// results[`${key}`] = properties[key][type][properties[key][type].type];
} else if (['files'].includes(properties[key].type)) {
// tslint:disable-next-line: no-any
results[`${key}`] = properties[key][type].map((file: { type: string, [key: string]: any }) => (file[file.type].url));
}
}
return results;
}

// tslint:disable-next-line: no-any
export function simplifyObjects(objects: any) {
export function simplifyObjects(objects: any, download = false) {
if (!Array.isArray(objects)) {
objects = [objects];
}
const results: IDataObject[] = [];
for (const { object, id, properties, parent, title } of objects) {
for (const { object, id, properties, parent, title, json, binary, url, created_time, last_edited_time } of objects) {
if (object === 'page' && (parent.type === 'page_id' || parent.type === 'workspace')) {
results.push({
id,
Expand All @@ -463,10 +476,18 @@ export function simplifyObjects(objects: any) {
id,
...simplifyProperties(properties),
});
} else if (download && json.object === 'page' && json.parent.type === 'database_id') {
results.push({
json: {
id,
...simplifyProperties(json.properties),
},
binary,
});
} else if (object === 'database') {
results.push({
id,
title: title[0].plain_text,
title: title[0].plain_text || '',
});
}
}
Expand Down Expand Up @@ -668,3 +689,27 @@ export function validateCrendetials(this: ICredentialTestFunctions, credentials:
};
return this.helpers.request!(options);
}

// tslint:disable-next-line: no-any
export async function downloadFiles(this: IExecuteFunctions | IPollFunctions, records: [{ properties: { [key: string]: any | { id: string, type: string, files: [{ external: { url: string } } | { file: { url: 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 key of Object.keys(record.properties)) {
if (record.properties[key].type === 'files') {
if (record.properties[key].files.length) {
for (const [index, file] of record.properties[key].files.entries()) {
const data = await notionApiRequest.call(this, 'GET', '', {}, {}, file?.file?.url || file?.external?.url, { json: false, encoding: null });
element.binary![`${key}_${index}`] = await this.helpers.prepareBinaryData(data);
}
}
}
}
if (Object.keys(element.binary as IBinaryKeyData).length === 0) {
delete element.binary;
}
elements.push(element);
}
return elements;
}
6 changes: 6 additions & 0 deletions packages/nodes-base/nodes/Notion/v1/VersionDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ export const versionDescription: INodeTypeDescription = {
type: 'notice',
default: '',
},
{
displayName: 'Version',
name: 'version',
type: 'hidden',
default: 1,
},
{
displayName: 'Resource',
name: 'resource',
Expand Down
6 changes: 6 additions & 0 deletions packages/nodes-base/nodes/Notion/v2/VersionDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ export const versionDescription: INodeTypeDescription = {
type: 'notice',
default: '',
},
{
displayName: 'Version',
name: 'version',
type: 'hidden',
default: 2,
},
{
displayName: 'Resource',
name: 'resource',
Expand Down

0 comments on commit 837f799

Please sign in to comment.