forked from activepieces/activepieces
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(google-drive): add missing option to support team drives for dupl…
…icate file action
- Loading branch information
1 parent
362638b
commit c2005f1
Showing
4 changed files
with
146 additions
and
116 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,4 +1,4 @@ | ||
{ | ||
"name": "@activepieces/piece-google-drive", | ||
"version": "0.5.19" | ||
} | ||
"version": "0.5.20" | ||
} |
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
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
245 changes: 136 additions & 109 deletions
245
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,123 +1,150 @@ | ||
import { httpClient, HttpMethod, AuthenticationType, HttpRequest } from "@activepieces/pieces-common"; | ||
import { Property, OAuth2PropertyValue } from "@activepieces/pieces-framework"; | ||
import dayjs from "dayjs"; | ||
import { | ||
httpClient, | ||
HttpMethod, | ||
AuthenticationType, | ||
HttpRequest, | ||
} from '@activepieces/pieces-common'; | ||
import { Property, OAuth2PropertyValue } from '@activepieces/pieces-framework'; | ||
import dayjs from 'dayjs'; | ||
|
||
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 | ||
}, order?: string) { | ||
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 httpClient.sendRequest<{ files: { id: string, name: string }[] }>({ | ||
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: q.join(' and '), | ||
fields: 'files(id, name, mimeType, webViewLink, kind)', | ||
orderBy: order ?? 'createdTime asc' | ||
q: "mimeType='application/vnd.google-apps.folder' and trashed = false", | ||
includeItemsFromAllDrives: include_team_drives ? 'true' : 'false', | ||
supportsAllDrives: 'true', | ||
}, | ||
authentication: { | ||
type: AuthenticationType.BEARER_TOKEN, | ||
token: auth.access_token, | ||
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, | ||
}), | ||
}, | ||
|
||
return response.body.files; | ||
async getFiles( | ||
auth: OAuth2PropertyValue, | ||
search?: { | ||
parent?: string; | ||
createdTime?: string | number | Date; | ||
createdTimeOp?: string; | ||
}, | ||
order?: string | ||
) { | ||
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 httpClient.sendRequest<{ | ||
files: { id: string; name: string }[]; | ||
}>({ | ||
method: HttpMethod.GET, | ||
url: `https://www.googleapis.com/drive/v3/files`, | ||
queryParams: { | ||
q: q.join(' and '), | ||
fields: 'files(id, name, mimeType, webViewLink, kind)', | ||
orderBy: order ?? 'createdTime asc', | ||
}, | ||
authentication: { | ||
type: AuthenticationType.BEARER_TOKEN, | ||
token: auth.access_token, | ||
}, | ||
}); | ||
|
||
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; | ||
}, | ||
|
||
return response.body.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; | ||
}, | ||
}; |