Kaynağa Gözat

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.
Piotrek Koszuliński 5 yıl önce
ebeveyn
işleme
69710e64f3

+ 6 - 0
packages/ckeditor5-upload/src/filerepository.js

@@ -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;

+ 24 - 0
packages/ckeditor5-upload/tests/filerepository.js

@@ -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()', () => {