diff --git a/packages/api/src/filestorage/file/services/googledrive/index.ts b/packages/api/src/filestorage/file/services/googledrive/index.ts index 340d04705..c47f00621 100644 --- a/packages/api/src/filestorage/file/services/googledrive/index.ts +++ b/packages/api/src/filestorage/file/services/googledrive/index.ts @@ -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, @@ -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, + ): Promise { + 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[], diff --git a/packages/api/src/filestorage/folder/services/googledrive/index.ts b/packages/api/src/filestorage/folder/services/googledrive/index.ts index e4cc56b3b..56b927cf2 100644 --- a/packages/api/src/filestorage/folder/services/googledrive/index.ts +++ b/packages/api/src/filestorage/folder/services/googledrive/index.ts @@ -403,6 +403,8 @@ export class GoogleDriveFolderService implements IFolderService { connectionId, ); + await this.assignDriveIds(modifiedFolders, auth); + return await this.assignParentIds( modifiedFolders, connectionId, @@ -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 { + 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.