/
cloudservicesuploadadapter.ts
90 lines (72 loc) · 2.42 KB
/
cloudservicesuploadadapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module easy-image/cloudservicesuploadadapter
*/
import { Plugin } from 'ckeditor5/src/core.js';
import { FileRepository, type FileLoader, type UploadAdapter } from 'ckeditor5/src/upload.js';
import type { CloudServicesCore, CloudServices, UploadGateway, FileUploader } from '@ckeditor/ckeditor5-cloud-services';
/**
* A plugin that enables upload to [CKEditor Cloud Services](https://ckeditor.com/ckeditor-cloud-services/).
*
* It is mainly used by the {@link module:easy-image/easyimage~EasyImage} feature.
*
* After enabling this adapter you need to configure the CKEditor Cloud Services integration through
* {@link module:cloud-services/cloudservicesconfig~CloudServicesConfig `config.cloudServices`}.
*/
export default class CloudServicesUploadAdapter extends Plugin {
private _uploadGateway?: UploadGateway;
/**
* @inheritDoc
*/
public static get pluginName() {
return 'CloudServicesUploadAdapter' as const;
}
/**
* @inheritDoc
*/
public static get requires() {
return [ 'CloudServices', FileRepository ] as const;
}
/**
* @inheritDoc
*/
public init(): void {
const editor = this.editor;
const cloudServices: CloudServices = editor.plugins.get( 'CloudServices' );
const token = cloudServices.token;
const uploadUrl = cloudServices.uploadUrl;
if ( !token ) {
return;
}
const cloudServicesCore: CloudServicesCore = editor.plugins.get( 'CloudServicesCore' );
this._uploadGateway = cloudServicesCore.createUploadGateway( token, uploadUrl! );
editor.plugins.get( FileRepository ).createUploadAdapter = loader => {
return new Adapter( this._uploadGateway!, loader );
};
}
}
class Adapter implements UploadAdapter {
private uploadGateway: UploadGateway;
private loader: FileLoader;
private fileUploader?: FileUploader;
constructor( uploadGateway: UploadGateway, loader: FileLoader ) {
this.uploadGateway = uploadGateway;
this.loader = loader;
}
public upload() {
return this.loader.file.then( file => {
this.fileUploader = this.uploadGateway.upload( file! );
this.fileUploader.on( 'progress', ( evt, data ) => {
this.loader.uploadTotal = data.total;
this.loader.uploaded = data.uploaded;
} );
return this.fileUploader.send();
} );
}
public abort() {
this.fileUploader!.abort();
}
}