From cbe2fc2210abac4d81ed84b4f463a22fc6fe8541 Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Tue, 21 Dec 2021 17:36:37 -0500 Subject: [PATCH] :zap: Add Google native files support to file:download - Google Drive (#2372) * :zap: Add Google native files support to file:download * :zap: Small fix * :zap: Improvements * :zap: Fix lint issue * :zap: Improvements * :zap: Remove console.log Co-authored-by: Jan Oberhauser --- .../nodes/Google/Drive/GoogleDrive.node.ts | 143 +++++++++++++++++- 1 file changed, 140 insertions(+), 3 deletions(-) diff --git a/packages/nodes-base/nodes/Google/Drive/GoogleDrive.node.ts b/packages/nodes-base/nodes/Google/Drive/GoogleDrive.node.ts index dc1d5eb03c..9634c07e7f 100644 --- a/packages/nodes-base/nodes/Google/Drive/GoogleDrive.node.ts +++ b/packages/nodes-base/nodes/Google/Drive/GoogleDrive.node.ts @@ -239,7 +239,7 @@ export class GoogleDrive implements INodeType { // file:download // ---------------------------------- { - displayName: 'File Id', + displayName: 'File ID', name: 'fileId', type: 'string', default: '', @@ -291,6 +291,124 @@ export class GoogleDrive implements INodeType { }, }, options: [ + { + displayName: 'Google File Conversion', + name: 'googleFileConversion', + type: 'fixedCollection', + typeOptions: { + multipleValues: false, + }, + default: {}, + placeholder: 'Add Conversion', + options: [ + { + displayName: 'Conversion', + name: 'conversion', + values: [ + { + displayName: 'Google Docs', + name: 'docsToFormat', + type: 'options', + options: [ + { + name: 'To MS Word', + value: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + }, + { + name: 'To PDF', + value: 'application/pdf', + }, + { + name: 'To OpenOffice Doc', + value: 'application/vnd.oasis.opendocument.text', + }, + { + name: 'To HTML', + value: 'text/html', + }, + { + name: 'To Rich Text', + value: 'application/rtf', + }, + ], + default: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + description: 'Format used to export when downloading Google Docs files', + }, + { + displayName: 'Google Drawings', + name: 'drawingsToFormat', + type: 'options', + options: [ + { + name: 'To JPEG', + value: 'image/jpeg', + }, + { + name: 'To PNG', + value: 'image/png', + }, + { + name: 'To SVG', + value: 'image/svg+xml', + }, + { + name: 'To PDF', + value: 'application/pdf', + }, + ], + default: 'image/jpeg', + description: 'Format used to export when downloading Google Drawings files', + }, + { + displayName: 'Google Slides', + name: 'slidesToFormat', + type: 'options', + options: [ + { + name: 'To MS PowerPoint', + value: 'application/vnd.openxmlformats-officedocument.presentationml.presentation', + }, + { + name: 'To PDF', + value: 'application/pdf', + }, + { + name: 'To OpenOffice Presentation', + value: 'application/vnd.oasis.opendocument.presentation', + }, + { + name: 'To Plain Text', + value: 'text/plain', + }, + ], + default: 'application/vnd.openxmlformats-officedocument.presentationml.presentation', + description: 'Format used to export when downloading Google Slides files', + }, + { + displayName: 'Google Sheets', + name: 'sheetsToFormat', + type: 'options', + options: [ + { + name: 'To MS Excel', + value: 'application/x-vnd.oasis.opendocument.spreadsheet', + }, + { + name: 'To PDF', + value: 'application/pdf', + }, + { + name: 'To CSV', + value: 'text/csv', + }, + ], + default: 'application/x-vnd.oasis.opendocument.spreadsheet', + description: 'Format used to export when downloading Google Spreadsheets files', + }, + ], + }, + ], + }, { displayName: 'File Name', name: 'fileName', @@ -2136,7 +2254,26 @@ export class GoogleDrive implements INodeType { json: false, }; - const response = await googleApiRequest.call(this, 'GET', `/drive/v3/files/${fileId}`, {}, { alt: 'media' }, undefined, requestOptions); + const file = await googleApiRequest.call(this, 'GET', `/drive/v3/files/${fileId}`, {}, { fields: 'mimeType' }); + let response; + + if (file.mimeType.includes('vnd.google-apps')) { + const parameterKey = 'options.googleFileConversion.conversion'; + const type = file.mimeType.split('.')[2]; + let mime; + if (type === 'document') { + mime = this.getNodeParameter(`${parameterKey}.docsToFormat`, i, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') as string; + } else if (type === 'presentation') { + mime = this.getNodeParameter(`${parameterKey}.slidesToFormat`, i, 'application/vnd.openxmlformats-officedocument.presentationml.presentation') as string; + } else if (type === 'spreadsheet') { + mime = this.getNodeParameter(`${parameterKey}.sheetsToFormat`, i, 'application/x-vnd.oasis.opendocument.spreadsheet') as string; + } else { + mime = this.getNodeParameter(`${parameterKey}.drawingsToFormat`, i, 'image/jpeg') as string; + } + response = await googleApiRequest.call(this, 'GET', `/drive/v3/files/${fileId}/export`, {}, { mimeType: mime }, undefined, requestOptions); + } else { + response = await googleApiRequest.call(this, 'GET', `/drive/v3/files/${fileId}`, {}, { alt: 'media' }, undefined, requestOptions); + } let mimeType: string | undefined; let fileName: string | undefined = undefined; @@ -2321,7 +2458,7 @@ export class GoogleDrive implements INodeType { const properties = this.getNodeParameter('options.propertiesUi.propertyValues', i, []) as IDataObject[]; if (properties.length) { - Object.assign(body, { properties: properties.reduce((obj, value) => Object.assign(obj, { [`${value.key}`]: value.value }), {}) } ); + Object.assign(body, { properties: properties.reduce((obj, value) => Object.assign(obj, { [`${value.key}`]: value.value }), {}) }); } const appProperties = this.getNodeParameter('options.appPropertiesUi.appPropertyValues', i, []) as IDataObject[];