Skip to content

Commit 98080f2

Browse files
Added create upload session for large file upload in base class
1 parent c50be7f commit 98080f2

File tree

4 files changed

+71
-52
lines changed

4 files changed

+71
-52
lines changed

spec/tasks/LargeFileUploadTask.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { LargeFileUploadTask } from "../../src/tasks/LargeFileUploadTask";
1111
import { getClient } from "../test-helper";
1212

1313
describe("LargeFileUploadTask.ts", () => {
14+
/* tslint:disable: no-string-literal */
1415
describe("Parsing Range", () => {
1516
const name = "sample_image.jpg";
1617
const arrayBuffer = new ArrayBuffer(80000);
@@ -27,34 +28,33 @@ describe("LargeFileUploadTask.ts", () => {
2728
const options = {};
2829
const uploadTask = new LargeFileUploadTask(getClient(), fileObj, uploadSession, options);
2930
it("Should return default range for given undefined range", (done) => {
30-
const range = uploadTask.parseRange([]);
31+
const range = uploadTask["parseRange"]([]);
3132
assert.equal(range.minValue, -1);
3233
assert.equal(range.maxValue, -1);
3334
done();
3435
});
3536

3637
it("Should return default range for given empty range", (done) => {
37-
const range = uploadTask.parseRange([""]);
38+
const range = uploadTask["parseRange"]([""]);
3839
assert.equal(range.minValue, -1);
3940
assert.equal(range.maxValue, -1);
4041
done();
4142
});
4243

4344
it("Should return valid range for given range with from and to values", (done) => {
44-
const range = uploadTask.parseRange(["100-200"]);
45+
const range = uploadTask["parseRange"](["100-200"]);
4546
assert.equal(range.minValue, 100);
4647
assert.equal(range.maxValue, 200);
4748
done();
4849
});
4950

5051
it("Should return valid range for given range without to value", (done) => {
51-
const range = uploadTask.parseRange(["0-"]);
52+
const range = uploadTask["parseRange"](["0-"]);
5253
assert.equal(range.minValue, 0);
5354
assert.equal(range.maxValue, 99999);
5455
done();
5556
});
5657
});
57-
/* tslint:disable: no-string-literal */
5858
describe("Update Task Status", () => {
5959
const name = "sample_image.jpg";
6060
const arrayBuffer = new ArrayBuffer(80000);
@@ -75,7 +75,7 @@ describe("LargeFileUploadTask.ts", () => {
7575
expirationDateTime: "2018-08-06T09:05:45.195Z",
7676
nextExpectedRanges: ["100-2000"],
7777
};
78-
uploadTask.updateTaskStatus(statusResponse);
78+
uploadTask["updateTaskStatus"](statusResponse);
7979
assert.equal(uploadTask["nextRange"].minValue, 100);
8080
assert.equal(uploadTask["nextRange"].maxValue, 2000);
8181
done();
@@ -85,13 +85,12 @@ describe("LargeFileUploadTask.ts", () => {
8585
expirationDateTime: "2018-08-06T09:05:45.195Z",
8686
nextExpectedRanges: ["100-"],
8787
};
88-
uploadTask.updateTaskStatus(statusResponse);
88+
uploadTask["updateTaskStatus"](statusResponse);
8989
assert.equal(uploadTask["nextRange"].minValue, 100);
9090
assert.equal(uploadTask["nextRange"].maxValue, 99999);
9191
done();
9292
});
9393
});
94-
/* tslint:enable: no-string-literal */
9594

9695
describe("GetNextRange", () => {
9796
const name = "sample_image.jpg";
@@ -123,7 +122,7 @@ describe("LargeFileUploadTask.ts", () => {
123122
expirationDateTime: "2018-08-06T09:05:45.195Z",
124123
nextExpectedRanges: ["327680-"],
125124
};
126-
uploadTask.updateTaskStatus(statusResponse);
125+
uploadTask["updateTaskStatus"](statusResponse);
127126
const nextRange = uploadTask.getNextRange();
128127
assert.equal(nextRange.minValue, 327680);
129128
assert.equal(nextRange.maxValue, 328679);
@@ -135,7 +134,7 @@ describe("LargeFileUploadTask.ts", () => {
135134
expirationDateTime: "2018-08-06T09:05:45.195Z",
136135
nextExpectedRanges: [],
137136
};
138-
uploadTask.updateTaskStatus(statusResponse);
137+
uploadTask["updateTaskStatus"](statusResponse);
139138
const nextRange = uploadTask.getNextRange();
140139
assert.equal(nextRange.minValue, -1);
141140
assert.equal(nextRange.maxValue, -1);
@@ -165,7 +164,7 @@ describe("LargeFileUploadTask.ts", () => {
165164
expirationDateTime: "2018-08-06T09:05:45.195Z",
166165
nextExpectedRanges: [],
167166
};
168-
uploadTask.updateTaskStatus(statusResponse);
167+
uploadTask["updateTaskStatus"](statusResponse);
169168
uploadTask
170169
.upload()
171170
.then((res) => {
@@ -178,4 +177,5 @@ describe("LargeFileUploadTask.ts", () => {
178177
});
179178
});
180179
});
180+
/* tslint:enable: no-string-literal */
181181
});

spec/tasks/OneDriveLargeFileUploadTask.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,34 @@ describe("OneDriveLargeFileUploadTask.ts", () => {
1616
const specialFileName = "test file.png";
1717
const encodedFileName = "test%20file.png";
1818

19+
/* tslint:disable: no-string-literal */
1920
it("Should trim the extra spaces in the filename", () => {
20-
assert.equal(`/me/drive/root:/${fileName}:/createUploadSession`, OneDriveLargeFileUploadTask.constructCreateSessionUrl(spaceFileName));
21+
assert.equal(`/me/drive/root:/${fileName}:/createUploadSession`, OneDriveLargeFileUploadTask["constructCreateSessionUrl"](spaceFileName));
2122
});
2223

2324
it("Should encode space in the filename", () => {
24-
assert.equal(`/me/drive/root:/${encodedFileName}:/createUploadSession`, OneDriveLargeFileUploadTask.constructCreateSessionUrl(specialFileName));
25+
assert.equal(`/me/drive/root:/${encodedFileName}:/createUploadSession`, OneDriveLargeFileUploadTask["constructCreateSessionUrl"](specialFileName));
2526
});
2627

2728
it("Should return url with default root value", () => {
28-
assert.equal(`/me/drive/root:/${fileName}:/createUploadSession`, OneDriveLargeFileUploadTask.constructCreateSessionUrl(fileName));
29+
assert.equal(`/me/drive/root:/${fileName}:/createUploadSession`, OneDriveLargeFileUploadTask["constructCreateSessionUrl"](fileName));
2930
});
3031

3132
it("Should return url with default root value for an empty path string", () => {
32-
assert.equal(`/me/drive/root:/${fileName}:/createUploadSession`, OneDriveLargeFileUploadTask.constructCreateSessionUrl(fileName, ""));
33+
assert.equal(`/me/drive/root:/${fileName}:/createUploadSession`, OneDriveLargeFileUploadTask["constructCreateSessionUrl"](fileName, ""));
3334
});
3435

3536
it("Should add / in front of the path", () => {
36-
assert.equal(`/me/drive/root:/Documents/${fileName}:/createUploadSession`, OneDriveLargeFileUploadTask.constructCreateSessionUrl(fileName, "Documents/"));
37+
assert.equal(`/me/drive/root:/Documents/${fileName}:/createUploadSession`, OneDriveLargeFileUploadTask["constructCreateSessionUrl"](fileName, "Documents/"));
3738
});
3839

3940
it("Should add / in back of the path", () => {
40-
assert.equal(`/me/drive/root:/Documents/${fileName}:/createUploadSession`, OneDriveLargeFileUploadTask.constructCreateSessionUrl(fileName, "/Documents"));
41+
assert.equal(`/me/drive/root:/Documents/${fileName}:/createUploadSession`, OneDriveLargeFileUploadTask["constructCreateSessionUrl"](fileName, "/Documents"));
4142
});
4243

4344
it("Should trim the extra spaces in the path", () => {
44-
assert.equal(`/me/drive/root:/Documents/${fileName}:/createUploadSession`, OneDriveLargeFileUploadTask.constructCreateSessionUrl(fileName, " /Documents/ "));
45+
assert.equal(`/me/drive/root:/Documents/${fileName}:/createUploadSession`, OneDriveLargeFileUploadTask["constructCreateSessionUrl"](fileName, " /Documents/ "));
4546
});
47+
/* tslint:enable: no-string-literal */
4648
});
4749
});

src/tasks/LargeFileUploadTask.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Range } from "../Range";
1515
/**
1616
* @interface
1717
* Signature to represent the resulting response in the status enquiry request
18-
* @property {string} expirationDateTime - The expiration of the time of the upload session
18+
* @property {string} expirationDateTime - The expiration time of the upload session
1919
* @property {string[]} nextExpectedRanges - The ranges expected in next consecutive request in the upload
2020
*/
2121
interface UploadStatusResponse {
@@ -97,6 +97,28 @@ export class LargeFileUploadTask {
9797
*/
9898
protected nextRange: Range;
9999

100+
/**
101+
* @public
102+
* @static
103+
* @async
104+
* Makes request to the server to create an upload session
105+
* @param {Client} client - The GraphClient instance
106+
* @param {any} payload - The payload that needs to be sent
107+
* @returns The promise that resolves to LargeFileUploadSession
108+
*/
109+
public static async createUploadSession(client: Client, requestUrl: string, payload: any): Promise<any> {
110+
try {
111+
const session = await client.api(requestUrl).post(payload);
112+
const largeFileUploadSession: LargeFileUploadSession = {
113+
url: session.uploadUrl,
114+
expiry: new Date(session.expirationDateTime),
115+
};
116+
return largeFileUploadSession;
117+
} catch (err) {
118+
throw err;
119+
}
120+
}
121+
100122
/**
101123
* @public
102124
* @constructor
@@ -119,12 +141,12 @@ export class LargeFileUploadTask {
119141
}
120142

121143
/**
122-
* @public
144+
* @private
123145
* Parses given range string to the Range instance
124146
* @param {string[]} ranges - The ranges value
125147
* @returns The range instance
126148
*/
127-
public parseRange(ranges: string[]): Range {
149+
private parseRange(ranges: string[]): Range {
128150
const rangeStr = ranges[0];
129151
if (typeof rangeStr === "undefined" || rangeStr === "") {
130152
return new Range();
@@ -139,12 +161,12 @@ export class LargeFileUploadTask {
139161
}
140162

141163
/**
142-
* @public
164+
* @private
143165
* Updates the expiration date and the next range
144166
* @param {UploadStatusResponse} response - The response of the upload status
145167
* @returns Nothing
146168
*/
147-
public updateTaskStatus(response: UploadStatusResponse): void {
169+
private updateTaskStatus(response: UploadStatusResponse): void {
148170
this.uploadSession.expiry = new Date(response.expirationDateTime);
149171
this.nextRange = this.parseRange(response.nextExpectedRanges);
150172
}

src/tasks/OneDriveLargeFileUploadTask.ts

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@ export class OneDriveLargeFileUploadTask extends LargeFileUploadTask {
3939
*/
4040
private static DEFAULT_UPLOAD_PATH: string = "/";
4141

42+
/**
43+
* @private
44+
* @static
45+
* Constructs the create session url for Onedrive
46+
* @param {string} fileName - The name of the file
47+
* @param {path} [path = OneDriveLargeFileUploadTask.DEFAULT_UPLOAD_PATH] - The path for the upload
48+
* @returns The constructed create session url
49+
*/
50+
private static constructCreateSessionUrl(fileName: string, path: string = OneDriveLargeFileUploadTask.DEFAULT_UPLOAD_PATH): string {
51+
fileName = fileName.trim();
52+
path = path.trim();
53+
if (path === "") {
54+
path = "/";
55+
}
56+
if (path[0] !== "/") {
57+
path = `/${path}`;
58+
}
59+
if (path[path.length - 1] !== "/") {
60+
path = `${path}/`;
61+
}
62+
return encodeURI(`/me/drive/root:${path}${fileName}:/createUploadSession`);
63+
}
64+
4265
/**
4366
* @public
4467
* @static
@@ -85,29 +108,6 @@ export class OneDriveLargeFileUploadTask extends LargeFileUploadTask {
85108
}
86109
}
87110

88-
/**
89-
* @public
90-
* @static
91-
* Constructs the create session url for Onedrive
92-
* @param {string} fileName - The name of the file
93-
* @param {path} [path = OneDriveLargeFileUploadTask.DEFAULT_UPLOAD_PATH] - The path for the upload
94-
* @returns The constructed create session url
95-
*/
96-
public static constructCreateSessionUrl(fileName: string, path: string = OneDriveLargeFileUploadTask.DEFAULT_UPLOAD_PATH): string {
97-
fileName = fileName.trim();
98-
path = path.trim();
99-
if (path === "") {
100-
path = "/";
101-
}
102-
if (path[0] !== "/") {
103-
path = `/${path}`;
104-
}
105-
if (path[path.length - 1] !== "/") {
106-
path = `${path}/`;
107-
}
108-
return encodeURI(`/me/drive/root:${path}${fileName}:/createUploadSession`);
109-
}
110-
111111
/**
112112
* @public
113113
* @static
@@ -126,12 +126,7 @@ export class OneDriveLargeFileUploadTask extends LargeFileUploadTask {
126126
},
127127
};
128128
try {
129-
const session = await client.api(requestUrl).post(payload);
130-
const largeFileUploadSession: LargeFileUploadSession = {
131-
url: session.uploadUrl,
132-
expiry: new Date(session.expirationDateTime),
133-
};
134-
return largeFileUploadSession;
129+
return super.createUploadSession(client, requestUrl, payload);
135130
} catch (err) {
136131
throw err;
137132
}

0 commit comments

Comments
 (0)