Skip to content

Commit

Permalink
feat: onedrive: add incremental sync for folders
Browse files Browse the repository at this point in the history
  • Loading branch information
amuwal committed Dec 19, 2024
1 parent c0d9375 commit 019ce08
Show file tree
Hide file tree
Showing 4 changed files with 346 additions and 25 deletions.
24 changes: 19 additions & 5 deletions packages/api/src/filestorage/folder/services/googledrive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ interface GoogleDriveListResponse {
};
}

const GOOGLE_DRIVE_QUOTA_DELAY = 100; // ms between requests
const MAX_RETRIES = 3;
const INITIAL_BACKOFF = 1000; // 1 second
const RATE_LIMIT_DELAY = 100; // ms between requests to avoid quota issues
const MAX_API_RETRIES = 3;
const BASE_BACKOFF_MS = 1000;
Expand Down Expand Up @@ -439,7 +436,10 @@ export class GoogleDriveFolderService implements IFolderService {
);

if (internalParentId) {
const folder_internal_id = uuidv4();
const folder_internal_id = await this.getIntenalIdForFolder(
folder.id,
connectionId,
);
foldersToSync.push(
this.createFolderWithInternalIds(
folder,
Expand All @@ -462,6 +462,7 @@ export class GoogleDriveFolderService implements IFolderService {
foldersToSync,
remote_folders,
parentLookupCache,
folderIdToInternalIdMap,
driveIds,
connectionId,
drive,
Expand All @@ -475,6 +476,17 @@ export class GoogleDriveFolderService implements IFolderService {
return foldersToSync;
}

private async getIntenalIdForFolder(
folderId: string,
connectionId: string,
): Promise<string> {
const folder = await this.prisma.fs_folders.findFirst({
where: { remote_id: folderId, id_connection: connectionId },
select: { id_fs_folder: true },
});
return folder?.id_fs_folder || uuidv4();
}

private createFolderWithInternalIds(
folder: GoogleDriveFolderOutput,
internalParentId: string,
Expand All @@ -500,6 +512,7 @@ export class GoogleDriveFolderService implements IFolderService {
output: GoogleDriveFolderOutput[],
remote_folders: Map<string, GoogleDriveFolderOutput>,
parentLookupCache: Map<string, string | null>,
idCache: Map<string, string | null>,
driveIds: string[],
connectionId: string,
drive: any,
Expand All @@ -524,7 +537,7 @@ export class GoogleDriveFolderService implements IFolderService {
// Check cache first
const internal_parent_id = await this.resolveParentId(
remote_parent_id,
parentLookupCache,
idCache,
driveIds,
connectionId,
parentLookupCache,
Expand Down Expand Up @@ -565,6 +578,7 @@ export class GoogleDriveFolderService implements IFolderService {
pending.map(async (folder) => {
const internal_parent_id = await getInternalParentRecursive(folder);
const folder_internal_id = uuidv4();
idCache.set(folder.id, folder_internal_id);
output.push({
...folder,
internal_parent_folder_id: internal_parent_id,
Expand Down
Loading

0 comments on commit 019ce08

Please sign in to comment.