Skip to content

Commit

Permalink
fix: googledrive: missing drive ids
Browse files Browse the repository at this point in the history
  • Loading branch information
amuwal committed Dec 22, 2024
1 parent fee0ae2 commit f3ed35d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/api/src/filestorage/file/services/googledrive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ export class GoogleDriveService implements IFileService {
}
});

await this.assignDriveIds(sourceData, drive);

// Ingest files with updated permissions
const syncedFiles = await this.ingestService.ingestData<
UnifiedFilestorageFileOutput,
Expand Down Expand Up @@ -382,6 +384,32 @@ export class GoogleDriveService implements IFileService {
);
}

/**
* Assigns driveId as root to files that don't have one.
* @param files Array of Google Drive files.
* @param auth OAuth2 client for Google Drive API.
* @returns Array of Google Drive files with driveId assigned.
*/
private async assignDriveIds(
files: GoogleDriveFileOutput[],
drive: ReturnType<typeof google.drive>,
): Promise<GoogleDriveFileOutput[]> {
const rootDriveId = await this.rateLimitedRequest(() =>
drive.files
.get({
fileId: 'root',
fields: 'id',
})
.then((res) => res.data.id),
);

files.forEach((file) => {
file.driveId = file.driveId || rootDriveId;
});

return files;
}

private async fetchPermissions(
permissionIds: string[],
files: GoogleDriveFileOutput[],
Expand Down
29 changes: 29 additions & 0 deletions packages/api/src/filestorage/folder/services/googledrive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ export class GoogleDriveFolderService implements IFolderService {
connectionId,
);

await this.assignDriveIds(modifiedFolders, auth);

return await this.assignParentIds(
modifiedFolders,
connectionId,
Expand All @@ -415,6 +417,33 @@ export class GoogleDriveFolderService implements IFolderService {
}
}

/**
* Assigns driveId as root to folders that don't have one.
* @param folders Array of Google Drive folders.
* @param auth OAuth2 client for Google Drive API.
* @returns Array of Google Drive folders with driveId assigned.
*/
private async assignDriveIds(
folders: GoogleDriveFolderOutput[],
auth: OAuth2Client,
): Promise<GoogleDriveFolderOutput[]> {
const drive = google.drive({ version: 'v3', auth });
const rootDriveId = await this.executeWithRetry(() =>
drive.files
.get({
fileId: 'root',
fields: 'id',
})
.then((res) => res.data.id),
);

folders.forEach((folder) => {
folder.driveId = folder.driveId || rootDriveId;
});

return folders;
}

/**
* Assigns parent IDs to folders based on their parents.
* Handles circular references and orphaned folders.
Expand Down

0 comments on commit f3ed35d

Please sign in to comment.