cloudservicesuploadadapter.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module easy-image/cloudservicesuploadadapter
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
  10. import UploadGateway from '@ckeditor/ckeditor-cloudservices-core/src/uploadgateway/uploadgateway';
  11. import CloudServices from '@ckeditor/ckeditor5-cloudservices/src/cloudservices';
  12. /**
  13. * A plugin which enables upload to Cloud Services.
  14. *
  15. * It is mainly used by the {@link module:easy-image/easyimage~EasyImage} feature.
  16. *
  17. * After enabling this adapter you need to configure the Cloud Services integration through
  18. * {@link module:easy-image/cloudservicesuploadadapter~CloudServicesAdapterConfig `config.cloudServices`}.
  19. *
  20. * @extends module:core/plugin~Plugin
  21. */
  22. export default class CloudServicesUploadAdapter extends Plugin {
  23. /**
  24. * @inheritDoc
  25. */
  26. static get requires() {
  27. return [ FileRepository, CloudServices ];
  28. }
  29. /**
  30. * @inheritDoc
  31. */
  32. init() {
  33. const editor = this.editor;
  34. const cloudServices = editor.plugins.get( CloudServices );
  35. const token = cloudServices.token;
  36. const uploadUrl = cloudServices.uploadUrl || 'https://files.cke-cs.com/upload/';
  37. this._uploadGateway = new CloudServicesUploadAdapter._UploadGateway( token, uploadUrl );
  38. editor.plugins.get( FileRepository ).createAdapter = loader => {
  39. return new Adapter( this._uploadGateway, loader );
  40. };
  41. }
  42. }
  43. /**
  44. * @private
  45. */
  46. class Adapter {
  47. constructor( uploadGateway, loader ) {
  48. this.uploadGateway = uploadGateway;
  49. this.loader = loader;
  50. }
  51. upload() {
  52. this.fileUploader = this.uploadGateway.upload( this.loader.file );
  53. this.fileUploader.on( 'progress', ( evt, data ) => {
  54. this.loader.uploadTotal = data.total;
  55. this.loader.uploaded = data.uploaded;
  56. } );
  57. return this.fileUploader.send();
  58. }
  59. abort() {
  60. this.fileUploader.abort();
  61. }
  62. }
  63. // Store the API in static property to easily overwrite it in tests.
  64. // Too bad dependency injection does not work in Webpack + ES 6 (const) + Babel.
  65. CloudServicesUploadAdapter._UploadGateway = UploadGateway;
  66. /**
  67. * The configuration of the {@link module:easy-image/cloudservicesuploadadapter~CloudServicesUploadAdapter Cloud Services upload adapter}.
  68. *
  69. * It is used mainly by the {@link module:easy-image/easyimage~EasyImage} feature.
  70. *
  71. * Read more in {@link module:easy-image/cloudservicesuploadadapter~CloudServicesAdapterConfig}.
  72. *
  73. * @member {module:easy-image/cloudservicesuploadadapter~CloudServicesAdapterConfig}
  74. * module:core/editor/editorconfig~EditorConfig#cloudServices
  75. */
  76. /**
  77. * The configuration of the {@link module:easy-image/cloudservicesuploadadapter~CloudServicesUploadAdapter Cloud Services upload adapter}.
  78. *
  79. * It is used mainly by the {@link module:easy-image/easyimage~EasyImage} feature.
  80. *
  81. * ClassicEditor
  82. * .create( editorElement, {
  83. * cloudServices: {
  84. * tokenUrl: '...'
  85. * }
  86. * } )
  87. * .then( ... )
  88. * .catch( ... );
  89. *
  90. * See {@link module:core/editor/editorconfig~EditorConfig all editor options}.
  91. *
  92. * @interface CloudServicesAdapterConfig
  93. */
  94. /**
  95. * The URL to which the files should be uploaded.
  96. *
  97. * @member {String} [module:easy-image/cloudservicesuploadadapter~CloudServicesAdapterConfig#uploadUrl='https://files.cke-cs.com/upload/']
  98. */
  99. /**
  100. * The token to the Cloud Services application. You can obtain it from the token service.
  101. *
  102. * @member {String} module:easy-image/cloudservicesuploadadapter~CloudServicesAdapterConfig#token
  103. */