Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit ed7187b

Browse files
authored
Merge pull request #108 from ckeditor/i/5892
Fix: Restored a condition handling an edge case in upload vs abort promise chains. Closes ckeditor/ckeditor5#5892.
2 parents 64209ec + 2e29e9a commit ed7187b

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/filerepository.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,12 @@ class FileLoader {
440440
return this.file
441441
.then( file => this._reader.read( file ) )
442442
.then( data => {
443+
// Edge case: reader was aborted after file was read - double check for proper status.
444+
// It can happen when image was deleted during its upload.
445+
if ( this.status !== 'reading' ) {
446+
throw this.status;
447+
}
448+
443449
this.status = 'idle';
444450

445451
return data;

tests/filerepository.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,30 @@ describe( 'FileRepository', () => {
582582

583583
return promise;
584584
} );
585+
586+
it( 'should abort upload if image is removed during the upload process', () => {
587+
const file = createNativeFileMock();
588+
const loader = fileRepository.createLoader( file );
589+
590+
sinon.stub( loader._reader, 'read' ).callsFake( () => {
591+
expect( loader.status ).to.equal( 'reading' );
592+
593+
// Reader is being aborted after file was read.
594+
// It can happen if an element (and its file that is being uploaded) will be removed during the upload process.
595+
loader.status = 'aborted';
596+
} );
597+
598+
return loader.read()
599+
.then(
600+
() => {
601+
throw new Error( 'Supposed to be rejected.' );
602+
},
603+
status => {
604+
expect( status ).to.equal( 'aborted' );
605+
expect( loader.status ).to.equal( 'aborted' );
606+
}
607+
);
608+
} );
585609
} );
586610

587611
describe( 'upload()', () => {

0 commit comments

Comments
 (0)