Skip to content

Commit b1492b4

Browse files
committed
AuthHandler check for graph url, createUploadResult static function
1 parent 34a5436 commit b1492b4

File tree

8 files changed

+171
-41
lines changed

8 files changed

+171
-41
lines changed

.huskyrc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
{
2-
"hooks": {
3-
"pre-commit": "npm run pre-build && git add src/Version.ts && lint-staged && npm run lint && npm run test"
4-
}
52
}

samples/node/main.js

Lines changed: 107 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,119 @@ const MicrosoftGraph = require("../../lib/src/index.js");
99

1010
const secrets = require("./secrets");
1111

12-
const fs = require("fs");
12+
const fs = require('fs');
1313

1414
const client = MicrosoftGraph.Client.init({
15-
defaultVersion: "v1.0",
16-
debugLogging: true,
17-
authProvider: (done) => {
18-
done(null, secrets.accessToken);
19-
},
15+
defaultVersion: "v1.0",
16+
debugLogging: true,
17+
authProvider: (done) => {
18+
done(null, secrets.accessToken);
19+
},
2020
});
2121

22+
function uploadFile1() {
23+
fs.readFile("./test.txt", {}, function(err, file) {
24+
if (err) {
25+
throw err;
26+
}
27+
let fileName = "text.txt";
28+
oneDriveLargeFileUpload1(client, file, fileName)
29+
.then((response) => {
30+
console.log(response);
31+
console.log("File Uploaded Successfully.!!");
32+
})
33+
.catch((error) => {
34+
throw error;
35+
});
36+
});
37+
}
38+
async function oneDriveLargeFileUpload1(client, file, fileName) {
39+
try {
40+
let options = {
41+
path: "/Documents",
42+
fileName,
43+
rangeSize: 1024 * 1024,
44+
};
45+
const uploadTask = await MicrosoftGraph.OneDriveLargeFileUploadTask.create(client, file, options);
46+
const response = await uploadTask.upload();
47+
return response;
48+
} catch (err) {
49+
console.log(err);
50+
}
51+
}
52+
uploadFile1();
53+
2254
// Get the name of the authenticated user with promises
23-
client
24-
.api("/me")
25-
.select("displayName")
26-
.get()
27-
.then((res) => {
28-
console.log(res);
29-
})
30-
.catch((err) => {
31-
console.log(err);
32-
});
55+
// client
56+
// .api("/me")
57+
// .select("displayName")
58+
// .get()
59+
// .then((res) => {
60+
// console.log(res);
61+
// })
62+
// .catch((err) => {
63+
// console.log(err);
64+
// });
65+
66+
67+
//uploadFile();
68+
function uploadFile() {
69+
fs.readFile("./test.txt", {}, function (err, file) {
70+
if (err) {
71+
throw err;
72+
}
73+
var stats = fs.statSync("./test.txt")
74+
75+
const messageId = "AAMkADZiNzhhNTVkLWU5MDEtNGNlNy1hMjZiLTJjN2RkNjcyNGM4NgBGAAAAAABxs3khvJ1fSYvq33QgqqSJBwBC_D0Xqz_3TKBt1JyxMQ_VAAAAAAEMAABC_D0Xqz_3TKBt1JyxMQ_VAACGMDZTAAA=";
76+
client.api(`me/messages/${messageId}/attachments/createUploadSession`).post({
77+
AttachmentItem: {
78+
attachmentType: 'file',
79+
name: "test.txt",
80+
size: stats.size,
81+
}
82+
})
83+
.then((response) => {
84+
console.log(response);
85+
86+
console.log("File Uploaded Successfully.!!");
87+
oneDriveLargeFileUpload(client, file, response)
88+
.then((res) => {
89+
console.log(res);
90+
console.log("File Uploaded Successfully.!!");
91+
})
92+
.catch((error) => {
93+
throw error;
94+
});
95+
})
96+
.catch((error) => {
97+
console.log(".!!");
98+
console.log(error);
99+
});
100+
101+
});
102+
}
33103

