UNPKG

10.4 kBJavaScriptView Raw
1"use strict";
2/**
3 * -------------------------------------------------------------------------------------------
4 * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
5 * See License in the project root for license information.
6 * -------------------------------------------------------------------------------------------
7 */
8Object.defineProperty(exports, "__esModule", { value: true });
9exports.OneDriveLargeFileUploadTask = void 0;
10var tslib_1 = require("tslib");
11/**
12 * @module OneDriveLargeFileUploadTask
13 */
14var GraphClientError_1 = require("../GraphClientError");
15var FileUpload_1 = require("./FileUploadTask/FileObjectClasses/FileUpload");
16var LargeFileUploadTask_1 = require("./LargeFileUploadTask");
17var OneDriveLargeFileUploadTaskUtil_1 = require("./OneDriveLargeFileUploadTaskUtil");
18/**
19 * @class
20 * Class representing OneDriveLargeFileUploadTask
21 */
22var OneDriveLargeFileUploadTask = /** @class */ (function (_super) {
23 (0, tslib_1.__extends)(OneDriveLargeFileUploadTask, _super);
24 /**
25 * @public
26 * @constructor
27 * Constructs a OneDriveLargeFileUploadTask
28 * @param {Client} client - The GraphClient instance
29 * @param {FileObject} file - The FileObject holding details of a file that needs to be uploaded
30 * @param {LargeFileUploadSession} uploadSession - The upload session to which the upload has to be done
31 * @param {LargeFileUploadTaskOptions} options - The upload task options
32 * @returns An instance of OneDriveLargeFileUploadTask
33 */
34 function OneDriveLargeFileUploadTask(client, file, uploadSession, options) {
35 return _super.call(this, client, file, uploadSession, options) || this;
36 }
37 /**
38 * @private
39 * @static
40 * Constructs the create session url for Onedrive
41 * @param {string} fileName - The name of the file
42 * @param {path} [path = OneDriveLargeFileUploadTask.DEFAULT_UPLOAD_PATH] - The path for the upload
43 * @returns The constructed create session url
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 // we choose to encode each component of the file path separately because when encoding full URI
59 // with encodeURI, special characters like # or % in the file name doesn't get encoded as desired
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 * @private
67 * @static
68 * Get file information
69 * @param {Blob | Uint8Array | File} file - The file entity
70 * @param {string} fileName - The file name
71 * @returns {FileInfo} The file information
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 * @public
96 * @static
97 * @async
98 * Creates a OneDriveLargeFileUploadTask
99 * @param {Client} client - The GraphClient instance
100 * @param {Blob | Uint8Array | File} file - File represented as Blob, Uint8Array or File
101 * @param {OneDriveLargeFileUploadOptions} options - The options for upload task
102 * @returns The promise that will be resolves to OneDriveLargeFileUploadTask instance
103 */
104 OneDriveLargeFileUploadTask.create = function (client, file, options) {
105 return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
106 var fileName, fileInfo, fileObj;
107 return (0, 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 /*return*/, this.createTaskWithFileObject(client, fileObj, options)];
115 });
116 });
117 };
118 /**
119 * @public
120 * @static
121 * @async
122 * Creates a OneDriveLargeFileUploadTask
123 * @param {Client} client - The GraphClient instance
124 * @param {FileObject} fileObject - FileObject instance
125 * @param {OneDriveLargeFileUploadOptions} options - The options for upload task
126 * @returns The promise that will be resolves to OneDriveLargeFileUploadTask instance
127 */
128 OneDriveLargeFileUploadTask.createTaskWithFileObject = function (client, fileObject, options) {
129 return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
130 var requestUrl, uploadSessionPayload, session, rangeSize;
131 return (0, 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 = OneDriveLargeFileUploadTask.constructCreateSessionUrl(options.fileName, options.path);
138 uploadSessionPayload = {
139 fileName: options.fileName,
140 fileDescription: options.fileDescription,
141 conflictBehavior: options.conflictBehavior,
142 };
143 return [4 /*yield*/, OneDriveLargeFileUploadTask.createUploadSession(client, requestUrl, uploadSessionPayload)];
144 case 1:
145 session = _a.sent();
146 rangeSize = (0, OneDriveLargeFileUploadTaskUtil_1.getValidRangeSize)(options.rangeSize);
147 return [2 /*return*/, new OneDriveLargeFileUploadTask(client, fileObject, session, {
148 rangeSize: rangeSize,
149 uploadEventHandlers: options.uploadEventHandlers,
150 })];
151 }
152 });
153 });
154 };
155 /**
156 * @public
157 * @static
158 * @async
159 * Makes request to the server to create an upload session
160 * @param {Client} client - The GraphClient instance
161 * @param {string} requestUrl - The URL to create the upload session
162 * @param {string} payloadOptions - The payload option. Default conflictBehavior is 'rename'
163 * @returns The promise that resolves to LargeFileUploadSession
164 */
165 OneDriveLargeFileUploadTask.createUploadSession = function (client, requestUrl, payloadOptions) {
166 return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
167 var payload;
168 return (0, 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 /*return*/, _super.createUploadSession.call(this, client, requestUrl, payload)];
177 });
178 });
179 };
180 /**
181 * @public
182 * Commits upload session to end uploading
183 * @param {string} requestUrl - The URL to commit the upload session
184 * @param {string} conflictBehavior - Conflict behaviour option. Default is 'rename'
185 * @returns The promise resolves to committed response
186 */
187 OneDriveLargeFileUploadTask.prototype.commit = function (requestUrl, conflictBehavior) {
188 if (conflictBehavior === void 0) { conflictBehavior = "rename"; }
189 return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
190 var payload;
191 return (0, 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 /*yield*/, this.client.api(requestUrl).put(payload)];
200 case 1: return [2 /*return*/, _a.sent()];
201 }
202 });
203 });
204 };
205 /**
206 * @private
207 * @static
208 * Default path for the file being uploaded
209 */
210 OneDriveLargeFileUploadTask.DEFAULT_UPLOAD_PATH = "/";
211 return OneDriveLargeFileUploadTask;
212}(LargeFileUploadTask_1.LargeFileUploadTask));
213exports.OneDriveLargeFileUploadTask = OneDriveLargeFileUploadTask;
214//# sourceMappingURL=OneDriveLargeFileUploadTask.js.map
\No newline at end of file