@ckeditor/ckeditor5-upload 35.2.1 → 35.3.1
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +16 -6
- package/src/adapters/base64uploadadapter.js +63 -76
- package/src/adapters/simpleuploadadapter.js +151 -270
- package/src/filereader.js +76 -96
- package/src/filerepository.js +480 -623
- package/src/index.js +0 -2
- package/src/ui/filedialogbuttonview.js +119 -139
package/src/filereader.js
CHANGED
@@ -2,107 +2,87 @@
|
|
2
2
|
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
4
|
*/
|
5
|
-
|
6
5
|
/**
|
7
6
|
* @module upload/filereader
|
8
7
|
*/
|
9
|
-
|
10
8
|
/* globals window */
|
11
|
-
|
12
|
-
import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
|
13
|
-
import mix from '@ckeditor/ckeditor5-utils/src/mix';
|
14
|
-
|
9
|
+
import { Observable } from '@ckeditor/ckeditor5-utils/src/observablemixin';
|
15
10
|
/**
|
16
11
|
* Wrapper over the native `FileReader`.
|
17
12
|
*/
|
18
|
-
export default class FileReader {
|
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
|
-
reject( 'aborted' );
|
94
|
-
};
|
95
|
-
|
96
|
-
this._reader.readAsDataURL( file );
|
97
|
-
} );
|
98
|
-
}
|
99
|
-
|
100
|
-
/**
|
101
|
-
* Aborts file reader.
|
102
|
-
*/
|
103
|
-
abort() {
|
104
|
-
this._reader.abort();
|
105
|
-
}
|
13
|
+
export default class FileReader extends Observable {
|
14
|
+
/**
|
15
|
+
* Creates an instance of the FileReader.
|
16
|
+
*/
|
17
|
+
constructor() {
|
18
|
+
super();
|
19
|
+
const reader = new window.FileReader();
|
20
|
+
/**
|
21
|
+
* Instance of native FileReader.
|
22
|
+
*
|
23
|
+
* @private
|
24
|
+
* @member {FileReader} #_reader
|
25
|
+
*/
|
26
|
+
this._reader = reader;
|
27
|
+
this._data = undefined;
|
28
|
+
/**
|
29
|
+
* Number of bytes loaded.
|
30
|
+
*
|
31
|
+
* @readonly
|
32
|
+
* @observable
|
33
|
+
* @member {Number} #loaded
|
34
|
+
*/
|
35
|
+
this.set('loaded', 0);
|
36
|
+
reader.onprogress = evt => {
|
37
|
+
this.loaded = evt.loaded;
|
38
|
+
};
|
39
|
+
}
|
40
|
+
/**
|
41
|
+
* Returns error that occurred during file reading.
|
42
|
+
*
|
43
|
+
* @returns {Error}
|
44
|
+
*/
|
45
|
+
get error() {
|
46
|
+
return this._reader.error;
|
47
|
+
}
|
48
|
+
/**
|
49
|
+
* Holds the data of an already loaded file. The file must be first loaded
|
50
|
+
* by using {@link module:upload/filereader~FileReader#read `read()`}.
|
51
|
+
*
|
52
|
+
* @type {File|undefined}
|
53
|
+
*/
|
54
|
+
get data() {
|
55
|
+
return this._data;
|
56
|
+
}
|
57
|
+
/**
|
58
|
+
* Reads the provided file.
|
59
|
+
*
|
60
|
+
* @param {File} file Native File object.
|
61
|
+
* @returns {Promise.<String>} Returns a promise that will be resolved with file's content.
|
62
|
+
* The promise will be rejected in case of an error or when the reading process is aborted.
|
63
|
+
*/
|
64
|
+
read(file) {
|
65
|
+
const reader = this._reader;
|
66
|
+
this.total = file.size;
|
67
|
+
return new Promise((resolve, reject) => {
|
68
|
+
reader.onload = () => {
|
69
|
+
const result = reader.result;
|
70
|
+
this._data = result;
|
71
|
+
resolve(result);
|
72
|
+
};
|
73
|
+
reader.onerror = () => {
|
74
|
+
reject('error');
|
75
|
+
};
|
76
|
+
reader.onabort = () => {
|
77
|
+
reject('aborted');
|
78
|
+
};
|
79
|
+
this._reader.readAsDataURL(file);
|
80
|
+
});
|
81
|
+
}
|
82
|
+
/**
|
83
|
+
* Aborts file reader.
|
84
|
+
*/
|
85
|
+
abort() {
|
86
|
+
this._reader.abort();
|
87
|
+
}
|
106
88
|
}
|
107
|
-
|
108
|
-
mix( FileReader, ObservableMixin );
|