-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathLargeFileUploadTask.ts
378 lines (346 loc) · 12.1 KB
/
LargeFileUploadTask.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
/**
* @module LargeFileUploadTask
*/
import { GraphClientError } from "../GraphClientError";
import { GraphResponseHandler } from "../GraphResponseHandler";
import { Client } from "../index";
import { ResponseType } from "../ResponseType";
import { UploadEventHandlers } from "./FileUploadTask/Interfaces/IUploadEventHandlers";
import { Range } from "./FileUploadTask/Range";
import { UploadResult } from "./FileUploadTask/UploadResult";
/**
* @interface
* Signature to representing key value pairs
* @property {[key: string] : string | number} - The Key value pair
*/
interface KeyValuePairObjectStringNumber {
[key: string]: string | number;
}
/**
* @interface
* Signature to represent the resulting response in the status enquiry request
* @property {string} expirationDateTime - The expiration time of the upload session
* @property {string[]} nextExpectedRanges - The ranges expected in next consecutive request in the upload
*/
interface UploadStatusResponse {
expirationDateTime: string;
nextExpectedRanges: string[];
}
/**
* @interface
* Signature to define options for upload task
* @property {number} [rangeSize = LargeFileUploadTask.DEFAULT_FILE_SIZE] - Specifies the range chunk size
* @property {UploadEventHandlers} uploadEventHandlers - UploadEventHandlers attached to an upload task
*/
export interface LargeFileUploadTaskOptions {
rangeSize?: number;
uploadEventHandlers?: UploadEventHandlers;
}
/**
* @interface
* Signature to represent upload session resulting from the session creation in the server
* @property {string} url - The URL to which the file upload is made
* @property {Date} expiry - The expiration of the time of the upload session
*/
export interface LargeFileUploadSession {
url: string;
expiry: Date;
isCancelled?: boolean;
}
/**
* @type
* Representing the return type of the sliceFile function that is type of the slice of a given range.
*/
export type SliceType = ArrayBuffer | Blob | Uint8Array;
/**
* @interface
* Signature to define the properties and content of the file in upload task
* @property {ArrayBuffer | File} content - The actual file content
* @property {string} name - Specifies the file name with extension
* @property {number} size - Specifies size of the file
*/
export interface FileObject<T> {
content: T;
name: string;
size: number;
sliceFile(range: Range): SliceType | Promise<SliceType>;
}
/**
* @class
* Class representing LargeFileUploadTask
*/
export class LargeFileUploadTask<T> {
/**
* @private
* Default value for the rangeSize
*/
private DEFAULT_FILE_SIZE: number = 5 * 1024 * 1024;
/**
* @protected
* The GraphClient instance
*/
protected client: Client;
/**
* @protected
* The object holding file details
*/
protected file: FileObject<T>;
/**
* @protected
* The object holding options for the task
*/
protected options: LargeFileUploadTaskOptions;
/**
* @protected
* The object for upload session
*/
protected uploadSession: LargeFileUploadSession;
/**
* @protected
* The next range needs to be uploaded
*/
protected nextRange: Range;
/**
* @public
* @static
* @async
* Makes request to the server to create an upload session
* @param {Client} client - The GraphClient instance
* @param {string} requestUrl - The URL to create the upload session
* @param {any} payload - The payload that needs to be sent
* @param {KeyValuePairObjectStringNumber} headers - The headers that needs to be sent
* @returns The promise that resolves to LargeFileUploadSession
*/
public static async createUploadSession(client: Client, requestUrl: string, payload: any, headers: KeyValuePairObjectStringNumber = {}): Promise<LargeFileUploadSession> {
const session = await client
.api(requestUrl)
.headers(headers)
.post(payload);
const largeFileUploadSession: LargeFileUploadSession = {
url: session.uploadUrl,
expiry: new Date(session.expirationDateTime),
isCancelled: false,
};
return largeFileUploadSession;
}
/**
* @public
* @constructor
* Constructs a LargeFileUploadTask
* @param {Client} client - The GraphClient instance
* @param {FileObject} file - The FileObject holding details of a file that needs to be uploaded
* @param {LargeFileUploadSession} uploadSession - The upload session to which the upload has to be done
* @param {LargeFileUploadTaskOptions} options - The upload task options
* @returns An instance of LargeFileUploadTask
*/
public constructor(client: Client, file: FileObject<T>, uploadSession: LargeFileUploadSession, options: LargeFileUploadTaskOptions = {}) {
this.client = client;
if (!file.sliceFile) {
throw new GraphClientError("Please pass the FileUpload object, StreamUpload object or any custom implementation of the FileObject interface");
} else {
this.file = file;
}
this.file = file;
if (!options.rangeSize) {
options.rangeSize = this.DEFAULT_FILE_SIZE;
}
this.options = options;
this.uploadSession = uploadSession;
this.nextRange = new Range(0, this.options.rangeSize - 1);
}
/**
* @private
* Parses given range string to the Range instance
* @param {string[]} ranges - The ranges value
* @returns The range instance
*/
private parseRange(ranges: string[]): Range {
const rangeStr = ranges[0];
if (typeof rangeStr === "undefined" || rangeStr === "") {
return new Range();
}
const firstRange = rangeStr.split("-");
const minVal = parseInt(firstRange[0], 10);
let maxVal = parseInt(firstRange[1], 10);
if (Number.isNaN(maxVal)) {
maxVal = this.file.size - 1;
}
return new Range(minVal, maxVal);
}
/**
* @private
* Updates the expiration date and the next range
* @param {UploadStatusResponse} response - The response of the upload status
* @returns Nothing
*/
private updateTaskStatus(response: UploadStatusResponse): void {
this.uploadSession.expiry = new Date(response.expirationDateTime);
this.nextRange = this.parseRange(response.nextExpectedRanges);
}
/**
* @public
* Gets next range that needs to be uploaded
* @returns The range instance
*/
public getNextRange(): Range {
if (this.nextRange.minValue === -1) {
return this.nextRange;
}
const minVal = this.nextRange.minValue;
let maxValue = minVal + this.options.rangeSize - 1;
if (maxValue >= this.file.size) {
maxValue = this.file.size - 1;
}
return new Range(minVal, maxValue);
}
/**
* @deprecated This function has been moved into FileObject interface.
* @public
* Slices the file content to the given range
* @param {Range} range - The range value
* @returns The sliced ArrayBuffer or Blob
*/
public sliceFile(range: Range): ArrayBuffer | Blob {
console.warn("The LargeFileUploadTask.sliceFile() function has been deprecated and moved into the FileObject interface.");
if (this.file.content instanceof ArrayBuffer || this.file.content instanceof Blob || this.file.content instanceof Uint8Array) {
return this.file.content.slice(range.minValue, range.maxValue + 1);
}
throw new GraphClientError("The LargeFileUploadTask.sliceFile() function expects only Blob, ArrayBuffer or Uint8Array file content. Please note that the sliceFile() function is deprecated.");
}
/**
* @public
* @async
* Uploads file to the server in a sequential order by slicing the file
* @returns The promise resolves to uploaded response
*/
public async upload(): Promise<UploadResult> {
const uploadEventHandlers = this.options && this.options.uploadEventHandlers;
while (!this.uploadSession.isCancelled) {
const nextRange = this.getNextRange();
if (nextRange.maxValue === -1) {
const err = new Error("Task with which you are trying to upload is already completed, Please check for your uploaded file");
err.name = "Invalid Session";
throw err;
}
const fileSlice = await this.file.sliceFile(nextRange);
const rawResponse = await this.uploadSliceGetRawResponse(fileSlice, nextRange, this.file.size);
if (!rawResponse) {
throw new GraphClientError("Something went wrong! Large file upload slice response is null.");
}
const responseBody = await GraphResponseHandler.getResponse(rawResponse);
/**
* (rawResponse.status === 201) -> This condition is applicable for OneDrive, PrintDocument and Outlook APIs.
* (rawResponse.status === 200 && responseBody.id) -> This additional condition is applicable only for OneDrive API.
*/
if (rawResponse.status === 201 || (rawResponse.status === 200 && responseBody.id)) {
this.reportProgress(uploadEventHandlers, nextRange);
return UploadResult.CreateUploadResult(responseBody, rawResponse.headers);
}
/* Handling the API issue where the case of Outlook upload response property -'nextExpectedRanges' is not uniform.
* https://github.com/microsoftgraph/msgraph-sdk-serviceissues/issues/39
*/
const res: UploadStatusResponse = {
expirationDateTime: responseBody.expirationDateTime || responseBody.ExpirationDateTime,
nextExpectedRanges: responseBody.NextExpectedRanges || responseBody.nextExpectedRanges,
};
this.updateTaskStatus(res);
this.reportProgress(uploadEventHandlers, nextRange);
}
}
private reportProgress(uploadEventHandlers: UploadEventHandlers, nextRange: Range) {
if (uploadEventHandlers && uploadEventHandlers.progress) {
uploadEventHandlers.progress(nextRange, uploadEventHandlers.extraCallbackParam);
}
}
/**
* @public
* @async
* Uploads given slice to the server
* @param {ArrayBuffer | Blob | File} fileSlice - The file slice
* @param {Range} range - The range value
* @param {number} totalSize - The total size of a complete file
* @returns The response body of the upload slice result
*/
public async uploadSlice(fileSlice: ArrayBuffer | Blob | File, range: Range, totalSize: number): Promise<unknown> {
return await this.client
.api(this.uploadSession.url)
.headers({
"Content-Length": `${range.maxValue - range.minValue + 1}`,
"Content-Range": `bytes ${range.minValue}-${range.maxValue}/${totalSize}`,
"Content-Type": "application/octet-stream",
})
.put(fileSlice);
}
/**
* @public
* @async
* Uploads given slice to the server
* @param {unknown} fileSlice - The file slice
* @param {Range} range - The range value
* @param {number} totalSize - The total size of a complete file
* @returns The raw response of the upload slice result
*/
public async uploadSliceGetRawResponse(fileSlice: unknown, range: Range, totalSize: number): Promise<Response> {
return await this.client
.api(this.uploadSession.url)
.headers({
"Content-Length": `${range.maxValue - range.minValue + 1}`,
"Content-Range": `bytes ${range.minValue}-${range.maxValue}/${totalSize}`,
"Content-Type": "application/octet-stream",
})
.responseType(ResponseType.RAW)
.put(fileSlice);
}
/**
* @public
* @async
* Deletes upload session in the server
* @returns The promise resolves to cancelled response
*/
public async cancel(): Promise<unknown> {
const cancelResponse = await this.client
.api(this.uploadSession.url)
.responseType(ResponseType.RAW)
.delete();
if (cancelResponse.status === 204) {
this.uploadSession.isCancelled = true;
}
return cancelResponse;
}
/**
* @public
* @async
* Gets status for the upload session
* @returns The promise resolves to the status enquiry response
*/
public async getStatus(): Promise<unknown> {
const response = await this.client.api(this.uploadSession.url).get();
this.updateTaskStatus(response);
return response;
}
/**
* @public
* @async
* Resumes upload session and continue uploading the file from the last sent range
* @returns The promise resolves to the uploaded response
*/
public async resume(): Promise<unknown> {
await this.getStatus();
return await this.upload();
}
/**
* @public
* @async
* Get the upload session information
* @returns The large file upload session
*/
public getUploadSession(): LargeFileUploadSession {
return this.uploadSession;
}
}