From 6547159e3b489debc67420fb7e77ea923b309054 Mon Sep 17 00:00:00 2001 From: mit <1mitccc@gmail.com> Date: Sun, 8 Sep 2024 18:39:47 +0530 Subject: [PATCH] Integrate sharedlink and permissions in file and folder objects in one-drive integration --- packages/api/src/@core/sync/sync.service.ts | 1 + .../types/original/original.file-storage.ts | 48 +++++++++++++++---- .../drive/services/onedrive/index.ts | 2 +- .../file/services/onedrive/index.ts | 18 +++++++ .../file/services/onedrive/mappers.ts | 39 +++++++++++++-- .../folder/services/onedrive/index.ts | 40 +++++++++++++--- .../folder/services/onedrive/mappers.ts | 35 ++++++++++++-- .../sharedlink/sharedlink.module.ts | 7 ++- 8 files changed, 162 insertions(+), 28 deletions(-) diff --git a/packages/api/src/@core/sync/sync.service.ts b/packages/api/src/@core/sync/sync.service.ts index f3b870096..633a50c94 100644 --- a/packages/api/src/@core/sync/sync.service.ts +++ b/packages/api/src/@core/sync/sync.service.ts @@ -425,6 +425,7 @@ export class CoreSyncService { try { await task(); } catch (error) { + console.log(error); this.logger.error(`File Task failed: ${error.message}`, error); } } diff --git a/packages/api/src/@core/utils/types/original/original.file-storage.ts b/packages/api/src/@core/utils/types/original/original.file-storage.ts index 415a259e7..58e1c1de7 100644 --- a/packages/api/src/@core/utils/types/original/original.file-storage.ts +++ b/packages/api/src/@core/utils/types/original/original.file-storage.ts @@ -1,17 +1,41 @@ -import { BoxSharedlinkInput, BoxSharedlinkOutput } from '@filestorage/sharedlink/services/box/types'; -import { OnedriveSharedlinkInput, OnedriveSharedlinkOutput } from '@filestorage/sharedlink/services/onedrive/types'; +import { + BoxSharedLinkInput, + BoxSharedLinkOutput, +} from '@filestorage/sharedlink/services/box/types'; +import { + OnedriveSharedLinkInput, + OnedriveSharedLinkOutput, +} from '@filestorage/sharedlink/services/onedrive/types'; -import { OnedrivePermissionInput, OnedrivePermissionOutput } from '@filestorage/permission/services/onedrive/types'; +import { + OnedrivePermissionInput, + OnedrivePermissionOutput, +} from '@filestorage/permission/services/onedrive/types'; -import { OnedriveGroupInput, OnedriveGroupOutput } from '@filestorage/group/services/onedrive/types'; +import { + OnedriveGroupInput, + OnedriveGroupOutput, +} from '@filestorage/group/services/onedrive/types'; -import { OnedriveUserInput, OnedriveUserOutput } from '@filestorage/user/services/onedrive/types'; +import { + OnedriveUserInput, + OnedriveUserOutput, +} from '@filestorage/user/services/onedrive/types'; -import { OnedriveFileInput, OnedriveFileOutput } from '@filestorage/file/services/onedrive/types'; +import { + OnedriveFileInput, + OnedriveFileOutput, +} from '@filestorage/file/services/onedrive/types'; -import { OnedriveFolderInput, OnedriveFolderOutput } from '@filestorage/folder/services/onedrive/types'; +import { + OnedriveFolderInput, + OnedriveFolderOutput, +} from '@filestorage/folder/services/onedrive/types'; -import { OnedriveDriveInput, OnedriveDriveOutput } from '@filestorage/drive/services/onedrive/types'; +import { + OnedriveDriveInput, + OnedriveDriveOutput, +} from '@filestorage/drive/services/onedrive/types'; /* INPUT */ @@ -94,6 +118,10 @@ export type FileStorageObjectOutput = | OriginalGroupOutput | OriginalUserOutput; -export type OriginalSharedlinkInput = BoxSharedlinkInput | OnedriveSharedlinkInput; +export type OriginalSharedlinkInput = + | BoxSharedLinkInput + | OnedriveSharedLinkInput; -export type OriginalSharedlinkOutput = BoxSharedlinkOutput | OnedriveSharedlinkOutput; +export type OriginalSharedlinkOutput = + | BoxSharedLinkOutput + | OnedriveSharedLinkOutput; diff --git a/packages/api/src/filestorage/drive/services/onedrive/index.ts b/packages/api/src/filestorage/drive/services/onedrive/index.ts index b36303e19..4d6229441 100644 --- a/packages/api/src/filestorage/drive/services/onedrive/index.ts +++ b/packages/api/src/filestorage/drive/services/onedrive/index.ts @@ -60,7 +60,7 @@ export class OnedriveService implements IDriveService { return { data: drives, - message: 'Onedrive drive retrived', + message: 'Onedrive drives retrived', statusCode: 200, }; } catch (error) { diff --git a/packages/api/src/filestorage/file/services/onedrive/index.ts b/packages/api/src/filestorage/file/services/onedrive/index.ts index cd1c95a62..3895ed570 100644 --- a/packages/api/src/filestorage/file/services/onedrive/index.ts +++ b/packages/api/src/filestorage/file/services/onedrive/index.ts @@ -61,6 +61,24 @@ export class OnedriveService implements IFileService { (elem) => !elem.folder, // files don't have a folder property ); + // Add permission shared link is also included in permissions in one-drive) + await Promise.all( + files.map(async (driveItem) => { + const resp = await axios.get( + `${connection.account_url}/v1.0/drive/items/${driveItem.id}/permissions`, + { + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${this.cryptoService.decrypt( + connection.access_token, + )}`, + }, + }, + ); + driveItem.permissions = resp.data.value; + }), + ); + this.logger.log(`Synced onedrive files !`); return { data: files, diff --git a/packages/api/src/filestorage/file/services/onedrive/mappers.ts b/packages/api/src/filestorage/file/services/onedrive/mappers.ts index f3c9d744e..0d45fefc0 100644 --- a/packages/api/src/filestorage/file/services/onedrive/mappers.ts +++ b/packages/api/src/filestorage/file/services/onedrive/mappers.ts @@ -1,6 +1,9 @@ import { MappersRegistry } from '@@core/@core-services/registries/mappers.registry'; import { CoreUnification } from '@@core/@core-services/unification/core-unification.service'; -import { OriginalSharedLinkOutput } from '@@core/utils/types/original/original.file-storage'; +import { + OriginalPermissionOutput, + OriginalSharedLinkOutput, +} from '@@core/utils/types/original/original.file-storage'; import { FileStorageObject } from '@filestorage/@lib/@types'; import { Utils } from '@filestorage/@lib/@utils'; import { IFileMapper } from '@filestorage/file/types'; @@ -85,8 +88,35 @@ export class OnedriveFileMapper implements IFileMapper { } } - // todo: handle shared link - // todo: handle permission + const opts: any = {}; + if (file.permissions.length) { + const permissions = await this.coreUnificationService.unify< + OriginalPermissionOutput[] + >({ + sourceObject: file.permissions, + targetType: FileStorageObject.permission, + providerName: 'onedrive', + vertical: 'filestorage', + connectionId, + customFieldMappings: [], + }); + opts.permissions = permissions; + + // shared link + if (file.permissions.some((p) => p.link)) { + const sharedLinks = + await this.coreUnificationService.unify({ + sourceObject: file.permissions.find((p) => p.link), + targetType: FileStorageObject.sharedlink, + providerName: 'onedrive', + vertical: 'filestorage', + connectionId, + customFieldMappings: [], + }); + opts.shared_links = sharedLinks; + } + } + // todo: handle folder return { @@ -97,8 +127,9 @@ export class OnedriveFileMapper implements IFileMapper { mime_type: file.file.mimeType, size: file.size.toString(), folder_id: null, + // permission: opts.permissions?.[0] || null, permission: null, - shared_link: null, + shared_link: opts.shared_links?.[0] || null, field_mappings, }; } diff --git a/packages/api/src/filestorage/folder/services/onedrive/index.ts b/packages/api/src/filestorage/folder/services/onedrive/index.ts index 7a2a63328..8ae7b041b 100644 --- a/packages/api/src/filestorage/folder/services/onedrive/index.ts +++ b/packages/api/src/filestorage/folder/services/onedrive/index.ts @@ -12,6 +12,7 @@ import { OnedriveFolderInput, OnedriveFolderOutput } from './types'; 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'; @Injectable() export class OnedriveService implements IFolderService { @@ -87,7 +88,7 @@ export class OnedriveService implements IFolderService { batch = [remote_folder_id]; while (batch.length > 0) { - if (depth > 2) { + if (depth > 5) { // todo: handle this better break; } @@ -106,15 +107,42 @@ export class OnedriveService implements IFolderService { }, ); - const folders = resp.data.value.filter( - (driveItem) => driveItem.folder, + // Add permissions (shared link is also included in permissions in one-drive) + await Promise.all( + resp.data.value.map(async (driveItem) => { + const resp = await axios.get( + `${connection.account_url}/v1.0/drive/items/${driveItem.id}/permissions`, + { + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${this.cryptoService.decrypt( + connection.access_token, + )}`, + }, + }, + ); + driveItem.permissions = resp.data.value; + }), ); - const files = resp.data.value.filter( - (driveItem) => !driveItem.folder, + const folders = resp.data.value.filter( + (driveItem) => driveItem.folder, ); - // todo: send files to ingest + // const files = resp.data.value.filter( + // (driveItem) => !driveItem.folder, + // ); + + // await this.ingestService.ingestData< + // UnifiedFilestorageFileOutput, + // OnedriveFileOutput + // >( + // files, + // 'onedrive', + // connection.id_connection, + // 'filestorage', + // FileStorageObject.file, + // ); return folders; }), diff --git a/packages/api/src/filestorage/folder/services/onedrive/mappers.ts b/packages/api/src/filestorage/folder/services/onedrive/mappers.ts index 145c53dca..4b93880e5 100644 --- a/packages/api/src/filestorage/folder/services/onedrive/mappers.ts +++ b/packages/api/src/filestorage/folder/services/onedrive/mappers.ts @@ -11,6 +11,7 @@ import { UnifiedFilestorageSharedlinkOutput } from '@filestorage/sharedlink/type import { Injectable } from '@nestjs/common'; import { OnedriveFolderInput, OnedriveFolderOutput } from './types'; import { FileStorageObject } from '@filestorage/@lib/@types'; +import { OriginalPermissionOutput } from '@@core/utils/types/original/original.file-storage'; @Injectable() export class OnedriveFolderMapper implements IFolderMapper { @@ -93,7 +94,34 @@ export class OnedriveFolderMapper implements IFolderMapper { } } - // todo: add support for shared link + const opts: any = {}; + if (folder.permissions.length) { + const permissions = await this.coreUnificationService.unify< + OriginalPermissionOutput[] + >({ + sourceObject: folder.permissions, + targetType: FileStorageObject.permission, + providerName: 'onedrive', + vertical: 'filestorage', + connectionId, + customFieldMappings: [], + }); + opts.permissions = permissions; + + // shared link + if (folder.permissions.some((p) => p.link)) { + const sharedLinks = + await this.coreUnificationService.unify({ + sourceObject: folder.permissions.find((p) => p.link), + targetType: FileStorageObject.sharedlink, + providerName: 'onedrive', + vertical: 'filestorage', + connectionId, + customFieldMappings: [], + }); + opts.shared_links = sharedLinks; + } + } const result = { remote_id: folder.id, @@ -106,9 +134,10 @@ export class OnedriveFolderMapper implements IFolderMapper { folder.parentReference?.id, connectionId, ), + // permission: opts.permissions?.[0] || null, permission: null, - size: folder.size.toString(), // Folders have 0 size in onedrive - shared_link: null, + size: folder.size.toString(), + shared_link: opts.shared_links?.[0] || null, field_mappings, }; diff --git a/packages/api/src/filestorage/sharedlink/sharedlink.module.ts b/packages/api/src/filestorage/sharedlink/sharedlink.module.ts index 296da41ad..e1d515bf8 100644 --- a/packages/api/src/filestorage/sharedlink/sharedlink.module.ts +++ b/packages/api/src/filestorage/sharedlink/sharedlink.module.ts @@ -1,5 +1,4 @@ -import { OnedriveSharedlinkMapper } from './services/onedrive/mappers'; -import { BoxSharedlinkMapper } from './services/box/mappers'; +import { OnedriveSharedLinkMapper } from './services/onedrive/mappers'; import { OnedriveService } from './services/onedrive'; import { BoxService } from './services/box'; import { EncryptionService } from '@@core/@core-services/encryption/encryption.service'; @@ -37,8 +36,8 @@ import { Utils } from '@filestorage/@lib/@utils'; BoxSharedLinkMapper, BoxService, OnedriveService, - BoxSharedlinkMapper, - OnedriveSharedlinkMapper, + BoxSharedLinkMapper, + OnedriveSharedLinkMapper, ], exports: [SyncService], })