-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add file, folder, permission, sharedlink, group and user objects to s…
…harepoint
- Loading branch information
Showing
27 changed files
with
2,134 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/api/src/filestorage/file/services/sharepoint/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { EncryptionService } from '@@core/@core-services/encryption/encryption.service'; | ||
import { LoggerService } from '@@core/@core-services/logger/logger.service'; | ||
import { PrismaService } from '@@core/@core-services/prisma/prisma.service'; | ||
import { ApiResponse } from '@@core/utils/types'; | ||
import { SyncParam } from '@@core/utils/types/interface'; | ||
import { FileStorageObject } from '@filestorage/@lib/@types'; | ||
import { IFileService } from '@filestorage/file/types'; | ||
import { Injectable } from '@nestjs/common'; | ||
import axios from 'axios'; | ||
import { ServiceRegistry } from '../registry.service'; | ||
import { SharepointFileOutput } from './types'; | ||
|
||
@Injectable() | ||
export class SharepointService implements IFileService { | ||
constructor( | ||
private prisma: PrismaService, | ||
private logger: LoggerService, | ||
private cryptoService: EncryptionService, | ||
private registry: ServiceRegistry, | ||
) { | ||
this.logger.setContext( | ||
`${FileStorageObject.file.toUpperCase()}:${SharepointService.name}`, | ||
); | ||
this.registry.registerService('sharepoint', this); | ||
} | ||
|
||
// todo: add addFile method | ||
|
||
async sync(data: SyncParam): Promise<ApiResponse<SharepointFileOutput[]>> { | ||
try { | ||
const { linkedUserId, id_folder } = data; | ||
if (!id_folder) return; | ||
|
||
const connection = await this.prisma.connections.findFirst({ | ||
where: { | ||
id_linked_user: linkedUserId, | ||
provider_slug: 'sharepoint', | ||
vertical: 'filestorage', | ||
}, | ||
}); | ||
|
||
const folder = await this.prisma.fs_folders.findUnique({ | ||
where: { | ||
id_fs_folder: id_folder as string, | ||
}, | ||
}); | ||
|
||
const resp = await axios.get( | ||
`${connection.account_url}/drive/items/${folder.remote_id}/children`, | ||
{ | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${this.cryptoService.decrypt( | ||
connection.access_token, | ||
)}`, | ||
}, | ||
}, | ||
); | ||
|
||
const files: SharepointFileOutput[] = resp.data.value.filter( | ||
(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}/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 sharepoint files !`); | ||
return { | ||
data: files, | ||
message: "One Drive's files retrieved", | ||
statusCode: 200, | ||
}; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
packages/api/src/filestorage/file/services/sharepoint/mappers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { MappersRegistry } from '@@core/@core-services/registries/mappers.registry'; | ||
import { CoreUnification } from '@@core/@core-services/unification/core-unification.service'; | ||
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'; | ||
import { | ||
UnifiedFilestorageFileInput, | ||
UnifiedFilestorageFileOutput, | ||
} from '@filestorage/file/types/model.unified'; | ||
import { UnifiedFilestorageSharedlinkOutput } from '@filestorage/sharedlink/types/model.unified'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { SharepointFileInput, SharepointFileOutput } from './types'; | ||
|
||
@Injectable() | ||
export class SharepointFileMapper implements IFileMapper { | ||
constructor( | ||
private mappersRegistry: MappersRegistry, | ||
private utils: Utils, | ||
private coreUnificationService: CoreUnification, | ||
) { | ||
this.mappersRegistry.registerService( | ||
'filestorage', | ||
'file', | ||
'sharepoint', | ||
this, | ||
); | ||
} | ||
|
||
async desunify( | ||
source: UnifiedFilestorageFileInput, | ||
customFieldMappings?: { | ||
slug: string; | ||
remote_id: string; | ||
}[], | ||
): Promise<SharepointFileInput> { | ||
// todo: do something with customFieldMappings | ||
return { | ||
name: source.name, | ||
file: { | ||
mimeType: source.mime_type, | ||
}, | ||
size: parseInt(source.size), | ||
parentReference: { | ||
id: source.folder_id, | ||
}, | ||
}; | ||
} | ||
|
||
async unify( | ||
source: SharepointFileOutput | SharepointFileOutput[], | ||
connectionId: string, | ||
customFieldMappings?: { | ||
slug: string; | ||
remote_id: string; | ||
}[], | ||
): Promise<UnifiedFilestorageFileOutput | UnifiedFilestorageFileOutput[]> { | ||
if (!Array.isArray(source)) { | ||
return await this.mapSingleFileToUnified( | ||
source, | ||
connectionId, | ||
customFieldMappings, | ||
); | ||
} | ||
// Handling array of SharepointFileOutput | ||
return Promise.all( | ||
source.map((file) => | ||
this.mapSingleFileToUnified(file, connectionId, customFieldMappings), | ||
), | ||
); | ||
} | ||
|
||
private async mapSingleFileToUnified( | ||
file: SharepointFileOutput, | ||
connectionId: string, | ||
customFieldMappings?: { | ||
slug: string; | ||
remote_id: string; | ||
}[], | ||
): Promise<UnifiedFilestorageFileOutput> { | ||
const field_mappings: { [key: string]: any } = {}; | ||
if (customFieldMappings) { | ||
for (const mapping of customFieldMappings) { | ||
field_mappings[mapping.slug] = file[mapping.remote_id]; | ||
} | ||
} | ||
|
||
const opts: any = {}; | ||
if (file.permissions?.length) { | ||
const permissions = await this.coreUnificationService.unify< | ||
OriginalPermissionOutput[] | ||
>({ | ||
sourceObject: file.permissions, | ||
targetType: FileStorageObject.permission, | ||
providerName: 'sharepoint', | ||
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: 'sharepoint', | ||
vertical: 'filestorage', | ||
connectionId, | ||
customFieldMappings: [], | ||
}); | ||
opts.shared_links = sharedLinks; | ||
} | ||
} | ||
|
||
// todo: handle folder | ||
|
||
return { | ||
remote_id: file.id, | ||
remote_data: file, | ||
name: file.name, | ||
file_url: file.webUrl, | ||
mime_type: file.file.mimeType, | ||
size: file.size.toString(), | ||
folder_id: null, | ||
// permission: opts.permissions?.[0] || null, | ||
permission: null, | ||
shared_link: opts.shared_links?.[0] || null, | ||
field_mappings, | ||
}; | ||
} | ||
} |
Oops, something went wrong.