Skip to content

Commit

Permalink
feat(google-drive): drive dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
kishanprmr committed May 7, 2024
1 parent 67b48fb commit 598e594
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 144 deletions.
8 changes: 7 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{
"singleQuote": true
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"useTabs": true,
"bracketSpacing": true,
"trailingComma": "all",
"semi": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ export const googleDriveCreateNewFolder = createAction({
description: 'Create a new empty folder in your Google Drive',
displayName: 'Create new folder',
props: {
driveId: common.properties.driveId,
folderName: Property.ShortText({
displayName: 'Folder name',
description: 'The name of the new folder',
required: true,
}),
parentFolder: common.properties.parentFolder,
include_team_drives: common.properties.include_team_drives,
// include_team_drives: common.properties.include_team_drives,
},
async run(context) {
const body: Record<string, string | string[] | undefined> = {
Expand Down
311 changes: 169 additions & 142 deletions packages/pieces/community/google-drive/src/lib/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,150 +1,177 @@
import {
httpClient,
HttpMethod,
AuthenticationType,
HttpRequest,
httpClient,
HttpMethod,
AuthenticationType,
HttpRequest,
} from '@activepieces/pieces-common';
import { Property, OAuth2PropertyValue } from '@activepieces/pieces-framework';
import {
Property,
OAuth2PropertyValue,
PiecePropValueSchema,
DropdownOption,
} from '@activepieces/pieces-framework';
import dayjs from 'dayjs';
import { OAuth2Client } from 'googleapis-common';
import { google } from 'googleapis';
import { googleDriveAuth } from '@activepieces/piece-google-drive';

export const common = {
properties: {
parentFolder: Property.Dropdown({
displayName: 'Parent Folder',
required: false,
refreshers: ['include_team_drives'],
options: async ({ auth, include_team_drives }) => {
if (!auth) {
return {
disabled: true,
options: [],
placeholder: 'Please authenticate first',
};
}
const authProp: OAuth2PropertyValue = auth as OAuth2PropertyValue;
let folders: { id: string; name: string }[] = [];
let pageToken = null;
do {
const request: HttpRequest = {
method: HttpMethod.GET,
url: `https://www.googleapis.com/drive/v3/files`,
queryParams: {
q: "mimeType='application/vnd.google-apps.folder' and trashed = false",
includeItemsFromAllDrives: include_team_drives ? 'true' : 'false',
supportsAllDrives: 'true',
},
authentication: {
type: AuthenticationType.BEARER_TOKEN,
token: authProp!['access_token'],
},
};
if (pageToken) {
if (request.queryParams !== undefined) {
request.queryParams['pageToken'] = pageToken;
}
}
try {
const response = await httpClient.sendRequest<{
files: { id: string; name: string }[];
nextPageToken: string;
}>(request);
folders = folders.concat(response.body.files);
pageToken = response.body.nextPageToken;
} catch (e) {
throw new Error(`Failed to get folders\nError:${e}`);
}
} while (pageToken);

return {
disabled: false,
options: folders.map((folder: { id: string; name: string }) => {
return {
label: folder.name,
value: folder.id,
};
}),
};
},
}),
include_team_drives: Property.Checkbox({
displayName: 'Include Team Drives',
description:
'Determines if folders from Team Drives should be included in the results.',
defaultValue: false,
required: false,
}),
},

async getFiles(
auth: OAuth2PropertyValue,
search?: {
parent?: string;
createdTime?: string | number | Date;
createdTimeOp?: string;
includeTeamDrive?: boolean;
},
order?: string
) {
const authClient = new OAuth2Client();
authClient.setCredentials(auth);

const drive = google.drive({ version: 'v3', auth: authClient });

const q: string[] = [];
if (search?.parent) q.push(`'${search.parent}' in parents`);
if (search?.createdTime)
q.push(
`createdTime ${search.createdTimeOp ?? '>'} '${dayjs(
search.createdTime
).format()}'`
);
q.push(`trashed = false`);
const response = await drive.files.list({
q: q.concat("mimeType!='application/vnd.google-apps.folder'").join(' and '),
fields: 'files(id, name, mimeType, webViewLink, kind)',
orderBy: order ?? 'createdTime asc',
supportsAllDrives: true,
includeItemsFromAllDrives: search?.includeTeamDrive,
});

return response.data.files;
},

async getFolders(
auth: OAuth2PropertyValue,
search?: {
parent?: string;
createdTime?: string | number | Date;
createdTimeOp?: string;
},
order?: string
) {
const q: string[] = [`mimeType='application/vnd.google-apps.folder'`];
if (search?.parent) q.push(`'${search.parent}' in parents`);
if (search?.createdTime)
q.push(
`createdTime ${search.createdTimeOp ?? '>'} '${dayjs(
search.createdTime
).format()}'`
);
q.push(`trashed = false`);
const response = await httpClient.sendRequest<{
files: { id: string; name: string }[];
}>({
method: HttpMethod.GET,
url: `https://www.googleapis.com/drive/v3/files`,
queryParams: {
q: q.join(' and '),
orderBy: order ?? 'createdTime asc',
},
authentication: {
type: AuthenticationType.BEARER_TOKEN,
token: auth.access_token,
},
});

return response.body.files;
},
properties: {
driveId: Property.Dropdown({
displayName: 'Drive',
refreshers: [],
required: true,
defaultValue: 'My Drive',
options: async ({ auth }) => {
if (!auth) {
return {
disabled: true,
options: [],
placeholder: 'Please connect account first.',
};
}

const authValue = auth as PiecePropValueSchema<typeof googleDriveAuth>;

const authClient = new OAuth2Client();
authClient.setCredentials(authValue);

const drive = google.drive({ version: 'v3', auth: authClient });
const options: DropdownOption<string>[] = [{ label: 'My Drive', value: 'My Drive' }];

let pageToken;
do {
const { data } = await drive.drives.list();
pageToken = data.nextPageToken;

if (data.drives) {
for (const drive of data.drives) {
options.push({ label: drive.name!, value: drive.id! });
}
}
} while (pageToken);

return {
disabled: false,
options,
};
},
}),
parentFolder: Property.Dropdown({
displayName: 'Parent Folder',
required: false,
refreshers: ['driveId'],
options: async ({ auth, driveId }) => {
if (!auth || !driveId) {
return {
disabled: true,
options: [],
placeholder: 'Please authenticate first',
};
}
const authValue = auth as PiecePropValueSchema<typeof googleDriveAuth>;
const driveValue = driveId as string;

const authClient = new OAuth2Client();
authClient.setCredentials(authValue);

const drive = google.drive({ version: 'v3', auth: authClient });

let options: DropdownOption<string>[] = [];
let pageToken;

do {
const { data } = await drive.files.list({
q: "mimeType='application/vnd.google-apps.folder' and trashed = false",
fields: 'nextPageToken,files(id,name)',
includeItemsFromAllDrives: true,
supportsAllDrives: true,
driveId: driveValue === 'My Drive' ? undefined : driveValue,
});

if (data.files) {
for (const folder of data.files) {
options.push({ label: folder.name!, value: folder.id! });
}
}

pageToken = data.nextPageToken;
} while (pageToken);

return {
disabled: false,
options,
};
},
}),
include_team_drives: Property.Checkbox({
displayName: 'Include Team Drives',
description: 'Determines if folders from Team Drives should be included in the results.',
defaultValue: false,
required: false,
}),
},

async getFiles(
auth: OAuth2PropertyValue,
search?: {
parent?: string;
createdTime?: string | number | Date;
createdTimeOp?: string;
includeTeamDrive?: boolean;
},
order?: string,
) {
const authClient = new OAuth2Client();
authClient.setCredentials(auth);

const drive = google.drive({ version: 'v3', auth: authClient });

const q: string[] = [];
if (search?.parent) q.push(`'${search.parent}' in parents`);
if (search?.createdTime)
q.push(`createdTime ${search.createdTimeOp ?? '>'} '${dayjs(search.createdTime).format()}'`);
q.push(`trashed = false`);
const response = await drive.files.list({
q: q.concat("mimeType!='application/vnd.google-apps.folder'").join(' and '),
fields: 'files(id, name, mimeType, webViewLink, kind)',
orderBy: order ?? 'createdTime asc',
supportsAllDrives: true,
includeItemsFromAllDrives: search?.includeTeamDrive,
});

return response.data.files;
},

async getFolders(
auth: OAuth2PropertyValue,
search?: {
parent?: string;
createdTime?: string | number | Date;
createdTimeOp?: string;
},
order?: string,
) {
const q: string[] = [`mimeType='application/vnd.google-apps.folder'`];
if (search?.parent) q.push(`'${search.parent}' in parents`);
if (search?.createdTime)
q.push(`createdTime ${search.createdTimeOp ?? '>'} '${dayjs(search.createdTime).format()}'`);
q.push(`trashed = false`);
const response = await httpClient.sendRequest<{
files: { id: string; name: string }[];
}>({
method: HttpMethod.GET,
url: `https://www.googleapis.com/drive/v3/files`,
queryParams: {
q: q.join(' and '),
orderBy: order ?? 'createdTime asc',
},
authentication: {
type: AuthenticationType.BEARER_TOKEN,
token: auth.access_token,
},
});

return response.body.files;
},
};

0 comments on commit 598e594

Please sign in to comment.