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

Commit

Permalink
Merge pull request #108 from ckeditor/i/5892
Browse files Browse the repository at this point in the history
Fix: Restored a condition handling an edge case in upload vs abort promise chains. Closes ckeditor/ckeditor5#5892.
  • Loading branch information
Reinmar authored Apr 21, 2020
2 parents 64209ec + 2e29e9a commit ed7187b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/filerepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,12 @@ class FileLoader {
return this.file
.then( file => this._reader.read( file ) )
.then( data => {
// Edge case: reader was aborted after file was read - double check for proper status.
// It can happen when image was deleted during its upload.
if ( this.status !== 'reading' ) {
throw this.status;
}

this.status = 'idle';

return data;
Expand Down
24 changes: 24 additions & 0 deletions tests/filerepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,30 @@ describe( 'FileRepository', () => {

return promise;
} );

it( 'should abort upload if image is removed during the upload process', () => {
const file = createNativeFileMock();
const loader = fileRepository.createLoader( file );

sinon.stub( loader._reader, 'read' ).callsFake( () => {
expect( loader.status ).to.equal( 'reading' );

// Reader is being aborted after file was read.
// It can happen if an element (and its file that is being uploaded) will be removed during the upload process.
loader.status = 'aborted';
} );

return loader.read()
.then(
() => {
throw new Error( 'Supposed to be rejected.' );
},
status => {
expect( status ).to.equal( 'aborted' );
expect( loader.status ).to.equal( 'aborted' );
}
);
} );
} );

describe( 'upload()', () => {
Expand Down

0 comments on commit ed7187b

Please sign in to comment.