Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ Add Google native files support to file:download - Google Drive #2372

Merged
merged 8 commits into from
Dec 21, 2021
143 changes: 140 additions & 3 deletions packages/nodes-base/nodes/Google/Drive/GoogleDrive.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export class GoogleDrive implements INodeType {
// file:download
// ----------------------------------
{
displayName: 'File Id',
displayName: 'File ID',
name: 'fileId',
type: 'string',
default: '',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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[];
Expand Down