|
| 1 | +/** |
| 2 | + * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. |
| 3 | + * For licensing, see LICENSE.md. |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | +* @module easy-image/cloudservicesuploadadapter |
| 8 | +*/ |
| 9 | + |
| 10 | +import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; |
| 11 | +import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository'; |
| 12 | +import UploadGateway from '@ckeditor/ckeditor-cloudservices-core/src/uploadgateway/uploadgateway'; |
| 13 | + |
| 14 | +export default class CloudServicesUploadAdapter extends Plugin { |
| 15 | + static get requires() { |
| 16 | + return [ FileRepository ]; |
| 17 | + } |
| 18 | + |
| 19 | + init() { |
| 20 | + const editor = this.editor; |
| 21 | + const config = editor.config; |
| 22 | + |
| 23 | + const token = config.get( 'cloudServices.token' ); |
| 24 | + const uploadUrl = config.get( 'cloudServices.uploadUrl' ); |
| 25 | + |
| 26 | + // Application is in offline mode. |
| 27 | + if ( !token || !uploadUrl ) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + this._uploadGateway = new CloudServicesUploadAdapter._UploadGateway( token, uploadUrl ); |
| 32 | + |
| 33 | + editor.plugins.get( FileRepository ).createAdapter = loader => { |
| 34 | + return new Adapter( this._uploadGateway, loader ); |
| 35 | + }; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +class Adapter { |
| 40 | + constructor( uploadGateway, loader ) { |
| 41 | + this.uploadGateway = uploadGateway; |
| 42 | + |
| 43 | + this.loader = loader; |
| 44 | + } |
| 45 | + |
| 46 | + upload() { |
| 47 | + this.fileUploader = this.uploadGateway.upload( this.loader.file ); |
| 48 | + |
| 49 | + this.fileUploader.on( 'progress', ( evt, data ) => { |
| 50 | + this.loader.uploadTotal = data.total; |
| 51 | + this.loader.uploaded = data.uploaded; |
| 52 | + } ); |
| 53 | + |
| 54 | + return this.fileUploader.send(); |
| 55 | + } |
| 56 | + |
| 57 | + abort() { |
| 58 | + this.fileUploader.abort(); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Store the API in static property to easily overwrite it in tests. |
| 63 | +// Too bad dependency injection does not work in Webpack + ES 6 (const) + Babel. |
| 64 | +CloudServicesUploadAdapter._UploadGateway = UploadGateway; |
0 commit comments