@ckeditor/ckeditor5-cloud-services 47.6.1 → 48.0.0-alpha.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,195 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
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 class FileUploader extends /* #__PURE__ */ EmitterMixin() {
11
- /**
12
- * A file that is being uploaded.
13
- */
14
- file;
15
- xhr;
16
- /**
17
- * CKEditor Cloud Services access token.
18
- */
19
- _token;
20
- /**
21
- * CKEditor Cloud Services API address.
22
- */
23
- _apiAddress;
24
- /**
25
- * Creates `FileUploader` instance.
26
- *
27
- * @param fileOrData A blob object or a data string encoded with Base64.
28
- * @param token Token used for authentication.
29
- * @param apiAddress API address.
30
- */
31
- constructor(fileOrData, token, apiAddress) {
32
- super();
33
- if (!fileOrData) {
34
- /**
35
- * File must be provided as the first argument.
36
- *
37
- * @error fileuploader-missing-file
38
- */
39
- throw new CKEditorError('fileuploader-missing-file', null);
40
- }
41
- if (!token) {
42
- /**
43
- * Token must be provided as the second argument.
44
- *
45
- * @error fileuploader-missing-token
46
- */
47
- throw new CKEditorError('fileuploader-missing-token', null);
48
- }
49
- if (!apiAddress) {
50
- /**
51
- * Api address must be provided as the third argument.
52
- *
53
- * @error fileuploader-missing-api-address
54
- */
55
- throw new CKEditorError('fileuploader-missing-api-address', null);
56
- }
57
- this.file = _isBase64(fileOrData) ? _base64ToBlob(fileOrData) : fileOrData;
58
- this._token = token;
59
- this._apiAddress = apiAddress;
60
- }
61
- /**
62
- * Registers callback on `progress` event.
63
- */
64
- onProgress(callback) {
65
- this.on('progress', (event, data) => callback(data));
66
- return this;
67
- }
68
- /**
69
- * Registers callback on `error` event. Event is called once when error occurs.
70
- */
71
- onError(callback) {
72
- this.once('error', (event, data) => callback(data));
73
- return this;
74
- }
75
- /**
76
- * Aborts upload process.
77
- */
78
- abort() {
79
- this.xhr.abort();
80
- }
81
- /**
82
- * Sends XHR request to API.
83
- */
84
- send() {
85
- this._prepareRequest();
86
- this._attachXHRListeners();
87
- return this._sendRequest();
88
- }
89
- /**
90
- * Prepares XHR request.
91
- */
92
- _prepareRequest() {
93
- const xhr = new XMLHttpRequest();
94
- xhr.open('POST', this._apiAddress);
95
- xhr.setRequestHeader('Authorization', this._token.value);
96
- xhr.responseType = 'json';
97
- this.xhr = xhr;
98
- }
99
- /**
100
- * Attaches listeners to the XHR.
101
- */
102
- _attachXHRListeners() {
103
- const xhr = this.xhr;
104
- const onError = (message) => {
105
- return () => this.fire('error', message);
106
- };
107
- xhr.addEventListener('error', onError('Network Error'));
108
- xhr.addEventListener('abort', onError('Abort'));
109
- /* istanbul ignore else -- @preserve */
110
- if (xhr.upload) {
111
- xhr.upload.addEventListener('progress', event => {
112
- if (event.lengthComputable) {
113
- this.fire('progress', {
114
- total: event.total,
115
- uploaded: event.loaded
116
- });
117
- }
118
- });
119
- }
120
- xhr.addEventListener('load', () => {
121
- const statusCode = xhr.status;
122
- const xhrResponse = xhr.response;
123
- if (statusCode < 200 || statusCode > 299) {
124
- return this.fire('error', xhrResponse.message || xhrResponse.error);
125
- }
126
- });
127
- }
128
- /**
129
- * Sends XHR request.
130
- */
131
- _sendRequest() {
132
- const formData = new FormData();
133
- const xhr = this.xhr;
134
- formData.append('file', this.file);
135
- return new Promise((resolve, reject) => {
136
- xhr.addEventListener('load', () => {
137
- const statusCode = xhr.status;
138
- const xhrResponse = xhr.response;
139
- if (statusCode < 200 || statusCode > 299) {
140
- if (xhrResponse.message) {
141
- /**
142
- * Uploading file failed.
143
- *
144
- * @error fileuploader-uploading-data-failed
145
- */
146
- return reject(new CKEditorError('fileuploader-uploading-data-failed', this, { message: xhrResponse.message }));
147
- }
148
- return reject(xhrResponse.error);
149
- }
150
- return resolve(xhrResponse);
151
- });
152
- xhr.addEventListener('error', () => reject(new Error('Network Error')));
153
- xhr.addEventListener('abort', () => reject(new Error('Abort')));
154
- xhr.send(formData);
155
- });
156
- }
157
- }
158
- /**
159
- * Transforms Base64 string data into file.
160
- *
161
- * @param base64 String data.
162
- */
163
- function _base64ToBlob(base64, sliceSize = 512) {
164
- try {
165
- const contentType = base64.match(BASE64_HEADER_REG_EXP)[1];
166
- const base64Data = atob(base64.replace(BASE64_HEADER_REG_EXP, ''));
167
- const byteArrays = [];
168
- for (let offset = 0; offset < base64Data.length; offset += sliceSize) {
169
- const slice = base64Data.slice(offset, offset + sliceSize);
170
- const byteNumbers = new Array(slice.length);
171
- for (let i = 0; i < slice.length; i++) {
172
- byteNumbers[i] = slice.charCodeAt(i);
173
- }
174
- byteArrays.push(new Uint8Array(byteNumbers));
175
- }
176
- return new Blob(byteArrays, { type: contentType });
177
- }
178
- catch {
179
- /**
180
- * Problem with decoding Base64 image data.
181
- *
182
- * @error fileuploader-decoding-image-data-error
183
- */
184
- throw new CKEditorError('fileuploader-decoding-image-data-error', null);
185
- }
186
- }
187
- /**
188
- * Checks that string is Base64.
189
- */
190
- function _isBase64(string) {
191
- if (typeof string !== 'string') {
192
- return false;
193
- }
194
- return !!string.match(BASE64_HEADER_REG_EXP)?.length;
195
- }
@@ -1,68 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
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 class UploadGateway {
14
- /**
15
- * CKEditor Cloud Services access token.
16
- */
17
- _token;
18
- /**
19
- * CKEditor Cloud Services API address.
20
- */
21
- _apiAddress;
22
- /**
23
- * Creates `UploadGateway` instance.
24
- *
25
- * @param token Token used for authentication.
26
- * @param apiAddress API address.
27
- */
28
- constructor(token, apiAddress) {
29
- if (!token) {
30
- /**
31
- * Token must be provided.
32
- *
33
- * @error uploadgateway-missing-token
34
- */
35
- throw new CKEditorError('uploadgateway-missing-token', null);
36
- }
37
- if (!apiAddress) {
38
- /**
39
- * Api address must be provided.
40
- *
41
- * @error uploadgateway-missing-api-address
42
- */
43
- throw new CKEditorError('uploadgateway-missing-api-address', null);
44
- }
45
- this._token = token;
46
- this._apiAddress = apiAddress;
47
- }
48
- /**
49
- * Creates a {@link module:cloud-services/uploadgateway/fileuploader~FileUploader} instance that wraps
50
- * file upload process. The file is being sent at a time when the
51
- * {@link module:cloud-services/uploadgateway/fileuploader~FileUploader#send} method is called.
52
- *
53
- * ```ts
54
- * const token = await Token.create( 'https://token-endpoint' );
55
- * new UploadGateway( token, 'https://example.org' )
56
- * .upload( 'FILE' )
57
- * .onProgress( ( data ) => console.log( data ) )
58
- * .send()
59
- * .then( ( response ) => console.log( response ) );
60
- * ```
61
- *
62
- * @param {Blob|String} fileOrData A blob object or a data string encoded with Base64.
63
- * @returns {module:cloud-services/uploadgateway/fileuploader~FileUploader} Returns `FileUploader` instance.
64
- */
65
- upload(fileOrData) {
66
- return new FileUploader(fileOrData, this._token, this._apiAddress);
67
- }
68
- }
File without changes
File without changes
File without changes