valor-software/ng2-file-upload

onWhenAddingFileFailed never fires

yauhenkashko opened this issue · 4 comments

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:

Pull request with the fix.

I also encountered this problem.

are there any news when this bug is gonna be fixed? Is there any workarround available?

Until the fix is released I used the following temporary workaround:

//TODO: Remove WorkaroundFileUploader (file-uploader-fix.class.ts) and replace with FileUploader when 0 index issue with failFilterIndex will be resolved
export class WorkaroundFileUploader extends FileUploader {

    constructor(options: FileUploaderOptions) {
        super(options);
        //Add filter stub at 0 index to avoid the issue
        this.options.filters?.unshift({ name: "zero-index-filter-stub", fn: () => true });
    }

    //Override FileUploader's _isValidFile because it never changes value of the failFilterIndex to greater then 0 due to incorrect if condition
    protected _isValidFile(file: FileLikeObject, filters: FilterFunction[], options: FileUploaderOptions): boolean {

        this._failFilterIndex = filters.length
            ? filters.findIndex((filter: FilterFunction) => !filter.fn.call(this, file, options))
            : -1;

        return this._failFilterIndex == -1;
    }
}