8
0

uploadgateway.js 2.2 KB

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