1
votes

I am working on a project that need to store images on Firebase storage. I use Ckeditor to upload image, I follow Ckeditor's docs to write my own image upload adapter, but it din't not work. I cannot upload image to firebase and get back the image url. here is what I've tried

class MyUploadAdapter {
    constructor(loader) {
        this.loader = loader;
    }
    upload() {
        return this.loader.file
            .then(file => new Promise((resolve, reject) => {
                this._initRequest();
                this._initListeners(resolve, reject, file);
                this._sendRequest(file);
            }));
    }
    abort() {
        if (this.xhr) {
            this.xhr.abort();
        }
    }

    _initRequest() {
        const xhr = this.xhr = new XMLHttpRequest();
        xhr.open('POST', 'https://firebasestorage.googleapis.com/v0/b/smart-farming-e3e2d.appspot.com/o/images/', true);
        xhr.responseType = 'json';
    }

    // Initializes XMLHttpRequest listeners.
    _initListeners(resolve, reject, file) {
        const xhr = this.xhr;
        const loader = this.loader;
        const genericErrorText = `Couldn't upload file: ${file.name}.`;

        xhr.addEventListener('error', () => reject(genericErrorText));
        xhr.addEventListener('abort', () => reject());
        xhr.addEventListener('load', () => {
            const response = xhr.response;

            if (!response || response.error) {
                return reject(response && response.error ? response.error.message : genericErrorText);
            }

            resolve({
                default: response.url
            });
        });

        if (xhr.upload) {
            xhr.upload.addEventListener('progress', evt => {
                if (evt.lengthComputable) {
                    loader.uploadTotal = evt.total;
                    loader.uploaded = evt.loaded;
                }
            });
        }
    }

    _sendRequest(file) {
        const data = new FormData();
        data.append('upload', file);
        this.xhr.send(data);
    }
}

function MyCustomUploadAdapterPlugin(editor) {
    editor.plugins.get('FileRepository').createUploadAdapter = (loader) => {
        return new MyUploadAdapter(loader);
    };
}

const editorConfiguration = {
    extraPlugins: [MyCustomUploadAdapterPlugin],
};

and I call it to use in my Component like this

<CKEditor
editor={ClassicEditor}
// data="<p>Hello from CKEditor 5!</p>"
onInit={editor => {
    // console.log('Editor is ready to use!', editor);
}}
onChange={(event, editor) => {
    const data = editor.getData();
    console.log({ event, editor, data });
}}
config={editorConfiguration}

/>

Can anyone have experience with upload image from Ckeditor to firebase storage help me? Thanks u.

1

1 Answers

0
votes

I have also been trying to fix this for ages now. I have now been able to upload an image to my server by adding CORS to the FireBase URL

xhr.open('POST', `https://cors-anywhere.herokuapp.com/https://firebasestorage.googleapis.com/v0/b/smart-farming-e3e2d.appspot.com/o/${file.name}`, true);

If you are wondering how I got the file attribute you need to pass it in the function

_initRequest(file) {
                console.log(file.name)
                const xhr = this.xhr = new XMLHttpRequest();

                xhr.open('POST', `https://cors-anywhere.herokuapp.com/FIREBASE_URL_HERE{file.name}`, true);

                xhr.responseType = 'json';
            }

This should now upload your image to your Firebase storage