UNPKG

7.09 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 */
7/// <reference types="node" />
8import { Client } from "../index";
9import { UploadEventHandlers } from "./FileUploadTask/Interfaces/IUploadEventHandlers";
10import { Range } from "./FileUploadTask/Range";
11import { UploadResult } from "./FileUploadTask/UploadResult";
12/**
13 * @interface
14 * Signature to representing key value pairs
15 * @property {[key: string] : string | number} - The Key value pair
16 */
17interface KeyValuePairObjectStringNumber {
18 [key: string]: string | number;
19}
20/**
21 * @interface
22 * Signature to define options for upload task
23 * @property {number} [rangeSize = LargeFileUploadTask.DEFAULT_FILE_SIZE] - Specifies the range chunk size
24 * @property {UploadEventHandlers} uploadEventHandlers - UploadEventHandlers attached to an upload task
25 */
26export interface LargeFileUploadTaskOptions {
27 rangeSize?: number;
28 uploadEventHandlers?: UploadEventHandlers;
29}
30/**
31 * @interface
32 * Signature to represent upload session resulting from the session creation in the server
33 * @property {string} url - The URL to which the file upload is made
34 * @property {Date} expiry - The expiration of the time of the upload session
35 */
36export interface LargeFileUploadSession {
37 url: string;
38 expiry: Date;
39 isCancelled?: boolean;
40}
41/**
42 * @type
43 * Representing the return type of the sliceFile function that is type of the slice of a given range.
44 */
45export declare type SliceType = ArrayBuffer | Blob | Buffer;
46/**
47 * @interface
48 * Signature to define the properties and content of the file in upload task
49 * @property {ArrayBuffer | File} content - The actual file content
50 * @property {string} name - Specifies the file name with extension
51 * @property {number} size - Specifies size of the file
52 */
53export interface FileObject<T> {
54 content: T;
55 name: string;
56 size: number;
57 sliceFile(range: Range): SliceType | Promise<SliceType>;
58}
59/**
60 * @class
61 * Class representing LargeFileUploadTask
62 */
63export declare class LargeFileUploadTask<T> {
64 /**
65 * @private
66 * Default value for the rangeSize
67 */
68 private DEFAULT_FILE_SIZE;
69 /**
70 * @protected
71 * The GraphClient instance
72 */
73 protected client: Client;
74 /**
75 * @protected
76 * The object holding file details
77 */
78 protected file: FileObject<T>;
79 /**
80 * @protected
81 * The object holding options for the task
82 */
83 protected options: LargeFileUploadTaskOptions;
84 /**
85 * @protected
86 * The object for upload session
87 */
88 protected uploadSession: LargeFileUploadSession;
89 /**
90 * @protected
91 * The next range needs to be uploaded
92 */
93 protected nextRange: Range;
94 /**
95 * @public
96 * @static
97 * @async
98 * Makes request to the server to create an upload session
99 * @param {Client} client - The GraphClient instance
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 /**
152 * @public
153 * @async
154 * Uploads given slice to the server
155 * @param {ArrayBuffer | Blob | File} fileSlice - The file slice
156 * @param {Range} range - The range value
157 * @param {number} totalSize - The total size of a complete file
158 * @returns The response body of the upload slice result
159 */
160 uploadSlice(fileSlice: ArrayBuffer | Blob | File, range: Range, totalSize: number): Promise<unknown>;
161 /**
162 * @public
163 * @async
164 * Uploads given slice to the server
165 * @param {unknown} fileSlice - The file slice
166 * @param {Range} range - The range value
167 * @param {number} totalSize - The total size of a complete file
168 * @returns The raw response of the upload slice result
169 */
170 uploadSliceGetRawResponse(fileSlice: unknown, range: Range, totalSize: number): Promise<Response>;
171 /**
172 * @public
173 * @async
174 * Deletes upload session in the server
175 * @returns The promise resolves to cancelled response
176 */
177 cancel(): Promise<unknown>;
178 /**
179 * @public
180 * @async
181 * Gets status for the upload session
182 * @returns The promise resolves to the status enquiry response
183 */
184 getStatus(): Promise<unknown>;
185 /**
186 * @public
187 * @async
188 * Resumes upload session and continue uploading the file from the last sent range
189 * @returns The promise resolves to the uploaded response
190 */
191 resume(): Promise<unknown>;
192 /**
193 * @public
194 * @async
195 * Get the upload session information
196 * @returns The large file upload session
197 */
198 getUploadSession(): LargeFileUploadSession;
199}
200export {};