This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathutils.js
97 lines (86 loc) · 3.35 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
94
95
96
97
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module image/imageupload/utils
*/
/* global fetch, File */
/**
* Checks if a given file is an image.
*
* @param {File} file
* @returns {Boolean}
*/
export function isImageType( file ) {
const types = /^image\/(jpeg|png|gif|bmp)$/;
return types.test( file.type );
}
/**
* Creates a promise which fetches the image local source (base64 or blob) and resolves with a `File` object.
*
* @param {module:engine/view/element~Element} image Image which source to fetch.
* @returns {Promise.<File>} A promise which resolves when image source is fetched and converted to `File` instance.
* It resolves with a `File` object. If there were any errors during file processing the promise will be rejected.
*/
export function fetchLocalImage( image ) {
return new Promise( ( resolve, reject ) => {
const imageSrc = image.getAttribute( 'src' );
// Fetch works asynchronously and so does not block browser UI when processing data.
fetch( imageSrc )
.then( resource => resource.blob() )
.then( blob => {
const mimeType = getImageMimeType( blob, imageSrc );
const ext = mimeType.replace( 'image/', '' );
const filename = `image.${ ext }`;
const file = createFileFromBlob( blob, filename, mimeType );
file ? resolve( file ) : reject();
} )
.catch( reject );
} );
}
/**
* Checks whether given node is an image element with local source (base64 or blob).
*
* @param {module:engine/view/node~Node} node Node to check.
* @returns {Boolean}
*/
export function isLocalImage( node ) {
if ( !node.is( 'element', 'img' ) || !node.getAttribute( 'src' ) ) {
return false;
}
return node.getAttribute( 'src' ).match( /^data:image\/\w+;base64,/g ) ||
node.getAttribute( 'src' ).match( /^blob:/g );
}
// Extracts image type based on its blob representation or its source.
//
// @param {String} src Image src attribute value.
// @param {Blob} blob Image blob representation.
// @returns {String}
function getImageMimeType( blob, src ) {
if ( blob.type ) {
return blob.type;
} else if ( src.match( /data:(image\/\w+);base64/ ) ) {
return src.match( /data:(image\/\w+);base64/ )[ 1 ].toLowerCase();
} else {
// Fallback to 'jpeg' as common extension.
return 'image/jpeg';
}
}
// Creates `File` instance from the given `Blob` instance using specified filename.
//
// @param {Blob} blob The `Blob` instance from which file will be created.
// @param {String} filename Filename used during file creation.
// @param {String} mimeType File mime type.
// @returns {File|null} The `File` instance created from the given blob or `null` if `File API` is not available.
function createFileFromBlob( blob, filename, mimeType ) {
try {
return new File( [ blob ], filename, { type: mimeType } );
} catch ( err ) {
// Edge does not support `File` constructor ATM, see https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/9551546/.
// However, the `File` function is present (so cannot be checked with `!window.File` or `typeof File === 'function'`), but
// calling it with `new File( ... )` throws an error. This try-catch prevents that. Also when the function will
// be implemented correctly in Edge the code will start working without any changes (see #247).
return null;
}
}