@ckeditor/ckeditor5-cloud-services 0.0.0-internal-20241017.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 +4 -0
- package/LICENSE.md +17 -0
- package/README.md +22 -0
- package/build/cloud-services.js +4 -0
- package/ckeditor5-metadata.json +11 -0
- package/dist/augmentation.d.ts +23 -0
- package/dist/cloudservices.d.ts +92 -0
- package/dist/cloudservicesconfig.d.ts +128 -0
- package/dist/cloudservicescore.d.ts +44 -0
- package/dist/index-content.css +4 -0
- package/dist/index-editor.css +4 -0
- package/dist/index.css +4 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +573 -0
- package/dist/index.js.map +1 -0
- package/dist/token/token.d.ts +113 -0
- package/dist/uploadgateway/fileuploader.d.ts +98 -0
- package/dist/uploadgateway/uploadgateway.d.ts +51 -0
- package/package.json +39 -0
- package/src/augmentation.d.ts +19 -0
- package/src/augmentation.js +5 -0
- package/src/cloudservices.d.ts +88 -0
- package/src/cloudservices.js +109 -0
- package/src/cloudservicesconfig.d.ts +124 -0
- package/src/cloudservicesconfig.js +5 -0
- package/src/cloudservicescore.d.ts +40 -0
- package/src/cloudservicescore.js +47 -0
- package/src/index.d.ts +14 -0
- package/src/index.js +10 -0
- package/src/token/token.d.ts +109 -0
- package/src/token/token.js +206 -0
- package/src/uploadgateway/fileuploader.d.ts +94 -0
- package/src/uploadgateway/fileuploader.js +183 -0
- package/src/uploadgateway/uploadgateway.d.ts +47 -0
- package/src/uploadgateway/uploadgateway.js +60 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, 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, logWarning } from 'ckeditor5/src/utils.js';
|
|
10
|
+
const DEFAULT_OPTIONS = { autoRefresh: true };
|
|
11
|
+
const DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME = 3600000; // 1 hour
|
|
12
|
+
const TOKEN_FAILED_REFRESH_TIMEOUT_TIME = 5000; // 5 seconds
|
|
13
|
+
/**
|
|
14
|
+
* The class representing the token used for communication with CKEditor Cloud Services.
|
|
15
|
+
* The value of the token is retrieved from the specified URL and refreshed every 1 hour by default.
|
|
16
|
+
* If the token retrieval fails, the token will automatically retry in 5 seconds intervals.
|
|
17
|
+
*/
|
|
18
|
+
export default class Token extends /* #__PURE__ */ ObservableMixin() {
|
|
19
|
+
/**
|
|
20
|
+
* Creates `Token` instance.
|
|
21
|
+
* Method `init` should be called after using the constructor or use `create` method instead.
|
|
22
|
+
*
|
|
23
|
+
* @param tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
|
|
24
|
+
* value is a function it has to match the {@link module:cloud-services/token/token~Token#refreshToken} interface.
|
|
25
|
+
*/
|
|
26
|
+
constructor(tokenUrlOrRefreshToken, options = {}) {
|
|
27
|
+
super();
|
|
28
|
+
if (!tokenUrlOrRefreshToken) {
|
|
29
|
+
/**
|
|
30
|
+
* A `tokenUrl` must be provided as the first constructor argument.
|
|
31
|
+
*
|
|
32
|
+
* @error token-missing-token-url
|
|
33
|
+
*/
|
|
34
|
+
throw new CKEditorError('token-missing-token-url', this);
|
|
35
|
+
}
|
|
36
|
+
if (options.initValue) {
|
|
37
|
+
this._validateTokenValue(options.initValue);
|
|
38
|
+
}
|
|
39
|
+
this.set('value', options.initValue);
|
|
40
|
+
if (typeof tokenUrlOrRefreshToken === 'function') {
|
|
41
|
+
this._refresh = tokenUrlOrRefreshToken;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
this._refresh = () => defaultRefreshToken(tokenUrlOrRefreshToken);
|
|
45
|
+
}
|
|
46
|
+
this._options = { ...DEFAULT_OPTIONS, ...options };
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Initializes the token.
|
|
50
|
+
*/
|
|
51
|
+
init() {
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
if (!this.value) {
|
|
54
|
+
this.refreshToken()
|
|
55
|
+
.then(resolve)
|
|
56
|
+
.catch(reject);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (this._options.autoRefresh) {
|
|
60
|
+
this._registerRefreshTokenTimeout();
|
|
61
|
+
}
|
|
62
|
+
resolve(this);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Refresh token method. Useful in a method form as it can be overridden in tests.
|
|
67
|
+
*
|
|
68
|
+
* This method will be invoked periodically based on the token expiry date after first call to keep the token up-to-date
|
|
69
|
+
* (requires {@link module:cloud-services/token/token~TokenOptions auto refresh option} to be set).
|
|
70
|
+
*
|
|
71
|
+
* If the token refresh fails, the method will retry in 5 seconds intervals until success or the token gets
|
|
72
|
+
* {@link #destroy destroyed}.
|
|
73
|
+
*/
|
|
74
|
+
refreshToken() {
|
|
75
|
+
const autoRefresh = this._options.autoRefresh;
|
|
76
|
+
return this._refresh()
|
|
77
|
+
.then(value => {
|
|
78
|
+
this._validateTokenValue(value);
|
|
79
|
+
this.set('value', value);
|
|
80
|
+
if (autoRefresh) {
|
|
81
|
+
this._registerRefreshTokenTimeout();
|
|
82
|
+
}
|
|
83
|
+
return this;
|
|
84
|
+
})
|
|
85
|
+
.catch(err => {
|
|
86
|
+
/**
|
|
87
|
+
* You will see this warning when the CKEditor {@link module:cloud-services/token/token~Token token} could not be refreshed.
|
|
88
|
+
* This may be a result of a network error, a token endpoint (server) error, or an invalid
|
|
89
|
+
* {@link module:cloud-services/cloudservicesconfig~CloudServicesConfig#tokenUrl token URL configuration}.
|
|
90
|
+
*
|
|
91
|
+
* If this warning repeats, please make sure that the configuration is correct and that the token
|
|
92
|
+
* endpoint is up and running. {@link module:cloud-services/cloudservicesconfig~CloudServicesConfig#tokenUrl Learn more}
|
|
93
|
+
* about token configuration.
|
|
94
|
+
*
|
|
95
|
+
* **Note:** If the token's {@link module:cloud-services/token/token~TokenOptions auto refresh option} is enabled,
|
|
96
|
+
* attempts to refresh will be made until success or token's
|
|
97
|
+
* {@link module:cloud-services/token/token~Token#destroy destruction}.
|
|
98
|
+
*
|
|
99
|
+
* @error token-refresh-failed
|
|
100
|
+
* @param autoRefresh Whether the token will keep auto refreshing.
|
|
101
|
+
*/
|
|
102
|
+
logWarning('token-refresh-failed', { autoRefresh });
|
|
103
|
+
// If the refresh failed, keep trying to refresh the token. Failing to do so will eventually
|
|
104
|
+
// lead to the disconnection from the RTC service and the editing session (and potential data loss
|
|
105
|
+
// if the user keeps editing).
|
|
106
|
+
if (autoRefresh) {
|
|
107
|
+
this._registerRefreshTokenTimeout(TOKEN_FAILED_REFRESH_TIMEOUT_TIME);
|
|
108
|
+
}
|
|
109
|
+
throw err;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Destroys token instance. Stops refreshing.
|
|
114
|
+
*/
|
|
115
|
+
destroy() {
|
|
116
|
+
clearTimeout(this._tokenRefreshTimeout);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Checks whether the provided token follows the JSON Web Tokens (JWT) format.
|
|
120
|
+
*
|
|
121
|
+
* @param tokenValue The token to validate.
|
|
122
|
+
*/
|
|
123
|
+
_validateTokenValue(tokenValue) {
|
|
124
|
+
// The token must be a string.
|
|
125
|
+
const isString = typeof tokenValue === 'string';
|
|
126
|
+
// The token must be a plain string without quotes ("").
|
|
127
|
+
const isPlainString = !/^".*"$/.test(tokenValue);
|
|
128
|
+
// JWT token contains 3 parts: header, payload, and signature.
|
|
129
|
+
// Each part is separated by a dot.
|
|
130
|
+
const isJWTFormat = isString && tokenValue.split('.').length === 3;
|
|
131
|
+
if (!(isPlainString && isJWTFormat)) {
|
|
132
|
+
/**
|
|
133
|
+
* The provided token must follow the [JSON Web Tokens](https://jwt.io/introduction/) format.
|
|
134
|
+
*
|
|
135
|
+
* @error token-not-in-jwt-format
|
|
136
|
+
*/
|
|
137
|
+
throw new CKEditorError('token-not-in-jwt-format', this);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Registers a refresh token timeout for the time taken from token.
|
|
142
|
+
*/
|
|
143
|
+
_registerRefreshTokenTimeout(timeoutTime) {
|
|
144
|
+
const tokenRefreshTimeoutTime = timeoutTime || this._getTokenRefreshTimeoutTime();
|
|
145
|
+
clearTimeout(this._tokenRefreshTimeout);
|
|
146
|
+
this._tokenRefreshTimeout = setTimeout(() => {
|
|
147
|
+
this.refreshToken();
|
|
148
|
+
}, tokenRefreshTimeoutTime);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Returns token refresh timeout time calculated from expire time in the token payload.
|
|
152
|
+
*
|
|
153
|
+
* If the token parse fails or the token payload doesn't contain, the default DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME is returned.
|
|
154
|
+
*/
|
|
155
|
+
_getTokenRefreshTimeoutTime() {
|
|
156
|
+
try {
|
|
157
|
+
const [, binaryTokenPayload] = this.value.split('.');
|
|
158
|
+
const { exp: tokenExpireTime } = JSON.parse(atob(binaryTokenPayload));
|
|
159
|
+
if (!tokenExpireTime) {
|
|
160
|
+
return DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME;
|
|
161
|
+
}
|
|
162
|
+
const tokenRefreshTimeoutTime = Math.floor(((tokenExpireTime * 1000) - Date.now()) / 2);
|
|
163
|
+
return tokenRefreshTimeoutTime;
|
|
164
|
+
}
|
|
165
|
+
catch (err) {
|
|
166
|
+
return DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Creates a initialized {@link module:cloud-services/token/token~Token} instance.
|
|
171
|
+
*
|
|
172
|
+
* @param tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
|
|
173
|
+
* value is a function it has to match the {@link module:cloud-services/token/token~Token#refreshToken} interface.
|
|
174
|
+
*/
|
|
175
|
+
static create(tokenUrlOrRefreshToken, options = {}) {
|
|
176
|
+
const token = new Token(tokenUrlOrRefreshToken, options);
|
|
177
|
+
return token.init();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* This function is called in a defined interval by the {@link ~Token} class. It also can be invoked manually.
|
|
182
|
+
* It should return a promise, which resolves with the new token value.
|
|
183
|
+
* If any error occurs it should return a rejected promise with an error message.
|
|
184
|
+
*/
|
|
185
|
+
function defaultRefreshToken(tokenUrl) {
|
|
186
|
+
return new Promise((resolve, reject) => {
|
|
187
|
+
const xhr = new XMLHttpRequest();
|
|
188
|
+
xhr.open('GET', tokenUrl);
|
|
189
|
+
xhr.addEventListener('load', () => {
|
|
190
|
+
const statusCode = xhr.status;
|
|
191
|
+
const xhrResponse = xhr.response;
|
|
192
|
+
if (statusCode < 200 || statusCode > 299) {
|
|
193
|
+
/**
|
|
194
|
+
* Cannot download new token from the provided url.
|
|
195
|
+
*
|
|
196
|
+
* @error token-cannot-download-new-token
|
|
197
|
+
*/
|
|
198
|
+
return reject(new CKEditorError('token-cannot-download-new-token', null));
|
|
199
|
+
}
|
|
200
|
+
return resolve(xhrResponse);
|
|
201
|
+
});
|
|
202
|
+
xhr.addEventListener('error', () => reject(new Error('Network Error')));
|
|
203
|
+
xhr.addEventListener('abort', () => reject(new Error('Abort')));
|
|
204
|
+
xhr.send();
|
|
205
|
+
});
|
|
206
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, 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.js';
|
|
9
|
+
import type { InitializedToken } from '../token/token.js';
|
|
10
|
+
declare const FileUploader_base: {
|
|
11
|
+
new (): import("ckeditor5/src/utils.js").Emitter;
|
|
12
|
+
prototype: import("ckeditor5/src/utils.js").Emitter;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* FileUploader class used to upload single file.
|
|
16
|
+
*/
|
|
17
|
+
export default class FileUploader extends /* #__PURE__ */ 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 {};
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, 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
|
+
import { EmitterMixin, CKEditorError } from 'ckeditor5/src/utils.js';
|
|
6
|
+
const BASE64_HEADER_REG_EXP = /^data:(\S*?);base64,/;
|
|
7
|
+
/**
|
|
8
|
+
* FileUploader class used to upload single file.
|
|
9
|
+
*/
|
|
10
|
+
export default class FileUploader extends /* #__PURE__ */ EmitterMixin() {
|
|
11
|
+
/**
|
|
12
|
+
* Creates `FileUploader` instance.
|
|
13
|
+
*
|
|
14
|
+
* @param fileOrData A blob object or a data string encoded with Base64.
|
|
15
|
+
* @param token Token used for authentication.
|
|
16
|
+
* @param apiAddress API address.
|
|
17
|
+
*/
|
|
18
|
+
constructor(fileOrData, token, apiAddress) {
|
|
19
|
+
super();
|
|
20
|
+
if (!fileOrData) {
|
|
21
|
+
/**
|
|
22
|
+
* File must be provided as the first argument.
|
|
23
|
+
*
|
|
24
|
+
* @error fileuploader-missing-file
|
|
25
|
+
*/
|
|
26
|
+
throw new CKEditorError('fileuploader-missing-file', null);
|
|
27
|
+
}
|
|
28
|
+
if (!token) {
|
|
29
|
+
/**
|
|
30
|
+
* Token must be provided as the second argument.
|
|
31
|
+
*
|
|
32
|
+
* @error fileuploader-missing-token
|
|
33
|
+
*/
|
|
34
|
+
throw new CKEditorError('fileuploader-missing-token', null);
|
|
35
|
+
}
|
|
36
|
+
if (!apiAddress) {
|
|
37
|
+
/**
|
|
38
|
+
* Api address must be provided as the third argument.
|
|
39
|
+
*
|
|
40
|
+
* @error fileuploader-missing-api-address
|
|
41
|
+
*/
|
|
42
|
+
throw new CKEditorError('fileuploader-missing-api-address', null);
|
|
43
|
+
}
|
|
44
|
+
this.file = _isBase64(fileOrData) ? _base64ToBlob(fileOrData) : fileOrData;
|
|
45
|
+
this._token = token;
|
|
46
|
+
this._apiAddress = apiAddress;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Registers callback on `progress` event.
|
|
50
|
+
*/
|
|
51
|
+
onProgress(callback) {
|
|
52
|
+
this.on('progress', (event, data) => callback(data));
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Registers callback on `error` event. Event is called once when error occurs.
|
|
57
|
+
*/
|
|
58
|
+
onError(callback) {
|
|
59
|
+
this.once('error', (event, data) => callback(data));
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Aborts upload process.
|
|
64
|
+
*/
|
|
65
|
+
abort() {
|
|
66
|
+
this.xhr.abort();
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Sends XHR request to API.
|
|
70
|
+
*/
|
|
71
|
+
send() {
|
|
72
|
+
this._prepareRequest();
|
|
73
|
+
this._attachXHRListeners();
|
|
74
|
+
return this._sendRequest();
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Prepares XHR request.
|
|
78
|
+
*/
|
|
79
|
+
_prepareRequest() {
|
|
80
|
+
const xhr = new XMLHttpRequest();
|
|
81
|
+
xhr.open('POST', this._apiAddress);
|
|
82
|
+
xhr.setRequestHeader('Authorization', this._token.value);
|
|
83
|
+
xhr.responseType = 'json';
|
|
84
|
+
this.xhr = xhr;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Attaches listeners to the XHR.
|
|
88
|
+
*/
|
|
89
|
+
_attachXHRListeners() {
|
|
90
|
+
const xhr = this.xhr;
|
|
91
|
+
const onError = (message) => {
|
|
92
|
+
return () => this.fire('error', message);
|
|
93
|
+
};
|
|
94
|
+
xhr.addEventListener('error', onError('Network Error'));
|
|
95
|
+
xhr.addEventListener('abort', onError('Abort'));
|
|
96
|
+
/* istanbul ignore else -- @preserve */
|
|
97
|
+
if (xhr.upload) {
|
|
98
|
+
xhr.upload.addEventListener('progress', event => {
|
|
99
|
+
if (event.lengthComputable) {
|
|
100
|
+
this.fire('progress', {
|
|
101
|
+
total: event.total,
|
|
102
|
+
uploaded: event.loaded
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
xhr.addEventListener('load', () => {
|
|
108
|
+
const statusCode = xhr.status;
|
|
109
|
+
const xhrResponse = xhr.response;
|
|
110
|
+
if (statusCode < 200 || statusCode > 299) {
|
|
111
|
+
return this.fire('error', xhrResponse.message || xhrResponse.error);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Sends XHR request.
|
|
117
|
+
*/
|
|
118
|
+
_sendRequest() {
|
|
119
|
+
const formData = new FormData();
|
|
120
|
+
const xhr = this.xhr;
|
|
121
|
+
formData.append('file', this.file);
|
|
122
|
+
return new Promise((resolve, reject) => {
|
|
123
|
+
xhr.addEventListener('load', () => {
|
|
124
|
+
const statusCode = xhr.status;
|
|
125
|
+
const xhrResponse = xhr.response;
|
|
126
|
+
if (statusCode < 200 || statusCode > 299) {
|
|
127
|
+
if (xhrResponse.message) {
|
|
128
|
+
/**
|
|
129
|
+
* Uploading file failed.
|
|
130
|
+
*
|
|
131
|
+
* @error fileuploader-uploading-data-failed
|
|
132
|
+
*/
|
|
133
|
+
return reject(new CKEditorError('fileuploader-uploading-data-failed', this, { message: xhrResponse.message }));
|
|
134
|
+
}
|
|
135
|
+
return reject(xhrResponse.error);
|
|
136
|
+
}
|
|
137
|
+
return resolve(xhrResponse);
|
|
138
|
+
});
|
|
139
|
+
xhr.addEventListener('error', () => reject(new Error('Network Error')));
|
|
140
|
+
xhr.addEventListener('abort', () => reject(new Error('Abort')));
|
|
141
|
+
xhr.send(formData);
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Transforms Base64 string data into file.
|
|
147
|
+
*
|
|
148
|
+
* @param base64 String data.
|
|
149
|
+
*/
|
|
150
|
+
function _base64ToBlob(base64, sliceSize = 512) {
|
|
151
|
+
try {
|
|
152
|
+
const contentType = base64.match(BASE64_HEADER_REG_EXP)[1];
|
|
153
|
+
const base64Data = atob(base64.replace(BASE64_HEADER_REG_EXP, ''));
|
|
154
|
+
const byteArrays = [];
|
|
155
|
+
for (let offset = 0; offset < base64Data.length; offset += sliceSize) {
|
|
156
|
+
const slice = base64Data.slice(offset, offset + sliceSize);
|
|
157
|
+
const byteNumbers = new Array(slice.length);
|
|
158
|
+
for (let i = 0; i < slice.length; i++) {
|
|
159
|
+
byteNumbers[i] = slice.charCodeAt(i);
|
|
160
|
+
}
|
|
161
|
+
byteArrays.push(new Uint8Array(byteNumbers));
|
|
162
|
+
}
|
|
163
|
+
return new Blob(byteArrays, { type: contentType });
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
/**
|
|
167
|
+
* Problem with decoding Base64 image data.
|
|
168
|
+
*
|
|
169
|
+
* @error fileuploader-decoding-image-data-error
|
|
170
|
+
*/
|
|
171
|
+
throw new CKEditorError('fileuploader-decoding-image-data-error', null);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Checks that string is Base64.
|
|
176
|
+
*/
|
|
177
|
+
function _isBase64(string) {
|
|
178
|
+
if (typeof string !== 'string') {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
const match = string.match(BASE64_HEADER_REG_EXP);
|
|
182
|
+
return !!(match && match.length);
|
|
183
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, 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/uploadgateway
|
|
7
|
+
*/
|
|
8
|
+
import FileUploader from './fileuploader.js';
|
|
9
|
+
import type { InitializedToken } from '../token/token.js';
|
|
10
|
+
/**
|
|
11
|
+
* UploadGateway abstracts file uploads to CKEditor Cloud Services.
|
|
12
|
+
*/
|
|
13
|
+
export default class UploadGateway {
|
|
14
|
+
/**
|
|
15
|
+
* CKEditor Cloud Services access token.
|
|
16
|
+
*/
|
|
17
|
+
private readonly _token;
|
|
18
|
+
/**
|
|
19
|
+
* CKEditor Cloud Services API address.
|
|
20
|
+
*/
|
|
21
|
+
private readonly _apiAddress;
|
|
22
|
+
/**
|
|
23
|
+
* Creates `UploadGateway` instance.
|
|
24
|
+
*
|
|
25
|
+
* @param token Token used for authentication.
|
|
26
|
+
* @param apiAddress API address.
|
|
27
|
+
*/
|
|
28
|
+
constructor(token: InitializedToken, apiAddress: string);
|
|
29
|
+
/**
|
|
30
|
+
* Creates a {@link module:cloud-services/uploadgateway/fileuploader~FileUploader} instance that wraps
|
|
31
|
+
* file upload process. The file is being sent at a time when the
|
|
32
|
+
* {@link module:cloud-services/uploadgateway/fileuploader~FileUploader#send} method is called.
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* const token = await Token.create( 'https://token-endpoint' );
|
|
36
|
+
* new UploadGateway( token, 'https://example.org' )
|
|
37
|
+
* .upload( 'FILE' )
|
|
38
|
+
* .onProgress( ( data ) => console.log( data ) )
|
|
39
|
+
* .send()
|
|
40
|
+
* .then( ( response ) => console.log( response ) );
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @param {Blob|String} fileOrData A blob object or a data string encoded with Base64.
|
|
44
|
+
* @returns {module:cloud-services/uploadgateway/fileuploader~FileUploader} Returns `FileUploader` instance.
|
|
45
|
+
*/
|
|
46
|
+
upload(fileOrData: string | Blob): FileUploader;
|
|
47
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, 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/uploadgateway
|
|
7
|
+
*/
|
|
8
|
+
import FileUploader from './fileuploader.js';
|
|
9
|
+
import { CKEditorError } from 'ckeditor5/src/utils.js';
|
|
10
|
+
/**
|
|
11
|
+
* UploadGateway abstracts file uploads to CKEditor Cloud Services.
|
|
12
|
+
*/
|
|
13
|
+
export default class UploadGateway {
|
|
14
|
+
/**
|
|
15
|
+
* Creates `UploadGateway` instance.
|
|
16
|
+
*
|
|
17
|
+
* @param token Token used for authentication.
|
|
18
|
+
* @param apiAddress API address.
|
|
19
|
+
*/
|
|
20
|
+
constructor(token, apiAddress) {
|
|
21
|
+
if (!token) {
|
|
22
|
+
/**
|
|
23
|
+
* Token must be provided.
|
|
24
|
+
*
|
|
25
|
+
* @error uploadgateway-missing-token
|
|
26
|
+
*/
|
|
27
|
+
throw new CKEditorError('uploadgateway-missing-token', null);
|
|
28
|
+
}
|
|
29
|
+
if (!apiAddress) {
|
|
30
|
+
/**
|
|
31
|
+
* Api address must be provided.
|
|
32
|
+
*
|
|
33
|
+
* @error uploadgateway-missing-api-address
|
|
34
|
+
*/
|
|
35
|
+
throw new CKEditorError('uploadgateway-missing-api-address', null);
|
|
36
|
+
}
|
|
37
|
+
this._token = token;
|
|
38
|
+
this._apiAddress = apiAddress;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Creates a {@link module:cloud-services/uploadgateway/fileuploader~FileUploader} instance that wraps
|
|
42
|
+
* file upload process. The file is being sent at a time when the
|
|
43
|
+
* {@link module:cloud-services/uploadgateway/fileuploader~FileUploader#send} method is called.
|
|
44
|
+
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* const token = await Token.create( 'https://token-endpoint' );
|
|
47
|
+
* new UploadGateway( token, 'https://example.org' )
|
|
48
|
+
* .upload( 'FILE' )
|
|
49
|
+
* .onProgress( ( data ) => console.log( data ) )
|
|
50
|
+
* .send()
|
|
51
|
+
* .then( ( response ) => console.log( response ) );
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @param {Blob|String} fileOrData A blob object or a data string encoded with Base64.
|
|
55
|
+
* @returns {module:cloud-services/uploadgateway/fileuploader~FileUploader} Returns `FileUploader` instance.
|
|
56
|
+
*/
|
|
57
|
+
upload(fileOrData) {
|
|
58
|
+
return new FileUploader(fileOrData, this._token, this._apiAddress);
|
|
59
|
+
}
|
|
60
|
+
}
|