onWhenAddingFileFailed never fires #1221

Open
opened 2022-07-26 16:49:25 +00:00 by yauhenkashko · 1 comment
yauhenkashko commented 2022-07-26 16:49:25 +00:00 (Migrated from github.com)

There are 2 issues with _failFilterIndex that cause onWhenAddingFileFailed never to fire.

  1. If any filter fails the _failFilterIndex will never change to a value greater than 0 due to the Incorrect if (this._failFilterIndex) { condition:
 protected _isValidFile(file: FileLikeObject, filters: FilterFunction[], options: FileUploaderOptions): boolean {
   this._failFilterIndex = -1;

   return !filters.length ? true : filters.every((filter: FilterFunction) => {
----> if (this._failFilterIndex) {  <----
        this._failFilterIndex++;
      }

     return filter.fn.call(this, file, options);
   });
 }
  1. When the _isValidFile returns false (if any filter fails), this._failFilterIndex is always 0 (as we saw in the previous _isValidFile issue - see above). Сonsidering the previous statement, the invalid condition if(this._failFilterIndex) with a value of 0 will not fire _onWhenAddingFileFailed:
addToQueue(files: File[], _options?: FileUploaderOptions, filters?: [] | string): void {
 ...
     if (this._isValidFile(temp, arrayOfFilters, options)) {
     ...
     } else {
-----> if (this._failFilterIndex) {  <-----
         const filter = arrayOfFilters[ this._failFilterIndex ];
         this._onWhenAddingFileFailed(temp, filter, options);
       }
     }
   });
 ...
 }

As the result, the FileUploader's onWhenAddingFileFailed is not called.

Was broken during the upgrade to angular 11:

There are 2 issues with `_failFilterIndex` that cause `onWhenAddingFileFailed` never to fire. 1. If any filter fails the `_failFilterIndex` will never change to a value greater than `0` due to the Incorrect ```if (this._failFilterIndex) {``` condition: ``` protected _isValidFile(file: FileLikeObject, filters: FilterFunction[], options: FileUploaderOptions): boolean { this._failFilterIndex = -1; return !filters.length ? true : filters.every((filter: FilterFunction) => { ----> if (this._failFilterIndex) { <---- this._failFilterIndex++; } return filter.fn.call(this, file, options); }); } ``` 2. When the `_isValidFile` returns `false` (if any filter fails), `this._failFilterIndex` is always `0` (as we saw in the previous `_isValidFile` issue - see above). Сonsidering the previous statement, the invalid condition `if(this._failFilterIndex)` with a value of `0` will not fire _onWhenAddingFileFailed: ``` addToQueue(files: File[], _options?: FileUploaderOptions, filters?: [] | string): void { ... if (this._isValidFile(temp, arrayOfFilters, options)) { ... } else { -----> if (this._failFilterIndex) { <----- const filter = arrayOfFilters[ this._failFilterIndex ]; this._onWhenAddingFileFailed(temp, filter, options); } } }); ... } ``` As the result, the FileUploader's `onWhenAddingFileFailed` is not called. Was broken during the upgrade to angular 11: - [Related MR](https://github.com/valor-software/ng2-file-upload/pull/1177) - [Related commit with file-uploader.class.ts changes](https://github.com/valor-software/ng2-file-upload/pull/1177/commits/69cd64dc287c9bdd1c35af1062e27ce32a47e977#diff-a07d24a16d35f10dfbcb72b4103ebb5bad06480b602776354e553e4e1d4506bf)
yauhenkashko commented 2022-08-01 07:18:41 +00:00 (Migrated from github.com)

Pull request with the fix.

[Pull request](https://github.com/valor-software/ng2-file-upload/pull/1222) with the fix.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dc/ng2-file-upload#1221