1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | exports.OneDriveLargeFileUploadTask = void 0;
|
10 | var tslib_1 = require("tslib");
|
11 |
|
12 |
|
13 |
|
14 | var GraphClientError_1 = require("../GraphClientError");
|
15 | var FileUpload_1 = require("./FileUploadTask/FileObjectClasses/FileUpload");
|
16 | var LargeFileUploadTask_1 = require("./LargeFileUploadTask");
|
17 | var OneDriveLargeFileUploadTaskUtil_1 = require("./OneDriveLargeFileUploadTaskUtil");
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var OneDriveLargeFileUploadTask = (function (_super) {
|
23 | tslib_1.__extends(OneDriveLargeFileUploadTask, _super);
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function OneDriveLargeFileUploadTask(client, file, uploadSession, options) {
|
35 | return _super.call(this, client, file, uploadSession, options) || this;
|
36 | }
|
37 | |
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | OneDriveLargeFileUploadTask.constructCreateSessionUrl = function (fileName, path) {
|
46 | if (path === void 0) { path = OneDriveLargeFileUploadTask.DEFAULT_UPLOAD_PATH; }
|
47 | fileName = fileName.trim();
|
48 | path = path.trim();
|
49 | if (path === "") {
|
50 | path = "/";
|
51 | }
|
52 | if (path[0] !== "/") {
|
53 | path = "/".concat(path);
|
54 | }
|
55 | if (path[path.length - 1] !== "/") {
|
56 | path = "".concat(path, "/");
|
57 | }
|
58 |
|
59 |
|
60 | return "/me/drive/root:".concat(path
|
61 | .split("/")
|
62 | .map(function (p) { return encodeURIComponent(p); })
|
63 | .join("/")).concat(encodeURIComponent(fileName), ":/createUploadSession");
|
64 | };
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | OneDriveLargeFileUploadTask.getFileInfo = function (file, fileName) {
|
74 | var content;
|
75 | var size;
|
76 | if (typeof Blob !== "undefined" && file instanceof Blob) {
|
77 | content = new File([file], fileName);
|
78 | size = content.size;
|
79 | }
|
80 | else if (typeof File !== "undefined" && file instanceof File) {
|
81 | content = file;
|
82 | size = content.size;
|
83 | }
|
84 | else if (typeof Uint8Array !== "undefined" && file instanceof Uint8Array) {
|
85 | var b = file;
|
86 | size = b.byteLength;
|
87 | content = b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);
|
88 | }
|
89 | return {
|
90 | content: content,
|
91 | size: size,
|
92 | };
|
93 | };
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | OneDriveLargeFileUploadTask.create = function (client, file, options) {
|
105 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
106 | var fileName, fileInfo, fileObj;
|
107 | return tslib_1.__generator(this, function (_a) {
|
108 | if (!client || !file || !options) {
|
109 | throw new GraphClientError_1.GraphClientError("Please provide the Graph client instance, file object and OneDriveLargeFileUploadOptions value");
|
110 | }
|
111 | fileName = options.fileName;
|
112 | fileInfo = OneDriveLargeFileUploadTask.getFileInfo(file, fileName);
|
113 | fileObj = new FileUpload_1.FileUpload(fileInfo.content, fileName, fileInfo.size);
|
114 | return [2 , this.createTaskWithFileObject(client, fileObj, options)];
|
115 | });
|
116 | });
|
117 | };
|
118 | |
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | OneDriveLargeFileUploadTask.createTaskWithFileObject = function (client, fileObject, options) {
|
129 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
130 | var requestUrl, uploadSessionPayload, session, rangeSize;
|
131 | return tslib_1.__generator(this, function (_a) {
|
132 | switch (_a.label) {
|
133 | case 0:
|
134 | if (!client || !fileObject || !options) {
|
135 | throw new GraphClientError_1.GraphClientError("Please provide the Graph client instance, FileObject interface implementation and OneDriveLargeFileUploadOptions value");
|
136 | }
|
137 | requestUrl = options.uploadSessionURL ? options.uploadSessionURL : OneDriveLargeFileUploadTask.constructCreateSessionUrl(options.fileName, options.path);
|
138 | uploadSessionPayload = {
|
139 | fileName: options.fileName,
|
140 | fileDescription: options.fileDescription,
|
141 | conflictBehavior: options.conflictBehavior,
|
142 | };
|
143 | return [4 , OneDriveLargeFileUploadTask.createUploadSession(client, requestUrl, uploadSessionPayload)];
|
144 | case 1:
|
145 | session = _a.sent();
|
146 | rangeSize = (0, OneDriveLargeFileUploadTaskUtil_1.getValidRangeSize)(options.rangeSize);
|
147 | return [2 , new OneDriveLargeFileUploadTask(client, fileObject, session, {
|
148 | rangeSize: rangeSize,
|
149 | uploadEventHandlers: options.uploadEventHandlers,
|
150 | })];
|
151 | }
|
152 | });
|
153 | });
|
154 | };
|
155 | |
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | OneDriveLargeFileUploadTask.createUploadSession = function (client, requestUrl, payloadOptions) {
|
166 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
167 | var payload;
|
168 | return tslib_1.__generator(this, function (_a) {
|
169 | payload = {
|
170 | item: {
|
171 | "@microsoft.graph.conflictBehavior": (payloadOptions === null || payloadOptions === void 0 ? void 0 : payloadOptions.conflictBehavior) || "rename",
|
172 | name: payloadOptions === null || payloadOptions === void 0 ? void 0 : payloadOptions.fileName,
|
173 | description: payloadOptions === null || payloadOptions === void 0 ? void 0 : payloadOptions.fileDescription,
|
174 | },
|
175 | };
|
176 | return [2 , _super.createUploadSession.call(this, client, requestUrl, payload)];
|
177 | });
|
178 | });
|
179 | };
|
180 | |
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | OneDriveLargeFileUploadTask.prototype.commit = function (requestUrl, conflictBehavior) {
|
188 | if (conflictBehavior === void 0) { conflictBehavior = "rename"; }
|
189 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
190 | var payload;
|
191 | return tslib_1.__generator(this, function (_a) {
|
192 | switch (_a.label) {
|
193 | case 0:
|
194 | payload = {
|
195 | name: this.file.name,
|
196 | "@microsoft.graph.conflictBehavior": conflictBehavior,
|
197 | "@microsoft.graph.sourceUrl": this.uploadSession.url,
|
198 | };
|
199 | return [4 , this.client.api(requestUrl).put(payload)];
|
200 | case 1: return [2 , _a.sent()];
|
201 | }
|
202 | });
|
203 | });
|
204 | };
|
205 | |
206 |
|
207 |
|
208 |
|
209 |
|
210 | OneDriveLargeFileUploadTask.DEFAULT_UPLOAD_PATH = "/";
|
211 | return OneDriveLargeFileUploadTask;
|
212 | }(LargeFileUploadTask_1.LargeFileUploadTask));
|
213 | exports.OneDriveLargeFileUploadTask = OneDriveLargeFileUploadTask;
|
214 |
|
\ | No newline at end of file |