-
Notifications
You must be signed in to change notification settings - Fork 962
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4342 from AbdullahBitar/main
feat(google-drive): allow new file trigger to include content
- Loading branch information
Showing
4 changed files
with
127 additions
and
76 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.21" | ||
"version": "0.5.22" | ||
} |
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
91 changes: 91 additions & 0 deletions
91
packages/pieces/community/google-drive/src/lib/common/get-file-content.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 |
---|---|---|
@@ -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); | ||
const srcFileName = fileName; | ||
const name = | ||
(srcFileName | ||
? srcFileName.replace(new RegExp(extention + '$'), '') | ||
: fileId) + extention; | ||
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 | ||
); | ||
} | ||
} |
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