104+
async function oneDriveLargeFileUpload(client, file, uploadSession) {
105+
try {
106+
let fileName = "test.txt";
107+
let options = {
108+
path: "/Documents",
109+
fileName,
110+
rangeSize: 1024 * 1024,
111+
};
112+
const s = {
113+
url:uploadSession.uploadUrl,
114+
expiry: uploadSession.expirationDateTime
115+
}
116+
console.log("uploadSession");
117+
const uploadTask = new MicrosoftGraph.LargeFileUploadTask(client, file, s, options);
118+
console.log(uploadTask+"uploadTask");
119+
const response = await uploadTask.upload();
120+
return response;
121+
} catch (err) {
122+
console.log(err);
123+
}
124+
}
34125
/*
35126
36127
// Update the authenticated users birthday.

samples/node/package-lock.json

Lines changed: 43 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/node/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"author": "",
1010
"license": "ISC",
1111
"dependencies": {
12-
"isomorphic-fetch": "^2.2.1"
12+
"bytes": "^3.1.0",
13+
"isomorphic-fetch": "^2.2.1",
14+
"minimist": "^1.2.5",
15+
"path": "^0.12.7"
1316
}
1417
}

samples/node/secrets.sample.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/middleware/AuthenticationHandler.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @module AuthenticationHandler
1010
*/
1111

12+
import { isGraphURL } from "../GraphRequestUtil";
1213
import { AuthenticationProvider } from "../IAuthenticationProvider";
1314
import { AuthenticationProviderOptions } from "../IAuthenticationProviderOptions";
1415
import { Context } from "../IContext";
@@ -60,6 +61,8 @@ export class AuthenticationHandler implements Middleware {
6061
* @returns A Promise that resolves to nothing
6162
*/
6263
public async execute(context: Context): Promise<void> {
64+
const url = typeof context.request === "string" ? context.request : context.request.url;
65+
if (isGraphURL(url)) {
6366
let options: AuthenticationHandlerOptions;
6467
if (context.middlewareControl instanceof MiddlewareControl) {
6568
options = context.middlewareControl.getMiddlewareOptions(AuthenticationHandlerOptions) as AuthenticationHandlerOptions;
@@ -77,6 +80,11 @@ export class AuthenticationHandler implements Middleware {
7780
const bearerKey = `Bearer ${token}`;
7881
appendRequestHeader(context.request, context.options, AuthenticationHandler.AUTHORIZATION_HEADER, bearerKey);
7982
TelemetryHandlerOptions.updateFeatureUsageFlag(context, FeatureUsageFlag.AUTHENTICATION_HANDLER_ENABLED);
83+
} else {
84+
if (context.options.headers) {
85+
delete context.options.headers[AuthenticationHandler.AUTHORIZATION_HEADER];
86+
}
87+
}
8088
return await this.nextMiddleware.execute(context);
8189
}
8290

src/tasks/FileUploadUtil/UploadResult.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
2-
import { GraphResponseHandler } from "../../GraphResponseHandler";
31
export class UploadResult {
42
private _location: string;
53
private _body: unknown;
6-
private _uploadSuccess: boolean;
74

85
public get location(): string {
96
return this._location;
@@ -22,10 +19,12 @@ export class UploadResult {
2219
this._body = body;
2320
}
2421

25-
public async setUploadResult(response: Response) {
26-
this._location = response.headers.get("Location");
27-
this._body = await GraphResponseHandler.getResponse(response);
28-
this._uploadSuccess =
22+
public constructor(body:unknown, location:string){
23+
this._location = location;
24+
this._body = body;
25+
}
26+
27+
public static CreateUploadResult(responsebody?:unknown, responseHeaders?:Headers){
28+
return new UploadResult(responsebody, (responseHeaders.get("Location") || responseHeaders.get("headers")));
2929
}
30-
3130
}

src/tasks/LargeFileUploadTask.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,10 @@ export class LargeFileUploadTask {
236236

237237
const responseBody = await GraphResponseHandler.getResponse(rawResponse);
238238
if (rawResponse.status == 201 || (rawResponse.status == 200 && responseBody.id)) {
239-
const result = new UploadResult();
240-
return result.setUploadResult(rawResponse);
239+
return UploadResult.CreateUploadResult(responseBody, rawResponse.headers);
241240
}
242241

243-
/* Handling an API issue where the case of Outlook upload 'nextExpectedRanges' property is not uniform.
242+
/* Handling the API issue where the case of Outlook upload response property -'nextExpectedRanges' is not uniform.
244243
* https://github.com/microsoftgraph/msgraph-sdk-serviceissues/issues/39
245244
*/
246245
const res: UploadStatusResponse = {
@@ -249,7 +248,6 @@ export class LargeFileUploadTask {
249248
};
250249

251250
this.updateTaskStatus(res);
252-
this.updateTaskStatus(responseBody);
253251
}
254252
}
255253

0 commit comments

Comments
 (0)