uploadgateway.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import FileUploader from './fileuploader';
  7. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  8. /**
  9. * UploadGateway abstracts file uploads to CKEditor Cloud Services.
  10. */
  11. export default class UploadGateway {
  12. /**
  13. * Creates `UploadGateway` instance.
  14. *
  15. * @param {Token} token Token used for authentication.
  16. * @param {String} apiAddress API address.
  17. */
  18. constructor( token, apiAddress ) {
  19. if ( !token ) {
  20. /**
  21. * Token must be provided.
  22. *
  23. * @error uploadgateway-missing-token
  24. */
  25. throw new CKEditorError( 'uploadgateway-missing-token: Token must be provided.', null );
  26. }
  27. if ( !apiAddress ) {
  28. /**
  29. * Api address must be provided.
  30. *
  31. * @error uploadgateway-missing-api-address
  32. */
  33. throw new CKEditorError( 'uploadgateway-missing-api-address: Api address must be provided.', null );
  34. }
  35. /**
  36. * CKEditor Cloud Services access token.
  37. *
  38. * @type {Token}
  39. * @private
  40. */
  41. this._token = token;
  42. /**
  43. * CKEditor Cloud Services API address.
  44. *
  45. * @type {String}
  46. * @private
  47. */
  48. this._apiAddress = apiAddress;
  49. }
  50. /**
  51. * Creates a {@link FileUploader} instance that wraps file upload process.
  52. * The file is being sent at a time when the method {@link FileUploader#then then} is called
  53. * or when {@link FileUploader#send send} method is called.
  54. *
  55. * const token = await Token.create( 'https://token-endpoint' );
  56. * new UploadGateway( token, 'https://example.org' )
  57. * .upload( 'FILE' )
  58. * .onProgress( ( data ) => console.log( data ) )
  59. * .send()
  60. * .then( ( response ) => console.log( response ) );
  61. *
  62. * // OR
  63. *
  64. * const token = await Token.create( 'https://token-endpoint' );
  65. * new UploadGateway( token, 'https://example.org' )
  66. * .upload( 'FILE' )
  67. * .onProgress( ( data ) => console.log( data ) )
  68. * .send()
  69. * .then( ( response ) => console.log( response ) );
  70. *
  71. * @param {Blob/String} fileOrData A blob object or a data string encoded with Base64.
  72. * @returns {FileUploader} Returns `FileUploader` instance.
  73. */
  74. upload( fileOrData ) {
  75. return new FileUploader( fileOrData, this._token, this._apiAddress );
  76. }
  77. }