@ckeditor/ckeditor5-cloud-services 40.0.0 → 40.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -20
- package/LICENSE.md +2 -2
- package/package.json +2 -2
- package/src/augmentation.d.ts +19 -19
- package/src/augmentation.js +5 -5
- package/src/cloudservices.d.ts +84 -84
- package/src/cloudservices.js +103 -103
- package/src/cloudservicesconfig.d.ts +121 -121
- package/src/cloudservicesconfig.js +5 -5
- package/src/cloudservicescore.d.ts +36 -36
- package/src/cloudservicescore.js +41 -41
- package/src/index.d.ts +14 -14
- package/src/index.js +10 -10
- package/src/token/token.d.ts +96 -96
- package/src/token/token.js +171 -171
- package/src/uploadgateway/fileuploader.d.ts +94 -94
- package/src/uploadgateway/fileuploader.js +183 -183
- package/src/uploadgateway/uploadgateway.d.ts +47 -47
- package/src/uploadgateway/uploadgateway.js +60 -60
- package/build/cloud-services.js.map +0 -1
package/src/token/token.js
CHANGED
|
@@ -1,171 +1,171 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @module cloud-services/token/token
|
|
7
|
-
*/
|
|
8
|
-
/* globals XMLHttpRequest, setTimeout, clearTimeout, atob */
|
|
9
|
-
import { ObservableMixin, CKEditorError } from 'ckeditor5/src/utils';
|
|
10
|
-
const DEFAULT_OPTIONS = { autoRefresh: true };
|
|
11
|
-
const DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME = 3600000;
|
|
12
|
-
/**
|
|
13
|
-
* Class representing the token used for communication with CKEditor Cloud Services.
|
|
14
|
-
* Value of the token is retrieving from the specified URL and is refreshed every 1 hour by default.
|
|
15
|
-
*/
|
|
16
|
-
export default class Token extends ObservableMixin() {
|
|
17
|
-
/**
|
|
18
|
-
* Creates `Token` instance.
|
|
19
|
-
* Method `init` should be called after using the constructor or use `create` method instead.
|
|
20
|
-
*
|
|
21
|
-
* @param tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
|
|
22
|
-
* value is a function it has to match the {@link module:cloud-services/token/token~Token#refreshToken} interface.
|
|
23
|
-
*/
|
|
24
|
-
constructor(tokenUrlOrRefreshToken, options = {}) {
|
|
25
|
-
super();
|
|
26
|
-
if (!tokenUrlOrRefreshToken) {
|
|
27
|
-
/**
|
|
28
|
-
* A `tokenUrl` must be provided as the first constructor argument.
|
|
29
|
-
*
|
|
30
|
-
* @error token-missing-token-url
|
|
31
|
-
*/
|
|
32
|
-
throw new CKEditorError('token-missing-token-url', this);
|
|
33
|
-
}
|
|
34
|
-
if (options.initValue) {
|
|
35
|
-
this._validateTokenValue(options.initValue);
|
|
36
|
-
}
|
|
37
|
-
this.set('value', options.initValue);
|
|
38
|
-
if (typeof tokenUrlOrRefreshToken === 'function') {
|
|
39
|
-
this._refresh = tokenUrlOrRefreshToken;
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
this._refresh = () => defaultRefreshToken(tokenUrlOrRefreshToken);
|
|
43
|
-
}
|
|
44
|
-
this._options = { ...DEFAULT_OPTIONS, ...options };
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Initializes the token.
|
|
48
|
-
*/
|
|
49
|
-
init() {
|
|
50
|
-
return new Promise((resolve, reject) => {
|
|
51
|
-
if (!this.value) {
|
|
52
|
-
this.refreshToken()
|
|
53
|
-
.then(resolve)
|
|
54
|
-
.catch(reject);
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
if (this._options.autoRefresh) {
|
|
58
|
-
this._registerRefreshTokenTimeout();
|
|
59
|
-
}
|
|
60
|
-
resolve(this);
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Refresh token method. Useful in a method form as it can be override in tests.
|
|
65
|
-
*/
|
|
66
|
-
refreshToken() {
|
|
67
|
-
return this._refresh()
|
|
68
|
-
.then(value => {
|
|
69
|
-
this._validateTokenValue(value);
|
|
70
|
-
this.set('value', value);
|
|
71
|
-
if (this._options.autoRefresh) {
|
|
72
|
-
this._registerRefreshTokenTimeout();
|
|
73
|
-
}
|
|
74
|
-
return this;
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Destroys token instance. Stops refreshing.
|
|
79
|
-
*/
|
|
80
|
-
destroy() {
|
|
81
|
-
clearTimeout(this._tokenRefreshTimeout);
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Checks whether the provided token follows the JSON Web Tokens (JWT) format.
|
|
85
|
-
*
|
|
86
|
-
* @param tokenValue The token to validate.
|
|
87
|
-
*/
|
|
88
|
-
_validateTokenValue(tokenValue) {
|
|
89
|
-
// The token must be a string.
|
|
90
|
-
const isString = typeof tokenValue === 'string';
|
|
91
|
-
// The token must be a plain string without quotes ("").
|
|
92
|
-
const isPlainString = !/^".*"$/.test(tokenValue);
|
|
93
|
-
// JWT token contains 3 parts: header, payload, and signature.
|
|
94
|
-
// Each part is separated by a dot.
|
|
95
|
-
const isJWTFormat = isString && tokenValue.split('.').length === 3;
|
|
96
|
-
if (!(isPlainString && isJWTFormat)) {
|
|
97
|
-
/**
|
|
98
|
-
* The provided token must follow the [JSON Web Tokens](https://jwt.io/introduction/) format.
|
|
99
|
-
*
|
|
100
|
-
* @error token-not-in-jwt-format
|
|
101
|
-
*/
|
|
102
|
-
throw new CKEditorError('token-not-in-jwt-format', this);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Registers a refresh token timeout for the time taken from token.
|
|
107
|
-
*/
|
|
108
|
-
_registerRefreshTokenTimeout() {
|
|
109
|
-
const tokenRefreshTimeoutTime = this._getTokenRefreshTimeoutTime();
|
|
110
|
-
clearTimeout(this._tokenRefreshTimeout);
|
|
111
|
-
this._tokenRefreshTimeout = setTimeout(() => {
|
|
112
|
-
this.refreshToken();
|
|
113
|
-
}, tokenRefreshTimeoutTime);
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Returns token refresh timeout time calculated from expire time in the token payload.
|
|
117
|
-
*
|
|
118
|
-
* If the token parse fails or the token payload doesn't contain, the default DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME is returned.
|
|
119
|
-
*/
|
|
120
|
-
_getTokenRefreshTimeoutTime() {
|
|
121
|
-
try {
|
|
122
|
-
const [, binaryTokenPayload] = this.value.split('.');
|
|
123
|
-
const { exp: tokenExpireTime } = JSON.parse(atob(binaryTokenPayload));
|
|
124
|
-
if (!tokenExpireTime) {
|
|
125
|
-
return DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME;
|
|
126
|
-
}
|
|
127
|
-
const tokenRefreshTimeoutTime = Math.floor(((tokenExpireTime * 1000) - Date.now()) / 2);
|
|
128
|
-
return tokenRefreshTimeoutTime;
|
|
129
|
-
}
|
|
130
|
-
catch (err) {
|
|
131
|
-
return DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Creates a initialized {@link module:cloud-services/token/token~Token} instance.
|
|
136
|
-
*
|
|
137
|
-
* @param tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
|
|
138
|
-
* value is a function it has to match the {@link module:cloud-services/token/token~Token#refreshToken} interface.
|
|
139
|
-
*/
|
|
140
|
-
static create(tokenUrlOrRefreshToken, options = {}) {
|
|
141
|
-
const token = new Token(tokenUrlOrRefreshToken, options);
|
|
142
|
-
return token.init();
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* This function is called in a defined interval by the {@link ~Token} class. It also can be invoked manually.
|
|
147
|
-
* It should return a promise, which resolves with the new token value.
|
|
148
|
-
* If any error occurs it should return a rejected promise with an error message.
|
|
149
|
-
*/
|
|
150
|
-
function defaultRefreshToken(tokenUrl) {
|
|
151
|
-
return new Promise((resolve, reject) => {
|
|
152
|
-
const xhr = new XMLHttpRequest();
|
|
153
|
-
xhr.open('GET', tokenUrl);
|
|
154
|
-
xhr.addEventListener('load', () => {
|
|
155
|
-
const statusCode = xhr.status;
|
|
156
|
-
const xhrResponse = xhr.response;
|
|
157
|
-
if (statusCode < 200 || statusCode > 299) {
|
|
158
|
-
/**
|
|
159
|
-
* Cannot download new token from the provided url.
|
|
160
|
-
*
|
|
161
|
-
* @error token-cannot-download-new-token
|
|
162
|
-
*/
|
|
163
|
-
return reject(new CKEditorError('token-cannot-download-new-token', null));
|
|
164
|
-
}
|
|
165
|
-
return resolve(xhrResponse);
|
|
166
|
-
});
|
|
167
|
-
xhr.addEventListener('error', () => reject(new Error('Network Error')));
|
|
168
|
-
xhr.addEventListener('abort', () => reject(new Error('Abort')));
|
|
169
|
-
xhr.send();
|
|
170
|
-
});
|
|
171
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module cloud-services/token/token
|
|
7
|
+
*/
|
|
8
|
+
/* globals XMLHttpRequest, setTimeout, clearTimeout, atob */
|
|
9
|
+
import { ObservableMixin, CKEditorError } from 'ckeditor5/src/utils';
|
|
10
|
+
const DEFAULT_OPTIONS = { autoRefresh: true };
|
|
11
|
+
const DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME = 3600000;
|
|
12
|
+
/**
|
|
13
|
+
* Class representing the token used for communication with CKEditor Cloud Services.
|
|
14
|
+
* Value of the token is retrieving from the specified URL and is refreshed every 1 hour by default.
|
|
15
|
+
*/
|
|
16
|
+
export default class Token extends ObservableMixin() {
|
|
17
|
+
/**
|
|
18
|
+
* Creates `Token` instance.
|
|
19
|
+
* Method `init` should be called after using the constructor or use `create` method instead.
|
|
20
|
+
*
|
|
21
|
+
* @param tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
|
|
22
|
+
* value is a function it has to match the {@link module:cloud-services/token/token~Token#refreshToken} interface.
|
|
23
|
+
*/
|
|
24
|
+
constructor(tokenUrlOrRefreshToken, options = {}) {
|
|
25
|
+
super();
|
|
26
|
+
if (!tokenUrlOrRefreshToken) {
|
|
27
|
+
/**
|
|
28
|
+
* A `tokenUrl` must be provided as the first constructor argument.
|
|
29
|
+
*
|
|
30
|
+
* @error token-missing-token-url
|
|
31
|
+
*/
|
|
32
|
+
throw new CKEditorError('token-missing-token-url', this);
|
|
33
|
+
}
|
|
34
|
+
if (options.initValue) {
|
|
35
|
+
this._validateTokenValue(options.initValue);
|
|
36
|
+
}
|
|
37
|
+
this.set('value', options.initValue);
|
|
38
|
+
if (typeof tokenUrlOrRefreshToken === 'function') {
|
|
39
|
+
this._refresh = tokenUrlOrRefreshToken;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this._refresh = () => defaultRefreshToken(tokenUrlOrRefreshToken);
|
|
43
|
+
}
|
|
44
|
+
this._options = { ...DEFAULT_OPTIONS, ...options };
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Initializes the token.
|
|
48
|
+
*/
|
|
49
|
+
init() {
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
if (!this.value) {
|
|
52
|
+
this.refreshToken()
|
|
53
|
+
.then(resolve)
|
|
54
|
+
.catch(reject);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (this._options.autoRefresh) {
|
|
58
|
+
this._registerRefreshTokenTimeout();
|
|
59
|
+
}
|
|
60
|
+
resolve(this);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Refresh token method. Useful in a method form as it can be override in tests.
|
|
65
|
+
*/
|
|
66
|
+
refreshToken() {
|
|
67
|
+
return this._refresh()
|
|
68
|
+
.then(value => {
|
|
69
|
+
this._validateTokenValue(value);
|
|
70
|
+
this.set('value', value);
|
|
71
|
+
if (this._options.autoRefresh) {
|
|
72
|
+
this._registerRefreshTokenTimeout();
|
|
73
|
+
}
|
|
74
|
+
return this;
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Destroys token instance. Stops refreshing.
|
|
79
|
+
*/
|
|
80
|
+
destroy() {
|
|
81
|
+
clearTimeout(this._tokenRefreshTimeout);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Checks whether the provided token follows the JSON Web Tokens (JWT) format.
|
|
85
|
+
*
|
|
86
|
+
* @param tokenValue The token to validate.
|
|
87
|
+
*/
|
|
88
|
+
_validateTokenValue(tokenValue) {
|
|
89
|
+
// The token must be a string.
|
|
90
|
+
const isString = typeof tokenValue === 'string';
|
|
91
|
+
// The token must be a plain string without quotes ("").
|
|
92
|
+
const isPlainString = !/^".*"$/.test(tokenValue);
|
|
93
|
+
// JWT token contains 3 parts: header, payload, and signature.
|
|
94
|
+
// Each part is separated by a dot.
|
|
95
|
+
const isJWTFormat = isString && tokenValue.split('.').length === 3;
|
|
96
|
+
if (!(isPlainString && isJWTFormat)) {
|
|
97
|
+
/**
|
|
98
|
+
* The provided token must follow the [JSON Web Tokens](https://jwt.io/introduction/) format.
|
|
99
|
+
*
|
|
100
|
+
* @error token-not-in-jwt-format
|
|
101
|
+
*/
|
|
102
|
+
throw new CKEditorError('token-not-in-jwt-format', this);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Registers a refresh token timeout for the time taken from token.
|
|
107
|
+
*/
|
|
108
|
+
_registerRefreshTokenTimeout() {
|
|
109
|
+
const tokenRefreshTimeoutTime = this._getTokenRefreshTimeoutTime();
|
|
110
|
+
clearTimeout(this._tokenRefreshTimeout);
|
|
111
|
+
this._tokenRefreshTimeout = setTimeout(() => {
|
|
112
|
+
this.refreshToken();
|
|
113
|
+
}, tokenRefreshTimeoutTime);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Returns token refresh timeout time calculated from expire time in the token payload.
|
|
117
|
+
*
|
|
118
|
+
* If the token parse fails or the token payload doesn't contain, the default DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME is returned.
|
|
119
|
+
*/
|
|
120
|
+
_getTokenRefreshTimeoutTime() {
|
|
121
|
+
try {
|
|
122
|
+
const [, binaryTokenPayload] = this.value.split('.');
|
|
123
|
+
const { exp: tokenExpireTime } = JSON.parse(atob(binaryTokenPayload));
|
|
124
|
+
if (!tokenExpireTime) {
|
|
125
|
+
return DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME;
|
|
126
|
+
}
|
|
127
|
+
const tokenRefreshTimeoutTime = Math.floor(((tokenExpireTime * 1000) - Date.now()) / 2);
|
|
128
|
+
return tokenRefreshTimeoutTime;
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
return DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Creates a initialized {@link module:cloud-services/token/token~Token} instance.
|
|
136
|
+
*
|
|
137
|
+
* @param tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
|
|
138
|
+
* value is a function it has to match the {@link module:cloud-services/token/token~Token#refreshToken} interface.
|
|
139
|
+
*/
|
|
140
|
+
static create(tokenUrlOrRefreshToken, options = {}) {
|
|
141
|
+
const token = new Token(tokenUrlOrRefreshToken, options);
|
|
142
|
+
return token.init();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* This function is called in a defined interval by the {@link ~Token} class. It also can be invoked manually.
|
|
147
|
+
* It should return a promise, which resolves with the new token value.
|
|
148
|
+
* If any error occurs it should return a rejected promise with an error message.
|
|
149
|
+
*/
|
|
150
|
+
function defaultRefreshToken(tokenUrl) {
|
|
151
|
+
return new Promise((resolve, reject) => {
|
|
152
|
+
const xhr = new XMLHttpRequest();
|
|
153
|
+
xhr.open('GET', tokenUrl);
|
|
154
|
+
xhr.addEventListener('load', () => {
|
|
155
|
+
const statusCode = xhr.status;
|
|
156
|
+
const xhrResponse = xhr.response;
|
|
157
|
+
if (statusCode < 200 || statusCode > 299) {
|
|
158
|
+
/**
|
|
159
|
+
* Cannot download new token from the provided url.
|
|
160
|
+
*
|
|
161
|
+
* @error token-cannot-download-new-token
|
|
162
|
+
*/
|
|
163
|
+
return reject(new CKEditorError('token-cannot-download-new-token', null));
|
|
164
|
+
}
|
|
165
|
+
return resolve(xhrResponse);
|
|
166
|
+
});
|
|
167
|
+
xhr.addEventListener('error', () => reject(new Error('Network Error')));
|
|
168
|
+
xhr.addEventListener('abort', () => reject(new Error('Abort')));
|
|
169
|
+
xhr.send();
|
|
170
|
+
});
|
|
171
|
+
}
|
|
@@ -1,94 +1,94 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @module cloud-services/uploadgateway/fileuploader
|
|
7
|
-
*/
|
|
8
|
-
import type { UploadResponse } from 'ckeditor5/src/upload';
|
|
9
|
-
import type { InitializedToken } from '../token/token';
|
|
10
|
-
declare const FileUploader_base: {
|
|
11
|
-
new (): import("ckeditor5/src/utils").Emitter;
|
|
12
|
-
prototype: import("ckeditor5/src/utils").Emitter;
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* FileUploader class used to upload single file.
|
|
16
|
-
*/
|
|
17
|
-
export default class FileUploader extends FileUploader_base {
|
|
18
|
-
/**
|
|
19
|
-
* A file that is being uploaded.
|
|
20
|
-
*/
|
|
21
|
-
readonly file: Blob;
|
|
22
|
-
xhr?: XMLHttpRequest;
|
|
23
|
-
/**
|
|
24
|
-
* CKEditor Cloud Services access token.
|
|
25
|
-
*/
|
|
26
|
-
private readonly _token;
|
|
27
|
-
/**
|
|
28
|
-
* CKEditor Cloud Services API address.
|
|
29
|
-
*/
|
|
30
|
-
private readonly _apiAddress;
|
|
31
|
-
/**
|
|
32
|
-
* Creates `FileUploader` instance.
|
|
33
|
-
*
|
|
34
|
-
* @param fileOrData A blob object or a data string encoded with Base64.
|
|
35
|
-
* @param token Token used for authentication.
|
|
36
|
-
* @param apiAddress API address.
|
|
37
|
-
*/
|
|
38
|
-
constructor(fileOrData: string | Blob, token: InitializedToken, apiAddress: string);
|
|
39
|
-
/**
|
|
40
|
-
* Registers callback on `progress` event.
|
|
41
|
-
*/
|
|
42
|
-
onProgress(callback: (status: {
|
|
43
|
-
total: number;
|
|
44
|
-
uploaded: number;
|
|
45
|
-
}) => void): this;
|
|
46
|
-
/**
|
|
47
|
-
* Registers callback on `error` event. Event is called once when error occurs.
|
|
48
|
-
*/
|
|
49
|
-
onError(callback: (error: string) => void): this;
|
|
50
|
-
/**
|
|
51
|
-
* Aborts upload process.
|
|
52
|
-
*/
|
|
53
|
-
abort(): void;
|
|
54
|
-
/**
|
|
55
|
-
* Sends XHR request to API.
|
|
56
|
-
*/
|
|
57
|
-
send(): Promise<UploadResponse>;
|
|
58
|
-
/**
|
|
59
|
-
* Prepares XHR request.
|
|
60
|
-
*/
|
|
61
|
-
private _prepareRequest;
|
|
62
|
-
/**
|
|
63
|
-
* Attaches listeners to the XHR.
|
|
64
|
-
*/
|
|
65
|
-
private _attachXHRListeners;
|
|
66
|
-
/**
|
|
67
|
-
* Sends XHR request.
|
|
68
|
-
*/
|
|
69
|
-
private _sendRequest;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Fired when error occurs.
|
|
73
|
-
*
|
|
74
|
-
* @eventName ~FileUploader#error
|
|
75
|
-
* @param error Error message
|
|
76
|
-
*/
|
|
77
|
-
export type FileUploaderErrorEvent = {
|
|
78
|
-
name: 'error';
|
|
79
|
-
args: [error: string];
|
|
80
|
-
};
|
|
81
|
-
/**
|
|
82
|
-
* Fired on upload progress.
|
|
83
|
-
*
|
|
84
|
-
* @eventName ~FileUploader#progress
|
|
85
|
-
* @param status Total and uploaded status
|
|
86
|
-
*/
|
|
87
|
-
export type FileUploaderProgressErrorEvent = {
|
|
88
|
-
name: 'progress';
|
|
89
|
-
args: [status: {
|
|
90
|
-
total: number;
|
|
91
|
-
uploaded: number;
|
|
92
|
-
}];
|
|
93
|
-
};
|
|
94
|
-
export {};
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module cloud-services/uploadgateway/fileuploader
|
|
7
|
+
*/
|
|
8
|
+
import type { UploadResponse } from 'ckeditor5/src/upload';
|
|
9
|
+
import type { InitializedToken } from '../token/token';
|
|
10
|
+
declare const FileUploader_base: {
|
|
11
|
+
new (): import("ckeditor5/src/utils").Emitter;
|
|
12
|
+
prototype: import("ckeditor5/src/utils").Emitter;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* FileUploader class used to upload single file.
|
|
16
|
+
*/
|
|
17
|
+
export default class FileUploader extends FileUploader_base {
|
|
18
|
+
/**
|
|
19
|
+
* A file that is being uploaded.
|
|
20
|
+
*/
|
|
21
|
+
readonly file: Blob;
|
|
22
|
+
xhr?: XMLHttpRequest;
|
|
23
|
+
/**
|
|
24
|
+
* CKEditor Cloud Services access token.
|
|
25
|
+
*/
|
|
26
|
+
private readonly _token;
|
|
27
|
+
/**
|
|
28
|
+
* CKEditor Cloud Services API address.
|
|
29
|
+
*/
|
|
30
|
+
private readonly _apiAddress;
|
|
31
|
+
/**
|
|
32
|
+
* Creates `FileUploader` instance.
|
|
33
|
+
*
|
|
34
|
+
* @param fileOrData A blob object or a data string encoded with Base64.
|
|
35
|
+
* @param token Token used for authentication.
|
|
36
|
+
* @param apiAddress API address.
|
|
37
|
+
*/
|
|
38
|
+
constructor(fileOrData: string | Blob, token: InitializedToken, apiAddress: string);
|
|
39
|
+
/**
|
|
40
|
+
* Registers callback on `progress` event.
|
|
41
|
+
*/
|
|
42
|
+
onProgress(callback: (status: {
|
|
43
|
+
total: number;
|
|
44
|
+
uploaded: number;
|
|
45
|
+
}) => void): this;
|
|
46
|
+
/**
|
|
47
|
+
* Registers callback on `error` event. Event is called once when error occurs.
|
|
48
|
+
*/
|
|
49
|
+
onError(callback: (error: string) => void): this;
|
|
50
|
+
/**
|
|
51
|
+
* Aborts upload process.
|
|
52
|
+
*/
|
|
53
|
+
abort(): void;
|
|
54
|
+
/**
|
|
55
|
+
* Sends XHR request to API.
|
|
56
|
+
*/
|
|
57
|
+
send(): Promise<UploadResponse>;
|
|
58
|
+
/**
|
|
59
|
+
* Prepares XHR request.
|
|
60
|
+
*/
|
|
61
|
+
private _prepareRequest;
|
|
62
|
+
/**
|
|
63
|
+
* Attaches listeners to the XHR.
|
|
64
|
+
*/
|
|
65
|
+
private _attachXHRListeners;
|
|
66
|
+
/**
|
|
67
|
+
* Sends XHR request.
|
|
68
|
+
*/
|
|
69
|
+
private _sendRequest;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Fired when error occurs.
|
|
73
|
+
*
|
|
74
|
+
* @eventName ~FileUploader#error
|
|
75
|
+
* @param error Error message
|
|
76
|
+
*/
|
|
77
|
+
export type FileUploaderErrorEvent = {
|
|
78
|
+
name: 'error';
|
|
79
|
+
args: [error: string];
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Fired on upload progress.
|
|
83
|
+
*
|
|
84
|
+
* @eventName ~FileUploader#progress
|
|
85
|
+
* @param status Total and uploaded status
|
|
86
|
+
*/
|
|
87
|
+
export type FileUploaderProgressErrorEvent = {
|
|
88
|
+
name: 'progress';
|
|
89
|
+
args: [status: {
|
|
90
|
+
total: number;
|
|
91
|
+
uploaded: number;
|
|
92
|
+
}];
|
|
93
|
+
};
|
|
94
|
+
export {};
|