cloudservicesuploadadapter.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 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 './uploadgateway'
  11. /**
  12. * A plugin that enables upload to [CKEditor Cloud Services](https://ckeditor.com/ckeditor-cloud-services/).
  13. *
  14. * It is mainly used by the {@link module:easy-image/easyimage~EasyImage} feature.
  15. *
  16. * After enabling this adapter you need to configure the CKEditor Cloud Services integration through
  17. * {@link module:cloud-services/cloudservices~CloudServicesConfig `config.cloudServices`}.
  18. *
  19. * @extends module:core/plugin~Plugin
  20. */
  21. export default class CloudServicesUploadAdapter extends Plugin {
  22. /**
  23. * @inheritDoc
  24. */
  25. static get requires() {
  26. return [FileRepository]
  27. }
  28. /**
  29. * @inheritDoc
  30. */
  31. init() {
  32. const editor = this.editor
  33. const token = { value: 'Bearer ' + sessionStorage.getItem('vue_admin_template_token') }
  34. const uploadUrl = process.env.VUE_APP_IMAGE_UPLOAD_URL
  35. if (!token) {
  36. return
  37. }
  38. this._uploadGateway = new CloudServicesUploadAdapter._UploadGateway(token, uploadUrl)
  39. editor.plugins.get(FileRepository).createUploadAdapter = loader => {
  40. return new Adapter(this._uploadGateway, loader)
  41. }
  42. }
  43. }
  44. /**
  45. * @private
  46. */
  47. class Adapter {
  48. constructor(uploadGateway, loader) {
  49. this.uploadGateway = uploadGateway
  50. this.loader = loader
  51. }
  52. upload() {
  53. return this.loader.file.then(file => {
  54. this.fileUploader = this.uploadGateway.upload(file)
  55. this.fileUploader.on('progress', (evt, data) => {
  56. this.loader.uploadTotal = data.total
  57. this.loader.uploaded = data.uploaded
  58. })
  59. return this.fileUploader.send()
  60. })
  61. }
  62. abort() {
  63. this.fileUploader.abort()
  64. }
  65. }
  66. // Store the API in static property to easily overwrite it in tests.
  67. // Too bad dependency injection does not work in Webpack + ES 6 (const) + Babel.
  68. CloudServicesUploadAdapter._UploadGateway = UploadGateway