Skip to content

Commit

Permalink
✨ Sync root files for onedrive/sharepoint
Browse files Browse the repository at this point in the history
  • Loading branch information
naelob committed Sep 17, 2024
1 parent b3618a1 commit df225e6
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 82 deletions.
101 changes: 61 additions & 40 deletions packages/api/src/filestorage/file/services/onedrive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export class OnedriveService implements IFileService {
async sync(data: SyncParam): Promise<ApiResponse<OnedriveFileOutput[]>> {
try {
const { linkedUserId, id_folder } = data;
if (!id_folder) return;

const connection = await this.prisma.connections.findFirst({
where: {
Expand All @@ -39,57 +38,79 @@ export class OnedriveService implements IFileService {
},
});

const folder = await this.prisma.fs_folders.findUnique({
where: {
id_fs_folder: id_folder as string,
},
});

const resp = await axios.get(
`${connection.account_url}/v1.0/me/drive/items/${folder.remote_id}/children`,
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.cryptoService.decrypt(
connection.access_token,
)}`,
const foldersToSync = ['root'];
if (id_folder) {
const folder = await this.prisma.fs_folders.findUnique({
where: {
id_fs_folder: id_folder as string,
},
},
);
});
if (folder && folder.remote_id !== 'root') {
foldersToSync.push(folder.remote_id);
}
}

const files: OnedriveFileOutput[] = resp.data.value.filter(
(elem) => !elem.folder, // files don't have a folder property
);
const allFiles: OnedriveFileOutput[] = [];

// 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/me/drive/items/${driveItem.id}/permissions`,
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.cryptoService.decrypt(
connection.access_token,
)}`,
},
},
);
driveItem.permissions = resp.data.value;
}),
);
for (const folderId of foldersToSync) {
const files = await this.syncFolder(connection, folderId);
allFiles.push(...files);
}

this.logger.log(`Synced onedrive files !`);
this.logger.log(`Synced OneDrive files from root and specified folder!`);
return {
data: files,
message: "One Drive's files retrieved",
data: allFiles,
message: "OneDrive's files retrieved from root and specified folder",
statusCode: 200,
};
} catch (error) {
this.logger.error(
`Error syncing OneDrive files: ${error.message}`,
error,
);
throw error;
}
}
private async syncFolder(
connection: any,
folderId: string,
): Promise<OnedriveFileOutput[]> {
const resp = await axios.get(
`${connection.account_url}/v1.0/me/drive/items/${folderId}/children`,
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.cryptoService.decrypt(
connection.access_token,
)}`,
},
},
);

const files: OnedriveFileOutput[] = resp.data.value.filter(
(elem) => !elem.folder, // files don't have a folder property
);

// Add permissions (shared link is also included in permissions in OneDrive)
await Promise.all(
files.map(async (driveItem) => {
const resp = await axios.get(
`${connection.account_url}/v1.0/me/drive/items/${driveItem.id}/permissions`,
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.cryptoService.decrypt(
connection.access_token,
)}`,
},
},
);
driveItem.permissions = resp.data.value;
}),
);

return files;
}
async downloadFile(fileId: string, connection: any): Promise<Buffer> {
const response = await axios.get(
`${connection.account_url}/v1.0/me/drive/items/${fileId}/content`,
Expand Down
107 changes: 65 additions & 42 deletions packages/api/src/filestorage/file/services/sharepoint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ export class SharepointService implements IFileService {
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: {
Expand All @@ -39,57 +36,83 @@ export class SharepointService implements IFileService {
},
});

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 foldersToSync = ['root'];
if (id_folder) {
const folder = await this.prisma.fs_folders.findUnique({
where: {
id_fs_folder: id_folder as string,
},
},
);
});
if (folder && folder.remote_id !== 'root') {
foldersToSync.push(folder.remote_id);
}
}

const files: SharepointFileOutput[] = resp.data.value.filter(
(elem) => !elem.folder, // files don't have a folder property
);
const allFiles: SharepointFileOutput[] = [];

// 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;
}),
);
for (const folderId of foldersToSync) {
const files = await this.syncFolder(connection, folderId);
allFiles.push(...files);
}

this.logger.log(`Synced sharepoint files !`);
this.logger.log(
`Synced SharePoint files from root and specified folder!`,
);
return {
data: files,
message: "One Drive's files retrieved",
data: allFiles,
message: 'SharePoint files retrieved from root and specified folder',
statusCode: 200,
};
} catch (error) {
this.logger.error(
`Error syncing SharePoint files: ${error.message}`,
error,
);
throw error;
}
}

private async syncFolder(
connection: any,
folderId: string,
): Promise<SharepointFileOutput[]> {
const resp = await axios.get(
`${connection.account_url}/drive/items/${folderId}/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 permissions (shared link is also included in permissions in SharePoint)
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;
}),
);

return files;
}

async downloadFile(fileId: string, connection: any): Promise<Buffer> {
const response = await axios.get(
`${connection.account_url}/drive/items/${fileId}/content`,
Expand Down

0 comments on commit df225e6

Please sign in to comment.