Skip to content

Commit 893e032

Browse files
author
Piotr Siatka
committed
Fix OneDrive and Bing services cors bugs.
1 parent 46d4c03 commit 893e032

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/services/FilesSearchService.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export class FilesSearchService {
183183
const blob: Blob = await fileDownloadResult.blob();
184184
return new File([blob], fileName);
185185
} catch (err) {
186-
console.error(`[FileBrowserService.fetchFileContent] Err='${err.message}'`);
186+
console.error(`[FileSearchService.fetchFileContent] Err='${err.message}'`);
187187
return null;
188188
}
189189
}
@@ -193,7 +193,10 @@ export class FilesSearchService {
193193
*/
194194
public downloadBingContent = async (absoluteFileUrl: string, fileName: string): Promise<File> => {
195195
try {
196-
const fileDownloadResult = await this.context.httpClient.get(absoluteFileUrl, SPHttpClient.configurations.v1);
196+
const fileDownloadResult = await this.context.httpClient.get(absoluteFileUrl, SPHttpClient.configurations.v1, {
197+
method: "GET",
198+
mode: "cors"
199+
});
197200

198201
if (!fileDownloadResult || !fileDownloadResult.ok) {
199202
throw new Error(`Something went wrong when downloading the file. Status='${fileDownloadResult.status}'`);
@@ -203,7 +206,7 @@ export class FilesSearchService {
203206
const blob: Blob = await fileDownloadResult.blob();
204207
return new File([blob], fileName);
205208
} catch (err) {
206-
console.error(`[FileBrowserService.fetchFileContent] Err='${err.message}'`);
209+
console.error(`[FileSearchService.fetchFileContent] Err='${err.message}'`);
207210
return null;
208211
}
209212
}

src/services/OneDriveService.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,29 @@ export class OneDriveService extends FileBrowserService {
5656
return filesQueryResult;
5757
}
5858

59+
/**
60+
* Downloads document content from OneDrive location.
61+
*/
62+
public downloadSPFileContent = async (absoluteFileUrl: string, fileName: string): Promise<File> => {
63+
try {
64+
const fileDownloadResult = await this.context.spHttpClient.get(absoluteFileUrl, SPHttpClient.configurations.v1, {
65+
method: "GET",
66+
mode: "cors"
67+
});
68+
69+
if (!fileDownloadResult || !fileDownloadResult.ok) {
70+
throw new Error(`Something went wrong when downloading the file. Status='${fileDownloadResult.status}'`);
71+
}
72+
73+
// Return file created from blob
74+
const blob : Blob = await fileDownloadResult.blob();
75+
return new File([blob], fileName);
76+
} catch (err) {
77+
console.error(`[OneDriveService.fetchFileContent] Err='${err.message}'`);
78+
return null;
79+
}
80+
}
81+
5982
/**
6083
* Gets users one drive personal documents library path
6184
*/
@@ -147,4 +170,12 @@ export class OneDriveService extends FileBrowserService {
147170
}
148171
return this.oneDrivePersonalUrl;
149172
}
173+
174+
/**
175+
* Creates an absolute URL
176+
*/
177+
protected buildAbsoluteUrl = (relativeUrl: string) => {
178+
const oneDriveHost = `https://${this.oneDrivePersonalUrl.split("//")[1].split("/")[0]}`;
179+
return oneDriveHost + relativeUrl;
180+
}
150181
}

0 commit comments

Comments
 (0)