-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
fileuploader.ts
277 lines (229 loc) · 6.48 KB
/
fileuploader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module cloud-services/uploadgateway/fileuploader
*/
/* globals XMLHttpRequest, FormData, Blob, atob */
import type { UploadResponse } from 'ckeditor5/src/upload.js';
import { EmitterMixin, CKEditorError } from 'ckeditor5/src/utils.js';
import type { InitializedToken } from '../token/token.js';
const BASE64_HEADER_REG_EXP = /^data:(\S*?);base64,/;
/**
* FileUploader class used to upload single file.
*/
export default class FileUploader extends EmitterMixin() {
/**
* A file that is being uploaded.
*/
public readonly file: Blob;
public xhr?: XMLHttpRequest;
/**
* CKEditor Cloud Services access token.
*/
private readonly _token: InitializedToken;
/**
* CKEditor Cloud Services API address.
*/
private readonly _apiAddress: string;
/**
* Creates `FileUploader` instance.
*
* @param fileOrData A blob object or a data string encoded with Base64.
* @param token Token used for authentication.
* @param apiAddress API address.
*/
constructor(
fileOrData: string | Blob,
token: InitializedToken,
apiAddress: string
) {
super();
if ( !fileOrData ) {
/**
* File must be provided as the first argument.
*
* @error fileuploader-missing-file
*/
throw new CKEditorError( 'fileuploader-missing-file', null );
}
if ( !token ) {
/**
* Token must be provided as the second argument.
*
* @error fileuploader-missing-token
*/
throw new CKEditorError( 'fileuploader-missing-token', null );
}
if ( !apiAddress ) {
/**
* Api address must be provided as the third argument.
*
* @error fileuploader-missing-api-address
*/
throw new CKEditorError( 'fileuploader-missing-api-address', null );
}
this.file = _isBase64( fileOrData ) ? _base64ToBlob( fileOrData ) : fileOrData;
this._token = token;
this._apiAddress = apiAddress;
}
/**
* Registers callback on `progress` event.
*/
public onProgress( callback: ( status: { total: number; uploaded: number } ) => void ): this {
this.on<FileUploaderProgressErrorEvent>( 'progress', ( event, data ) => callback( data ) );
return this;
}
/**
* Registers callback on `error` event. Event is called once when error occurs.
*/
public onError( callback: ( error: string ) => void ): this {
this.once<FileUploaderErrorEvent>( 'error', ( event, data ) => callback( data ) );
return this;
}
/**
* Aborts upload process.
*/
public abort(): void {
this.xhr!.abort();
}
/**
* Sends XHR request to API.
*/
public send(): Promise<UploadResponse> {
this._prepareRequest();
this._attachXHRListeners();
return this._sendRequest();
}
/**
* Prepares XHR request.
*/
private _prepareRequest() {
const xhr = new XMLHttpRequest();
xhr.open( 'POST', this._apiAddress );
xhr.setRequestHeader( 'Authorization', this._token.value );
xhr.responseType = 'json';
this.xhr = xhr;
}
/**
* Attaches listeners to the XHR.
*/
private _attachXHRListeners() {
const xhr = this.xhr!;
const onError = ( message: string ) => {
return () => this.fire<FileUploaderErrorEvent>( 'error', message );
};
xhr.addEventListener( 'error', onError( 'Network Error' ) );
xhr.addEventListener( 'abort', onError( 'Abort' ) );
/* istanbul ignore else -- @preserve */
if ( xhr.upload ) {
xhr.upload.addEventListener( 'progress', event => {
if ( event.lengthComputable ) {
this.fire<FileUploaderProgressErrorEvent>( 'progress', {
total: event.total,
uploaded: event.loaded
} );
}
} );
}
xhr.addEventListener( 'load', () => {
const statusCode = xhr.status;
const xhrResponse = xhr.response;
if ( statusCode < 200 || statusCode > 299 ) {
return this.fire<FileUploaderErrorEvent>( 'error', xhrResponse.message || xhrResponse.error );
}
} );
}
/**
* Sends XHR request.
*/
private _sendRequest() {
const formData = new FormData();
const xhr = this.xhr!;
formData.append( 'file', this.file );
return new Promise<UploadResponse>( ( resolve, reject ) => {
xhr.addEventListener( 'load', () => {
const statusCode = xhr.status;
const xhrResponse = xhr.response;
if ( statusCode < 200 || statusCode > 299 ) {
if ( xhrResponse.message ) {
/**
* Uploading file failed.
*
* @error fileuploader-uploading-data-failed
*/
return reject( new CKEditorError(
'fileuploader-uploading-data-failed',
this,
{ message: xhrResponse.message }
) );
}
return reject( xhrResponse.error );
}
return resolve( xhrResponse );
} );
xhr.addEventListener( 'error', () => reject( new Error( 'Network Error' ) ) );
xhr.addEventListener( 'abort', () => reject( new Error( 'Abort' ) ) );
xhr.send( formData );
} );
}
}
/**
* Fired when error occurs.
*
* @eventName ~FileUploader#error
* @param error Error message
*/
export type FileUploaderErrorEvent = {
name: 'error';
args: [ error: string ];
};
/**
* Fired on upload progress.
*
* @eventName ~FileUploader#progress
* @param status Total and uploaded status
*/
export type FileUploaderProgressErrorEvent = {
name: 'progress';
args: [ status: { total: number; uploaded: number } ];
};
/**
* Transforms Base64 string data into file.
*
* @param base64 String data.
*/
function _base64ToBlob( base64: string, sliceSize = 512 ) {
try {
const contentType = base64.match( BASE64_HEADER_REG_EXP )![ 1 ];
const base64Data = atob( base64.replace( BASE64_HEADER_REG_EXP, '' ) );
const byteArrays: Array<Uint8Array> = [];
for ( let offset = 0; offset < base64Data.length; offset += sliceSize ) {
const slice = base64Data.slice( offset, offset + sliceSize );
const byteNumbers = new Array<number>( slice.length );
for ( let i = 0; i < slice.length; i++ ) {
byteNumbers[ i ] = slice.charCodeAt( i );
}
byteArrays.push( new Uint8Array( byteNumbers ) );
}
return new Blob( byteArrays, { type: contentType } );
} catch ( error ) {
/**
* Problem with decoding Base64 image data.
*
* @error fileuploader-decoding-image-data-error
*/
throw new CKEditorError( 'fileuploader-decoding-image-data-error', null );
}
}
/**
* Checks that string is Base64.
*/
function _isBase64( string: string | Blob ): string is string {
if ( typeof string !== 'string' ) {
return false;
}
const match = string.match( BASE64_HEADER_REG_EXP );
return !!( match && match.length );
}