@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.
@@ -1,183 +1,183 @@
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
- import { EmitterMixin, CKEditorError } from 'ckeditor5/src/utils';
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 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
- }
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
+ import { EmitterMixin, CKEditorError } from 'ckeditor5/src/utils';
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 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
+ }
@@ -1,47 +1,47 @@
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/uploadgateway
7
- */
8
- import FileUploader from './fileuploader';
9
- import type { InitializedToken } from '../token/token';
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
- }
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/uploadgateway
7
+ */
8
+ import FileUploader from './fileuploader';
9
+ import type { InitializedToken } from '../token/token';
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
+ }
@@ -1,60 +1,60 @@
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/uploadgateway
7
- */
8
- import FileUploader from './fileuploader';
9
- import { CKEditorError } from 'ckeditor5/src/utils';
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
- }
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/uploadgateway
7
+ */
8
+ import FileUploader from './fileuploader';
9
+ import { CKEditorError } from 'ckeditor5/src/utils';
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
+ }