Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fileUploader): Added queueMaxSizeLimit, to limit the file size o… #1187

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Easy to use Angular2 directives for files upload ([demo](http://valor-software.g
5. `formatDataFunction` - Function to modify the request body. 'DisableMultipart' must be 'true' for this function to be called.
6. `formatDataFunctionIsAsync` - Informs if the function sent in 'formatDataFunction' is asynchronous. Defaults to false.
7. `parametersBeforeFiles` - States if additional parameters should be appended before or after the file. Defaults to false.
8. `queueMaxSizeLimit` - States the maximum allowed size (in bytes) of all files in the queue that are going to be uploaded.

### Events

Expand Down
16 changes: 16 additions & 0 deletions libs/ng2-file-upload/file-upload/file-uploader.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface FileUploaderOptions {
authToken?: string;
maxFileSize?: number;
queueLimit?: number;
queueMaxSizeLimit? : number;
removeAfterUpload?: boolean;
url: string;
disableMultipart?: boolean;
Expand Down Expand Up @@ -79,6 +80,10 @@ export class FileUploader {
this.autoUpload = this.options.autoUpload;
this.options.filters?.unshift({ name: 'queueLimit', fn: this._queueLimitFilter });

if (this.options.queueMaxSizeLimit) {
this.options.filters?.unshift({ name: 'queueMaxSizeLimit', fn: this._queueMaxSizeLimitFilter });
}

if (this.options.maxFileSize) {
this.options.filters?.unshift({ name: 'fileSize', fn: this._fileSizeFilter });
}
Expand Down Expand Up @@ -430,6 +435,17 @@ export class FileUploader {
return this.options.queueLimit === undefined || this.queue.length < this.options.queueLimit;
}

protected _queueMaxSizeLimitFilter(item: FileLikeObject): boolean {
let queueFileSize = 0;
let queueNotProcessedItems = this.queue.filter(queuedItem => !(queuedItem.isSuccess || queuedItem.isCancel || queuedItem.isError));
if(queueNotProcessedItems.length>0)
{
// total size of all queued items that are going to be uploaded
queueFileSize = queueNotProcessedItems.map(queuedItem => queuedItem.file.size).reduce((fileSizeA,fileSizeB)=> fileSizeA + fileSizeB);
}
return this.options.queueMaxSizeLimit === undefined || (queueFileSize + item.size) < this.options.queueMaxSizeLimit;
}

protected _isValidFile(file: FileLikeObject, filters: FilterFunction[], options: FileUploaderOptions): boolean {
this._failFilterIndex = -1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('Directive: FileSelectDirective', () => {
expect(options.isHTML5).toBeTruthy();
expect(options.removeAfterUpload).toBeFalsy();
expect(options.disableMultipart).toBeFalsy();
expect(options.queueMaxSizeLimit).toBeUndefined();
}

});
Expand Down