8
0

utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module image/imageupload/utils
  7. */
  8. /* global fetch, File */
  9. /**
  10. * Creates a RegExp used to test for image files.
  11. *
  12. * const imageType = createImageTypeRegExp( [ 'png', 'jpeg', 'svg+xml', 'vnd.microsoft.icon' ] );
  13. *
  14. * console.log( 'is supported image', imageType.test( file.type ) );
  15. *
  16. * @param {Array.<String>} types
  17. * @returns {RegExp}
  18. */
  19. export function createImageTypeRegExp( types ) {
  20. // Sanitize mime-type name which may include: "+", "-" or ".".
  21. const regExpSafeNames = types.map( type => type.replace( '+', '\\+' ) );
  22. return new RegExp( `^image\\/(${ regExpSafeNames.join( '|' ) })$` );
  23. }
  24. /**
  25. * Creates a promise which fetches the image local source (base64 or blob) and resolves with a `File` object.
  26. *
  27. * @param {module:engine/view/element~Element} image Image which source to fetch.
  28. * @returns {Promise.<File>} A promise which resolves when image source is fetched and converted to `File` instance.
  29. * It resolves with a `File` object. If there were any errors during file processing the promise will be rejected.
  30. */
  31. export function fetchLocalImage( image ) {
  32. return new Promise( ( resolve, reject ) => {
  33. const imageSrc = image.getAttribute( 'src' );
  34. // Fetch works asynchronously and so does not block browser UI when processing data.
  35. fetch( imageSrc )
  36. .then( resource => resource.blob() )
  37. .then( blob => {
  38. const mimeType = getImageMimeType( blob, imageSrc );
  39. const ext = mimeType.replace( 'image/', '' );
  40. const filename = `image.${ ext }`;
  41. const file = createFileFromBlob( blob, filename, mimeType );
  42. file ? resolve( file ) : reject();
  43. } )
  44. .catch( reject );
  45. } );
  46. }
  47. /**
  48. * Checks whether given node is an image element with local source (base64 or blob).
  49. *
  50. * @param {module:engine/view/node~Node} node Node to check.
  51. * @returns {Boolean}
  52. */
  53. export function isLocalImage( node ) {
  54. if ( !node.is( 'element', 'img' ) || !node.getAttribute( 'src' ) ) {
  55. return false;
  56. }
  57. return node.getAttribute( 'src' ).match( /^data:image\/\w+;base64,/g ) ||
  58. node.getAttribute( 'src' ).match( /^blob:/g );
  59. }
  60. // Extracts image type based on its blob representation or its source.
  61. //
  62. // @param {String} src Image src attribute value.
  63. // @param {Blob} blob Image blob representation.
  64. // @returns {String}
  65. function getImageMimeType( blob, src ) {
  66. if ( blob.type ) {
  67. return blob.type;
  68. } else if ( src.match( /data:(image\/\w+);base64/ ) ) {
  69. return src.match( /data:(image\/\w+);base64/ )[ 1 ].toLowerCase();
  70. } else {
  71. // Fallback to 'jpeg' as common extension.
  72. return 'image/jpeg';
  73. }
  74. }
  75. // Creates `File` instance from the given `Blob` instance using specified filename.
  76. //
  77. // @param {Blob} blob The `Blob` instance from which file will be created.
  78. // @param {String} filename Filename used during file creation.
  79. // @param {String} mimeType File mime type.
  80. // @returns {File|null} The `File` instance created from the given blob or `null` if `File API` is not available.
  81. function createFileFromBlob( blob, filename, mimeType ) {
  82. try {
  83. return new File( [ blob ], filename, { type: mimeType } );
  84. } catch ( err ) {
  85. // Edge does not support `File` constructor ATM, see https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/9551546/.
  86. // However, the `File` function is present (so cannot be checked with `!window.File` or `typeof File === 'function'`), but
  87. // calling it with `new File( ... )` throws an error. This try-catch prevents that. Also when the function will
  88. // be implemented correctly in Edge the code will start working without any changes (see #247).
  89. return null;
  90. }
  91. }