diff --git a/connectors/src/connectors/google_drive/index.ts b/connectors/src/connectors/google_drive/index.ts index b3a4226aae9e..6568e489b43a 100644 --- a/connectors/src/connectors/google_drive/index.ts +++ b/connectors/src/connectors/google_drive/index.ts @@ -325,41 +325,33 @@ export async function retrieveGoogleDriveConnectorPermissions({ }, }); - const driveClient = await getDriveClient(authCredentials); - - const resources: ConnectorResource[] = await Promise.all( - folders.map(async (f): Promise => { - const folder = await driveClient.files.get({ - fileId: f.folderId, - supportsAllDrives: true, - fields: "id, name, webViewLink, driveId", - }); - const fd = folder.data; - if (fd.driveId === f.folderId) { - const d = await driveClient.drives.get({ - driveId: f.folderId, - }); - fd.name = d.data.name; - } - return { - provider: c.type, - internalId: f.folderId, - parentInternalId: null, - type: "folder", - title: fd.name || "", - sourceUrl: fd.webViewLink || null, - expandable: - (await GoogleDriveFiles.count({ - where: { - connectorId: connectorId, - parentId: f.folderId, - mimeType: "application/vnd.google-apps.folder", - }, - })) > 0, - permission: "read", - }; - }) - ); + const resources = ( + await Promise.all( + folders.map(async (f): Promise => { + const fd = await getGoogleDriveObject(authCredentials, f.folderId); + if (!fd) { + return null; + } + return { + provider: c.type, + internalId: f.folderId, + parentInternalId: null, + type: "folder", + title: fd.name || "", + sourceUrl: fd.webViewLink || null, + expandable: + (await GoogleDriveFiles.count({ + where: { + connectorId: connectorId, + parentId: f.folderId, + mimeType: "application/vnd.google-apps.folder", + }, + })) > 0, + permission: "read", + }; + }) + ) + ).flatMap((f) => (f ? [f] : [])); return new Ok(resources); } else { diff --git a/connectors/src/connectors/google_drive/temporal/activities.ts b/connectors/src/connectors/google_drive/temporal/activities.ts index ba259e5a1eb0..27b3a172d0a2 100644 --- a/connectors/src/connectors/google_drive/temporal/activities.ts +++ b/connectors/src/connectors/google_drive/temporal/activities.ts @@ -753,37 +753,28 @@ export async function garbageCollector( await Promise.all( files.map(async (file) => { return queue.add(async () => { - try { - const driveFile = await getGoogleDriveObject( - authCredentials, - file.driveFileId - ); - if (!driveFile) { - throw new Error( - `File ${file.driveFileId} unexpectedly not found (got 404).` - ); - } - const isInFolder = await objectIsInFolders( - connectorId, - authCredentials, - driveFile, - selectedFolders, - lastSeenTs - ); - if (isInFolder === false || driveFile.trashed) { - await deleteOneFile(connectorId, driveFile); - } else { - await file.update({ - lastSeenTs: new Date(), - }); - } - } catch (e) { - if (e instanceof GaxiosError) { - if (e.response?.status === 404) { - // File not found, we can delete it. - await file.destroy(); - } - } + const driveFile = await getGoogleDriveObject( + authCredentials, + file.driveFileId + ); + if (!driveFile) { + // Could not find the file on Gdrive, deleting our local reference to it. + await file.destroy(); + return null; + } + const isInFolder = await objectIsInFolders( + connectorId, + authCredentials, + driveFile, + selectedFolders, + lastSeenTs + ); + if (isInFolder === false || driveFile.trashed) { + await deleteOneFile(connectorId, driveFile); + } else { + await file.update({ + lastSeenTs: new Date(), + }); } }); })