-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67b48fb
commit 598e594
Showing
3 changed files
with
178 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
311 changes: 169 additions & 142 deletions
311
packages/pieces/community/google-drive/src/lib/common/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}; |