diff --git a/README.md b/README.md index 5b57069..c2de149 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,8 @@ Easy to use Angular2 directives for files upload ([demo](http://valor-software.g 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. `uploadFilesInSingleRequest` - If 'true', all files in the queue will be uploaded using one multipart request. Defaults to false. Notice this cannot be combined with 'disableMultipart' is 'true'. - + 9. `queueMaxSizeLimit` - States the maximum allowed size (in bytes) of all files in the queue that are going to be uploaded. + ### Events - `fileOver` - it fires during 'over' and 'out' events for Drop Area; returns `boolean`: `true` if file is over Drop Area, `false` in case of out. diff --git a/src/file-upload/file-uploader.class.ts b/src/file-upload/file-uploader.class.ts index 6622584..694dadd 100644 --- a/src/file-upload/file-uploader.class.ts +++ b/src/file-upload/file-uploader.class.ts @@ -30,6 +30,7 @@ export interface FileUploaderOptions { authToken?: string; maxFileSize?: number; queueLimit?: number; + queueMaxSizeLimit? : number; removeAfterUpload?: boolean; url?: string; disableMultipart?: boolean; @@ -78,6 +79,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 }); } @@ -467,6 +472,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; return !filters.length ? true : filters.every((filter: FilterFunction) => { diff --git a/src/spec/file-select.directive.spec.ts b/src/spec/file-select.directive.spec.ts index dfddc52..04537ae 100644 --- a/src/spec/file-select.directive.spec.ts +++ b/src/spec/file-select.directive.spec.ts @@ -63,6 +63,7 @@ describe('Directive: FileSelectDirective', () => { expect(options.isHTML5).toBeTruthy(); expect(options.removeAfterUpload).toBeFalsy(); expect(options.disableMultipart).toBeFalsy(); + expect(options.queueMaxSizeLimit).toBeUndefined(); }); it('can get filters', () => {