Skip to content

Commit

Permalink
Properly handle remotely deleted files for Gdrive (#2011)
Browse files Browse the repository at this point in the history
  • Loading branch information
lasryaric authored Oct 9, 2023
1 parent 6a0e260 commit f1603a6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 66 deletions.
62 changes: 27 additions & 35 deletions connectors/src/connectors/google_drive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,41 +325,33 @@ export async function retrieveGoogleDriveConnectorPermissions({
},
});

const driveClient = await getDriveClient(authCredentials);

const resources: ConnectorResource[] = await Promise.all(
folders.map(async (f): Promise<ConnectorResource> => {
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<ConnectorResource | null> => {
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 {
Expand Down
53 changes: 22 additions & 31 deletions connectors/src/connectors/google_drive/temporal/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
}
});
})
Expand Down

0 comments on commit f1603a6

Please sign in to comment.