Skip to content

Commit

Permalink
Merge pull request #4342 from AbdullahBitar/main
Browse files Browse the repository at this point in the history
feat(google-drive): allow new file trigger to include content
  • Loading branch information
abuaboud committed Apr 13, 2024
2 parents 7af0d84 + 108f32f commit 01dad08
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 76 deletions.
2 changes: 1 addition & 1 deletion packages/pieces/community/google-drive/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "@activepieces/piece-google-drive",
"version": "0.5.21"
"version": "0.5.22"
}
75 changes: 2 additions & 73 deletions packages/pieces/community/google-drive/src/lib/action/read-file.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { googleDriveAuth } from '../..';
import { createAction, Property } from '@activepieces/pieces-framework';
import { extension } from 'mime-types';
import { downloadFileFromDrive } from '../common/get-file-content';

export const readFile = createAction({
auth: googleDriveAuth,
Expand All @@ -19,77 +19,6 @@ export const readFile = createAction({
}),
},
run: async ({ auth, propsValue, files }) => {
let mimeType = (
await fetch(
`https://www.googleapis.com/drive/v3/files/${propsValue.fileId}?fields=mimeType`,
{
headers: {
Authorization: `Bearer ${auth.access_token}`,
},
}
).then((res) => res.json())
)['mimeType'] as string;

const googledlCall = async (url: string) => {
const download = await fetch(url, {
headers: {
Authorization: `Bearer ${auth.access_token}`,
},
})
.then((response) =>
response.ok ? response.blob() : Promise.reject(response)
)
.catch((error) =>
Promise.reject(
new Error(
`Error when download file:\n\tDownload file response: ${
(error as Error).message ?? error
}`
)
)
);

const extension = '.' + extension(mimeType);
const srcFileName = propsValue.fileName;
const fileName =
(srcFileName
? srcFileName.replace(new RegExp(extension + '$'), '')
: propsValue.fileId) + extension;
return files.write({
fileName,
data: Buffer.from(await download.arrayBuffer()),
});
};

// the google drive API doesn't allowed downloading google documents but we can export them to office formats
if (
[
'application/vnd.google-apps.document',
'application/vnd.google-apps.spreadsheet',
'application/vnd.google-apps.presentation',
].includes(mimeType)
) {
switch (mimeType) {
case 'application/vnd.google-apps.document':
mimeType =
'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
break;
case 'application/vnd.google-apps.spreadsheet':
mimeType =
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
break;
case 'application/vnd.google-apps.presentation':
mimeType =
'application/vnd.openxmlformats-officedocument.presentationml.presentation';
break;
}
return await googledlCall(
`https://www.googleapis.com/drive/v3/files/${propsValue.fileId}/export?mimeType=${mimeType}`
);
} else {
return await googledlCall(
`https://www.googleapis.com/drive/v3/files/${propsValue.fileId}?alt=media`
);
}
return downloadFileFromDrive(auth, files, propsValue.fileId, propsValue.fileName)
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { FilesService, OAuth2PropertyValue, OAuth2Props, Property, ShortTextProperty, StaticPropsValue } from '@activepieces/pieces-framework';
import { extension } from 'mime-types';

async function getMimeType(auth: OAuth2PropertyValue<OAuth2Props>, fileId: string): Promise<string> {
const mimeType = (
await fetch(
`https://www.googleapis.com/drive/v3/files/${fileId}?fields=mimeType`,
{
headers: {
Authorization: `Bearer ${auth.access_token}`,
},
}
).then((res) => res.json())
)['mimeType'] as string;
return mimeType;
}

const googledlCall = async (url: string, auth: OAuth2PropertyValue<OAuth2Props>, fileId: string, files: FilesService, fileName: string | undefined) => {
const mimeType = await getMimeType(auth, fileId)

const download = await fetch(url, {
headers: {
Authorization: `Bearer ${auth.access_token}`,
},
})
.then((response) =>
response.ok ? response.blob() : Promise.reject(response)
)
.catch((error) =>
Promise.reject(
new Error(
`Error when download file:\n\tDownload file response: ${(error as Error).message ?? error
}`
)
)
);

const extention = '.' + extension(mimeType);

Check warning on line 38 in packages/pieces/community/google-drive/src/lib/common/get-file-content.ts

View workflow job for this annotation

GitHub Actions / Spell Check with Typos on Changed Files

"extention" should be "extension".
const srcFileName = fileName;
const name =
(srcFileName
? srcFileName.replace(new RegExp(extention + '$'), '')

Check warning on line 42 in packages/pieces/community/google-drive/src/lib/common/get-file-content.ts

View workflow job for this annotation

GitHub Actions / Spell Check with Typos on Changed Files

"extention" should be "extension".
: fileId) + extention;

Check warning on line 43 in packages/pieces/community/google-drive/src/lib/common/get-file-content.ts

View workflow job for this annotation

GitHub Actions / Spell Check with Typos on Changed Files

"extention" should be "extension".
return files.write({
fileName: name,
data: Buffer.from(await download.arrayBuffer()),
});
};

export async function downloadFileFromDrive(auth: OAuth2PropertyValue<OAuth2Props>, files: FilesService, fileId: string, fileName: string | undefined): Promise<string> {
let mimeType = await getMimeType(auth, fileId)

// the google drive API doesn't allowed downloading google documents but we can export them to office formats
if (
[
'application/vnd.google-apps.document',
'application/vnd.google-apps.spreadsheet',
'application/vnd.google-apps.presentation',
].includes(mimeType)
) {
switch (mimeType) {
case 'application/vnd.google-apps.document':
mimeType =
'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
break;
case 'application/vnd.google-apps.spreadsheet':
mimeType =
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
break;
case 'application/vnd.google-apps.presentation':
mimeType =
'application/vnd.openxmlformats-officedocument.presentationml.presentation';
break;
}
return await googledlCall(
`https://www.googleapis.com/drive/v3/files/${fileId}/export?mimeType=${mimeType}`,
auth,
fileId,
files,
fileName
);
} else {
return await googledlCall(
`https://www.googleapis.com/drive/v3/files/${fileId}?alt=media`,
auth,
fileId,
files,
fileName
);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
PiecePropValueSchema,
Property,
createTrigger,
} from '@activepieces/pieces-framework';
import { TriggerStrategy } from '@activepieces/pieces-framework';
Expand All @@ -12,6 +13,7 @@ import {
import dayjs from 'dayjs';
import { googleDriveAuth } from '../..';
import { common } from '../common';
import { downloadFileFromDrive } from '../common/get-file-content';

const polling: Polling<
PiecePropValueSchema<typeof googleDriveAuth>,
Expand Down Expand Up @@ -41,6 +43,12 @@ export const newFile = createTrigger({
props: {
parentFolder: common.properties.parentFolder,
include_team_drives: common.properties.include_team_drives,
include_file_content: Property.Checkbox({
displayName: 'Include File Content',
description: 'Include the file content in the output. This will increase the time taken to fetch the files and might cause issues with large files.',
required: false,
defaultValue: false
}),
},
type: TriggerStrategy.POLLING,
onEnable: async (context) => {
Expand All @@ -58,24 +66,47 @@ export const newFile = createTrigger({
});
},
run: async (context) => {
return await pollingHelper.poll(polling, {
const newFiles = await pollingHelper.poll(polling, {
auth: context.auth,
store: context.store,
propsValue: context.propsValue,
});

return await handleFileContent(newFiles, context)
},
test: async (context) => {
return await pollingHelper.test(polling, {
const newFiles = await pollingHelper.test(polling, {
auth: context.auth,
store: context.store,
propsValue: context.propsValue,
});

return await handleFileContent(newFiles, context)
},

sampleData: {
kind: 'drive#file',
mimeType: 'image/jpeg',
id: '1dpv4-sKJfKRwI9qx1vWqQhEGEn3EpbI5',
name: 'sweep.jpg',
link: 'https://cloud.activepieces.com/api/v1/step-files/signed?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCYm0.Xyoy5nA-S70M9JpRnvadLxUm'
},
});

async function handleFileContent(newFiles: unknown[], context: any) {
const newFilesObj = JSON.parse(JSON.stringify(newFiles))

if (context.propsValue.include_file_content) {
const fileContentPromises: Promise<string>[] = []
for (const file of newFilesObj) {
fileContentPromises.push(downloadFileFromDrive(context.auth, context.files, file["id"], file["name"]));
}

const filesContent = await Promise.all(fileContentPromises)

for (let i = 0; i < newFilesObj.length; i++) {
newFilesObj[i].content = filesContent[i]
}
}
return newFilesObj
}

0 comments on commit 01dad08

Please sign in to comment.