1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | exports.LargeFileUploadTask = void 0;
|
10 | var tslib_1 = require("tslib");
|
11 |
|
12 |
|
13 |
|
14 | var GraphClientError_1 = require("../GraphClientError");
|
15 | var GraphResponseHandler_1 = require("../GraphResponseHandler");
|
16 | var ResponseType_1 = require("../ResponseType");
|
17 | var Range_1 = require("./FileUploadTask/Range");
|
18 | var UploadResult_1 = require("./FileUploadTask/UploadResult");
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var LargeFileUploadTask = (function () {
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function LargeFileUploadTask(client, file, uploadSession, options) {
|
35 | if (options === void 0) { options = {}; }
|
36 | |
37 |
|
38 |
|
39 |
|
40 | this.DEFAULT_FILE_SIZE = 5 * 1024 * 1024;
|
41 | this.client = client;
|
42 | if (!file.sliceFile) {
|
43 | throw new GraphClientError_1.GraphClientError("Please pass the FileUpload object, StreamUpload object or any custom implementation of the FileObject interface");
|
44 | }
|
45 | else {
|
46 | this.file = file;
|
47 | }
|
48 | this.file = file;
|
49 | if (!options.rangeSize) {
|
50 | options.rangeSize = this.DEFAULT_FILE_SIZE;
|
51 | }
|
52 | this.options = options;
|
53 | this.uploadSession = uploadSession;
|
54 | this.nextRange = new Range_1.Range(0, this.options.rangeSize - 1);
|
55 | }
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | LargeFileUploadTask.createUploadSession = function (client, requestUrl, payload, headers) {
|
68 | if (headers === void 0) { headers = {}; }
|
69 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
70 | var session, largeFileUploadSession;
|
71 | return tslib_1.__generator(this, function (_a) {
|
72 | switch (_a.label) {
|
73 | case 0: return [4 , client
|
74 | .api(requestUrl)
|
75 | .headers(headers)
|
76 | .post(payload)];
|
77 | case 1:
|
78 | session = _a.sent();
|
79 | largeFileUploadSession = {
|
80 | url: session.uploadUrl,
|
81 | expiry: new Date(session.expirationDateTime),
|
82 | isCancelled: false,
|
83 | };
|
84 | return [2 , largeFileUploadSession];
|
85 | }
|
86 | });
|
87 | });
|
88 | };
|
89 | |
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | LargeFileUploadTask.prototype.parseRange = function (ranges) {
|
96 | var rangeStr = ranges[0];
|
97 | if (typeof rangeStr === "undefined" || rangeStr === "") {
|
98 | return new Range_1.Range();
|
99 | }
|
100 | var firstRange = rangeStr.split("-");
|
101 | var minVal = parseInt(firstRange[0], 10);
|
102 | var maxVal = parseInt(firstRange[1], 10);
|
103 | if (Number.isNaN(maxVal)) {
|
104 | maxVal = this.file.size - 1;
|
105 | }
|
106 | return new Range_1.Range(minVal, maxVal);
|
107 | };
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | LargeFileUploadTask.prototype.updateTaskStatus = function (response) {
|
115 | this.uploadSession.expiry = new Date(response.expirationDateTime);
|
116 | this.nextRange = this.parseRange(response.nextExpectedRanges);
|
117 | };
|
118 | |
119 |
|
120 |
|
121 |
|
122 |
|
123 | LargeFileUploadTask.prototype.getNextRange = function () {
|
124 | if (this.nextRange.minValue === -1) {
|
125 | return this.nextRange;
|
126 | }
|
127 | var minVal = this.nextRange.minValue;
|
128 | var maxValue = minVal + this.options.rangeSize - 1;
|
129 | if (maxValue >= this.file.size) {
|
130 | maxValue = this.file.size - 1;
|
131 | }
|
132 | return new Range_1.Range(minVal, maxValue);
|
133 | };
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | LargeFileUploadTask.prototype.sliceFile = function (range) {
|
142 | console.warn("The LargeFileUploadTask.sliceFile() function has been deprecated and moved into the FileObject interface.");
|
143 | if (this.file.content instanceof ArrayBuffer || this.file.content instanceof Blob || this.file.content instanceof Uint8Array) {
|
144 | return this.file.content.slice(range.minValue, range.maxValue + 1);
|
145 | }
|
146 | throw new GraphClientError_1.GraphClientError("The LargeFileUploadTask.sliceFile() function expects only Blob, ArrayBuffer or Uint8Array file content. Please note that the sliceFile() function is deprecated.");
|
147 | };
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | LargeFileUploadTask.prototype.upload = function () {
|
155 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
156 | var uploadEventHandlers, nextRange, err, fileSlice, rawResponse, responseBody, res;
|
157 | return tslib_1.__generator(this, function (_a) {
|
158 | switch (_a.label) {
|
159 | case 0:
|
160 | uploadEventHandlers = this.options && this.options.uploadEventHandlers;
|
161 | _a.label = 1;
|
162 | case 1:
|
163 | if (!!this.uploadSession.isCancelled) return [3 , 5];
|
164 | nextRange = this.getNextRange();
|
165 | if (nextRange.maxValue === -1) {
|
166 | err = new Error("Task with which you are trying to upload is already completed, Please check for your uploaded file");
|
167 | err.name = "Invalid Session";
|
168 | throw err;
|
169 | }
|
170 | return [4 , this.file.sliceFile(nextRange)];
|
171 | case 2:
|
172 | fileSlice = _a.sent();
|
173 | return [4 , this.uploadSliceGetRawResponse(fileSlice, nextRange, this.file.size)];
|
174 | case 3:
|
175 | rawResponse = _a.sent();
|
176 | if (!rawResponse) {
|
177 | throw new GraphClientError_1.GraphClientError("Something went wrong! Large file upload slice response is null.");
|
178 | }
|
179 | return [4 , GraphResponseHandler_1.GraphResponseHandler.getResponse(rawResponse)];
|
180 | case 4:
|
181 | responseBody = _a.sent();
|
182 | |
183 |
|
184 |
|
185 |
|
186 | if (rawResponse.status === 201 || (rawResponse.status === 200 && responseBody.id)) {
|
187 | this.reportProgress(uploadEventHandlers, nextRange);
|
188 | return [2 , UploadResult_1.UploadResult.CreateUploadResult(responseBody, rawResponse.headers)];
|
189 | }
|
190 | res = {
|
191 | expirationDateTime: responseBody.expirationDateTime || responseBody.ExpirationDateTime,
|
192 | nextExpectedRanges: responseBody.NextExpectedRanges || responseBody.nextExpectedRanges,
|
193 | };
|
194 | this.updateTaskStatus(res);
|
195 | this.reportProgress(uploadEventHandlers, nextRange);
|
196 | return [3 , 1];
|
197 | case 5: return [2 ];
|
198 | }
|
199 | });
|
200 | });
|
201 | };
|
202 | LargeFileUploadTask.prototype.reportProgress = function (uploadEventHandlers, nextRange) {
|
203 | if (uploadEventHandlers && uploadEventHandlers.progress) {
|
204 | uploadEventHandlers.progress(nextRange, uploadEventHandlers.extraCallbackParam);
|
205 | }
|
206 | };
|
207 | |
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | LargeFileUploadTask.prototype.uploadSlice = function (fileSlice, range, totalSize) {
|
217 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
218 | return tslib_1.__generator(this, function (_a) {
|
219 | switch (_a.label) {
|
220 | case 0: return [4 , this.client
|
221 | .api(this.uploadSession.url)
|
222 | .headers({
|
223 | "Content-Length": "".concat(range.maxValue - range.minValue + 1),
|
224 | "Content-Range": "bytes ".concat(range.minValue, "-").concat(range.maxValue, "/").concat(totalSize),
|
225 | "Content-Type": "application/octet-stream",
|
226 | })
|
227 | .put(fileSlice)];
|
228 | case 1: return [2 , _a.sent()];
|
229 | }
|
230 | });
|
231 | });
|
232 | };
|
233 | |
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | LargeFileUploadTask.prototype.uploadSliceGetRawResponse = function (fileSlice, range, totalSize) {
|
243 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
244 | return tslib_1.__generator(this, function (_a) {
|
245 | switch (_a.label) {
|
246 | case 0: return [4 , this.client
|
247 | .api(this.uploadSession.url)
|
248 | .headers({
|
249 | "Content-Length": "".concat(range.maxValue - range.minValue + 1),
|
250 | "Content-Range": "bytes ".concat(range.minValue, "-").concat(range.maxValue, "/").concat(totalSize),
|
251 | "Content-Type": "application/octet-stream",
|
252 | })
|
253 | .responseType(ResponseType_1.ResponseType.RAW)
|
254 | .put(fileSlice)];
|
255 | case 1: return [2 , _a.sent()];
|
256 | }
|
257 | });
|
258 | });
|
259 | };
|
260 | |
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 | LargeFileUploadTask.prototype.cancel = function () {
|
267 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
268 | var cancelResponse;
|
269 | return tslib_1.__generator(this, function (_a) {
|
270 | switch (_a.label) {
|
271 | case 0: return [4 , this.client
|
272 | .api(this.uploadSession.url)
|
273 | .responseType(ResponseType_1.ResponseType.RAW)
|
274 | .delete()];
|
275 | case 1:
|
276 | cancelResponse = _a.sent();
|
277 | if (cancelResponse.status === 204) {
|
278 | this.uploadSession.isCancelled = true;
|
279 | }
|
280 | return [2 , cancelResponse];
|
281 | }
|
282 | });
|
283 | });
|
284 | };
|
285 | |
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 | LargeFileUploadTask.prototype.getStatus = function () {
|
292 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
293 | var response;
|
294 | return tslib_1.__generator(this, function (_a) {
|
295 | switch (_a.label) {
|
296 | case 0: return [4 , this.client.api(this.uploadSession.url).get()];
|
297 | case 1:
|
298 | response = _a.sent();
|
299 | this.updateTaskStatus(response);
|
300 | return [2 , response];
|
301 | }
|
302 | });
|
303 | });
|
304 | };
|
305 | |
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 | LargeFileUploadTask.prototype.resume = function () {
|
312 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
313 | return tslib_1.__generator(this, function (_a) {
|
314 | switch (_a.label) {
|
315 | case 0: return [4 , this.getStatus()];
|
316 | case 1:
|
317 | _a.sent();
|
318 | return [4 , this.upload()];
|
319 | case 2: return [2 , _a.sent()];
|
320 | }
|
321 | });
|
322 | });
|
323 | };
|
324 | |
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 | LargeFileUploadTask.prototype.getUploadSession = function () {
|
331 | return this.uploadSession;
|
332 | };
|
333 | return LargeFileUploadTask;
|
334 | }());
|
335 | exports.LargeFileUploadTask = LargeFileUploadTask;
|
336 |
|
\ | No newline at end of file |