UNPKG

6.96 kBTypeScriptView Raw
1/**
2 * -------------------------------------------------------------------------------------------
3 * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
4 * See License in the project root for license information.
5 * -------------------------------------------------------------------------------------------
6 */
7import { Client } from "../index";
8import { UploadEventHandlers } from "./FileUploadTask/Interfaces/IUploadEventHandlers";
9import { Range } from "./FileUploadTask/Range";
10import { UploadResult } from "./FileUploadTask/UploadResult";
11/**
12 * @interface
13 * Signature to representing key value pairs
14 * @property {[key: string] : string | number} - The Key value pair
15 */
16interface KeyValuePairObjectStringNumber {
17 [key: string]: string | number;
18}
19/**
20 * @interface
21 * Signature to define options for upload task
22 * @property {number} [rangeSize = LargeFileUploadTask.DEFAULT_FILE_SIZE] - Specifies the range chunk size
23 * @property {UploadEventHandlers} uploadEventHandlers - UploadEventHandlers attached to an upload task
24 */
25export interface LargeFileUploadTaskOptions {
26 rangeSize?: number;
27 uploadEventHandlers?: UploadEventHandlers;
28}
29/**
30 * @interface
31 * Signature to represent upload session resulting from the session creation in the server
32 * @property {string} url - The URL to which the file upload is made
33 * @property {Date} expiry - The expiration of the time of the upload session
34 */
35export interface LargeFileUploadSession {
36 url: string;
37 expiry: Date;
38 isCancelled?: boolean;
39}
40/**
41 * @type
42 * Representing the return type of the sliceFile function that is type of the slice of a given range.
43 */
44export declare type SliceType = ArrayBuffer | Blob | Uint8Array;
45/**
46 * @interface
47 * Signature to define the properties and content of the file in upload task
48 * @property {ArrayBuffer | File} content - The actual file content
49 * @property {string} name - Specifies the file name with extension
50 * @property {number} size - Specifies size of the file
51 */
52export interface FileObject<T> {
53 content: T;
54 name: string;
55 size: number;
56 sliceFile(range: Range): SliceType | Promise<SliceType>;
57}
58/**
59 * @class
60 * Class representing LargeFileUploadTask
61 */
62export declare class LargeFileUploadTask<T> {
63 /**
64 * @private
65 * Default value for the rangeSize
66 */
67 private DEFAULT_FILE_SIZE;
68 /**
69 * @protected
70 * The GraphClient instance
71 */
72 protected client: Client;
73 /**
74 * @protected
75 * The object holding file details
76 */
77 protected file: FileObject<T>;
78 /**
79 * @protected
80 * The object holding options for the task
81 */
82 protected options: LargeFileUploadTaskOptions;
83 /**
84 * @protected
85 * The object for upload session
86 */
87 protected uploadSession: LargeFileUploadSession;
88 /**
89 * @protected
90 * The next range needs to be uploaded
91 */
92 protected nextRange: Range;
93 /**
94 * @public
95 * @static
96 * @async
97 * Makes request to the server to create an upload session
98 * @param {Client} client - The GraphClient instance
99 * @param {string} requestUrl - The URL to create the upload session
100 * @param {any} payload - The payload that needs to be sent
101 * @param {KeyValuePairObjectStringNumber} headers - The headers that needs to be sent
102 * @returns The promise that resolves to LargeFileUploadSession
103 */
104 static createUploadSession(client: Client, requestUrl: string, payload: any, headers?: KeyValuePairObjectStringNumber): Promise<LargeFileUploadSession>;
105 /**
106 * @public
107 * @constructor
108 * Constructs a LargeFileUploadTask
109 * @param {Client} client - The GraphClient instance
110 * @param {FileObject} file - The FileObject holding details of a file that needs to be uploaded
111 * @param {LargeFileUploadSession} uploadSession - The upload session to which the upload has to be done
112 * @param {LargeFileUploadTaskOptions} options - The upload task options
113 * @returns An instance of LargeFileUploadTask
114 */
115 constructor(client: Client, file: FileObject<T>, uploadSession: LargeFileUploadSession, options?: LargeFileUploadTaskOptions);
116 /**
117 * @private
118 * Parses given range string to the Range instance
119 * @param {string[]} ranges - The ranges value
120 * @returns The range instance
121 */
122 private parseRange;
123 /**
124 * @private
125 * Updates the expiration date and the next range
126 * @param {UploadStatusResponse} response - The response of the upload status
127 * @returns Nothing
128 */
129 private updateTaskStatus;
130 /**
131 * @public
132 * Gets next range that needs to be uploaded
133 * @returns The range instance
134 */
135 getNextRange(): Range;
136 /**
137 * @deprecated This function has been moved into FileObject interface.
138 * @public
139 * Slices the file content to the given range
140 * @param {Range} range - The range value
141 * @returns The sliced ArrayBuffer or Blob
142 */
143 sliceFile(range: Range): ArrayBuffer | Blob;
144 /**
145 * @public
146 * @async
147 * Uploads file to the server in a sequential order by slicing the file
148 * @returns The promise resolves to uploaded response
149 */
150 upload(): Promise<UploadResult>;
151 private reportProgress;
152 /**
153 * @public
154 * @async
155 * Uploads given slice to the server
156 * @param {ArrayBuffer | Blob | File} fileSlice - The file slice
157 * @param {Range} range - The range value
158 * @param {number} totalSize - The total size of a complete file
159 * @returns The response body of the upload slice result
160 */
161 uploadSlice(fileSlice: ArrayBuffer | Blob | File, range: Range, totalSize: number): Promise<unknown>;
162 /**
163 * @public
164 * @async
165 * Uploads given slice to the server
166 * @param {unknown} fileSlice - The file slice
167 * @param {Range} range - The range value
168 * @param {number} totalSize - The total size of a complete file
169 * @returns The raw response of the upload slice result
170 */
171 uploadSliceGetRawResponse(fileSlice: unknown, range: Range, totalSize: number): Promise<Response>;
172 /**
173 * @public
174 * @async
175 * Deletes upload session in the server
176 * @returns The promise resolves to cancelled response
177 */
178 cancel(): Promise<unknown>;
179 /**
180 * @public
181 * @async
182 * Gets status for the upload session
183 * @returns The promise resolves to the status enquiry response
184 */
185 getStatus(): Promise<unknown>;
186 /**
187 * @public
188 * @async
189 * Resumes upload session and continue uploading the file from the last sent range
190 * @returns The promise resolves to the uploaded response
191 */
192 resume(): Promise<unknown>;
193 /**
194 * @public
195 * @async
196 * Get the upload session information
197 * @returns The large file upload session
198 */
199 getUploadSession(): LargeFileUploadSession;
200}
201export {};