Skip to content

Commit

Permalink
feat: onedrive: set up parent-child relationships for folder and files
Browse files Browse the repository at this point in the history
  • Loading branch information
amuwal committed Dec 18, 2024
1 parent 78f8c71 commit bdb1d20
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export class OnedriveFileMapper implements IFileMapper {
return {
remote_id: file.id,
remote_data: file,
remote_folder_id: file.parentReference?.id,
name: file.name,
file_url: file.webUrl,
mime_type: file.file.mimeType,
Expand Down
75 changes: 49 additions & 26 deletions packages/api/src/filestorage/folder/services/onedrive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { SyncParam } from '@@core/utils/types/interface';
import { IngestDataService } from '@@core/@core-services/unification/ingest-data.service';
import { UnifiedFilestorageFileOutput } from '@filestorage/file/types/model.unified';
import { OnedriveFileOutput } from '@filestorage/file/services/onedrive/types';
import { v4 as uuidv4 } from 'uuid';

@Injectable()
export class OnedriveService implements IFolderService {
Expand Down Expand Up @@ -112,7 +113,17 @@ export class OnedriveService implements IFolderService {

let result: OnedriveFolderOutput[] = [rootFolderData.data];
let depth = 0;
let batch: string[] = [remote_folder_id];
let batch: {
remote_folder_id: string;
internal_id: string | null;
internal_parent_folder_id: string | null;
}[] = [
{
remote_folder_id: remote_folder_id,
internal_id: uuidv4(),
internal_parent_folder_id: null,
},
];

while (batch.length > 0) {
if (depth > 50) {
Expand All @@ -121,37 +132,49 @@ export class OnedriveService implements IFolderService {
}

const nestedFoldersPromises: Promise<OnedriveFolderOutput[]>[] =
batch.map(async (folder_id: string) => {
const childrenConfig: AxiosRequestConfig = {
method: 'get',
url: `${connection.account_url}/v1.0/me/drive/items/${folder_id}/children`,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.cryptoService.decrypt(
connection.access_token,
)}`,
},
};

const resp: AxiosResponse = await this.makeRequestWithRetry(
childrenConfig,
);

const folders: OnedriveFolderOutput[] = resp.data.value.filter(
(driveItem: any) => driveItem.folder,
);

return folders;
});
batch.map(
async (folder: {
remote_folder_id: string;
internal_id: string;
internal_parent_folder_id: string;
}) => {
const childrenConfig: AxiosRequestConfig = {
method: 'get',
url: `${connection.account_url}/v1.0/me/drive/items/${folder.remote_folder_id}/children`,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.cryptoService.decrypt(
connection.access_token,
)}`,
},
};

const resp: AxiosResponse = await this.makeRequestWithRetry(
childrenConfig,
);

const folders: OnedriveFolderOutput[] = resp.data.value.filter(
(driveItem: any) => driveItem.folder,
);

return folders.map((f: OnedriveFolderOutput) => ({
...f,
internal_id: uuidv4(),
internal_parent_folder_id: folder.internal_id,
}));
},
);

const nestedFolders: OnedriveFolderOutput[][] = await Promise.all(
nestedFoldersPromises,
);

result = result.concat(nestedFolders.flat());
batch = nestedFolders
.flat()
.map((folder: OnedriveFolderOutput) => folder.id);
batch = nestedFolders.flat().map((folder: OnedriveFolderOutput) => ({
remote_folder_id: folder.id,
internal_id: folder.internal_id,
internal_parent_folder_id: folder.internal_parent_folder_id,
}));
this.logger.log(`Batch size: ${batch.length} at depth ${depth}`);
depth++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,14 @@ export class OnedriveFolderMapper implements IFolderMapper {
}

const result = {
id: folder.internal_id ?? null,
parent_folder_id: folder.internal_parent_folder_id ?? null,
remote_id: folder.id,
remote_data: folder,
name: folder.name,
folder_url: folder.webUrl,
description: folder.description,
drive_id: null,
parent_folder_id: await this.utils.getFolderIdFromRemote(
folder.parentReference?.id,
connectionId,
),
// permission: opts.permissions?.[0] || null,
permissions: [],
size: folder.size.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export interface OnedriveFolderInput {
readonly lastModifiedBy?: IdentitySet;
/** If this property is non-null, it indicates that the driveItem is the top-most driveItem in the drive. */
readonly root?: any;

// internal fields
internal_id?: string;
internal_parent_folder_id?: string;
}

/**
Expand Down

0 comments on commit bdb1d20

Please sign in to comment.