Skip to content

Commit

Permalink
Integrate sharedlink and permissions in file and folder objects in on…
Browse files Browse the repository at this point in the history
…e-drive integration
  • Loading branch information
amuwal committed Sep 8, 2024
1 parent 6a63c5b commit 6547159
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 28 deletions.
1 change: 1 addition & 0 deletions packages/api/src/@core/sync/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ export class CoreSyncService {
try {
await task();
} catch (error) {
console.log(error);
this.logger.error(`File Task failed: ${error.message}`, error);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 */

Expand Down Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class OnedriveService implements IDriveService {

return {
data: drives,
message: 'Onedrive drive retrived',
message: 'Onedrive drives retrived',
statusCode: 200,
};
} catch (error) {
Expand Down
18 changes: 18 additions & 0 deletions packages/api/src/filestorage/file/services/onedrive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 35 additions & 4 deletions packages/api/src/filestorage/file/services/onedrive/mappers.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<OriginalSharedLinkOutput>({
sourceObject: file.permissions.find((p) => p.link),
targetType: FileStorageObject.sharedlink,
providerName: 'onedrive',
vertical: 'filestorage',
connectionId,
customFieldMappings: [],
});
opts.shared_links = sharedLinks;
}
}

// todo: handle folder

return {
Expand All @@ -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,
};
}
Expand Down
40 changes: 34 additions & 6 deletions packages/api/src/filestorage/folder/services/onedrive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}),
Expand Down
35 changes: 32 additions & 3 deletions packages/api/src/filestorage/folder/services/onedrive/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<OriginalSharedLinkOutput>({
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,
Expand All @@ -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,
};

Expand Down
7 changes: 3 additions & 4 deletions packages/api/src/filestorage/sharedlink/sharedlink.module.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -37,8 +36,8 @@ import { Utils } from '@filestorage/@lib/@utils';
BoxSharedLinkMapper,
BoxService,
OnedriveService,
BoxSharedlinkMapper,
OnedriveSharedlinkMapper,
BoxSharedLinkMapper,
OnedriveSharedLinkMapper,
],
exports: [SyncService],
})
Expand Down

0 comments on commit 6547159

Please sign in to comment.