-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fix/identify-lock
- Loading branch information
Showing
6 changed files
with
191 additions
and
123 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
integration-templates/google-drive/actions/fetch-document.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,70 @@ | ||
import type { NangoAction, ProxyConfiguration } from '../../models'; | ||
import type { GoogleDriveFileResponse } from '../types.js'; | ||
import { mimeTypeMapping } from '../types.js'; | ||
|
||
/** | ||
* Retrieves and returns the content of a Google Drive file as a base64-encoded string. | ||
* | ||
* For detailed endpoint documentation, refer to: | ||
* | ||
* https://developers.google.com/drive/api/reference/rest/v3/files/get | ||
* https://developers.google.com/drive/api/reference/rest/v3/files/export | ||
* @param nango - An instance of NangoAction used for making API requests. | ||
* @param input - The ID of the file to be retrieved, provided as a string. | ||
* @returns The base64-encoded content of the file. | ||
* @throws Error if the input is invalid, or if the file metadata or content retrieval fails. | ||
*/ | ||
export default async function runAction(nango: NangoAction, input: string): Promise<string> { | ||
if (!input || typeof input !== 'string') { | ||
throw new Error('Missing or invalid input: a file ID is required and should be a string'); | ||
} | ||
|
||
// Fetch the file metadata first to get the MIME type | ||
const Config: ProxyConfiguration = { | ||
endpoint: `drive/v3/files/${input}`, | ||
params: { | ||
fields: 'id, name, mimeType' | ||
} | ||
}; | ||
const fileMetadataResponse = await nango.get<GoogleDriveFileResponse>(Config); | ||
|
||
if (fileMetadataResponse.status !== 200 || !fileMetadataResponse.data) { | ||
throw new Error(`Failed to retrieve file metadata: Status Code ${fileMetadataResponse.status}`); | ||
} | ||
|
||
const file = fileMetadataResponse.data; | ||
const mimeTypeDetails = mimeTypeMapping[file.mimeType]; | ||
|
||
if (!mimeTypeDetails) { | ||
throw new Error(`Unsupported MIME type: ${file.mimeType}`); | ||
} | ||
|
||
const { mimeType: exportMimeType, responseType } = mimeTypeDetails; | ||
|
||
await nango.log('Fetching document of ', { exportMimeType }); | ||
|
||
const endpoint = responseType === 'text' ? `drive/v3/files/${file.id}/export` : `drive/v3/files/${file.id}`; | ||
const params = responseType === 'text' ? { mimeType: exportMimeType } : { alt: 'media' }; | ||
|
||
const config: ProxyConfiguration = { | ||
endpoint, | ||
params, | ||
responseType | ||
}; | ||
const response = await nango.get(config); | ||
|
||
if (response.status !== 200) { | ||
throw new Error(`Failed to retrieve file content: Status Code ${response.status}`); | ||
} | ||
|
||
if (responseType === 'text') { | ||
return response.data ?? ''; | ||
} else { | ||
const chunks: Buffer[] = []; | ||
for await (const chunk of response.data) { | ||
chunks.push(chunk); | ||
} | ||
const buffer = Buffer.concat(chunks); | ||
return buffer.toString('base64'); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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,52 @@ | ||
export interface GoogleDriveFileResponse { | ||
id: string; | ||
name: string; | ||
mimeType: string; | ||
webViewLink: string; | ||
} | ||
|
||
export interface Metadata { | ||
files?: string[]; | ||
folders?: string[]; | ||
} | ||
|
||
interface MimeTypeMapping { | ||
mimeType: string; | ||
responseType: 'text' | 'stream'; | ||
} | ||
|
||
export const mimeTypeMapping: Record<string, MimeTypeMapping> = { | ||
// Documents | ||
'application/vnd.google-apps.document': { mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', responseType: 'text' }, | ||
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': { | ||
mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', | ||
responseType: 'stream' | ||
}, | ||
'application/vnd.oasis.opendocument.text': { mimeType: 'application/vnd.oasis.opendocument.text', responseType: 'stream' }, | ||
'application/rtf': { mimeType: 'application/rtf', responseType: 'stream' }, | ||
'text/plain': { mimeType: 'text/plain', responseType: 'stream' }, | ||
// Spreadsheets | ||
'application/vnd.google-apps.spreadsheet': { mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', responseType: 'text' }, | ||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': { | ||
mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', | ||
responseType: 'stream' | ||
}, | ||
'application/vnd.oasis.opendocument.spreadsheet': { mimeType: 'application/vnd.oasis.opendocument.spreadsheet', responseType: 'stream' }, | ||
// PDFs | ||
'application/pdf': { mimeType: 'application/pdf', responseType: 'stream' }, | ||
// Text Files | ||
'text/csv': { mimeType: 'text/csv', responseType: 'text' }, | ||
'text/tab-separated-values': { mimeType: 'text/tab-separated-values', responseType: 'text' }, | ||
// Presentations | ||
'application/vnd.google-apps.presentation': { mimeType: 'application/vnd.openxmlformats-officedocument.presentationml.presentation', responseType: 'text' }, | ||
'application/vnd.openxmlformats-officedocument.presentationml.presentation': { | ||
mimeType: 'application/vnd.openxmlformats-officedocument.presentationml.presentation', | ||
responseType: 'stream' | ||
}, | ||
'application/vnd.oasis.opendocument.presentation': { mimeType: 'application/vnd.oasis.opendocument.presentation', responseType: 'stream' }, | ||
// Drawings and Images | ||
'application/vnd.google-apps.drawing': { mimeType: 'image/jpeg', responseType: 'stream' }, | ||
'image/jpeg': { mimeType: 'image/jpeg', responseType: 'stream' }, | ||
'image/png': { mimeType: 'image/png', responseType: 'stream' }, | ||
'image/svg+xml': { mimeType: 'image/svg+xml', responseType: 'stream' } | ||
}; |
Oops, something went wrong.