Skip to content

Commit

Permalink
Added maxFiles feature
Browse files Browse the repository at this point in the history
  • Loading branch information
bop10 authored and jkuri committed Jun 18, 2018
1 parent fee88db commit f33b90a
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions projects/ngx-uploader/src/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Subscription } from 'rxjs';
export interface UploaderOptions {
concurrency: number;
allowedContentTypes?: string[];
maxUploads?: number;
}

export interface BlobFile extends Blob {
Expand Down
3 changes: 2 additions & 1 deletion projects/ngx-uploader/src/lib/ng-file-drop.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export class NgFileDropDirective implements OnInit, OnDestroy {
this._sub = [];
const concurrency = this.options && this.options.concurrency || Number.POSITIVE_INFINITY;
const allowedContentTypes = this.options && this.options.allowedContentTypes || ['*'];
this.upload = new NgUploaderService(concurrency, allowedContentTypes);
const maxUploads = this.options && this.options.maxUploads || Number.POSITIVE_INFINITY;
this.upload = new NgUploaderService(concurrency, allowedContentTypes, maxUploads);

this.el = this.elementRef.nativeElement;

Expand Down
3 changes: 2 additions & 1 deletion projects/ngx-uploader/src/lib/ng-file-select.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export class NgFileSelectDirective implements OnInit, OnDestroy {
this._sub = [];
const concurrency = this.options && this.options.concurrency || Number.POSITIVE_INFINITY;
const allowedContentTypes = this.options && this.options.allowedContentTypes || ['*'];
this.upload = new NgUploaderService(concurrency, allowedContentTypes);
const maxUploads = this.options && this.options.maxUploads || Number.POSITIVE_INFINITY;
this.upload = new NgUploaderService(concurrency, allowedContentTypes, maxUploads);

this.el = this.elementRef.nativeElement;
this.el.addEventListener('change', this.fileListener, false);
Expand Down
7 changes: 5 additions & 2 deletions projects/ngx-uploader/src/lib/ngx-uploader.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ export class NgUploaderService {
uploadScheduler: Subject<{ file: UploadFile, event: UploadInput }>;
subs: { id: string, sub: Subscription }[];
contentTypes: string[];
maxUploads: number;

constructor(concurrency: number = Number.POSITIVE_INFINITY, contentTypes: string[] = ['*']) {
constructor(concurrency: number = Number.POSITIVE_INFINITY, contentTypes: string[] = ['*'], maxUploads: number = Number.POSITIVE_INFINITY) {
this.queue = [];
this.serviceEvents = new EventEmitter<UploadOutput>();
this.uploadScheduler = new Subject();
this.subs = [];
this.contentTypes = contentTypes;
this.maxUploads = maxUploads;

this.uploadScheduler
.pipe(
Expand All @@ -38,7 +40,8 @@ export class NgUploaderService {

handleFiles(incomingFiles: FileList): void {
const allowedIncomingFiles: File[] = [].reduce.call(incomingFiles, (acc: File[], checkFile: File, i: number) => {
if (this.isContentTypeAllowed(checkFile.type)) {
const futureQueueLength = acc.length + this.queue.length + 1;
if (this.isContentTypeAllowed(checkFile.type) && futureQueueLength <= this.maxUploads) {
acc = acc.concat(checkFile);
} else {
const rejectedFile: UploadFile = this.makeUploadFile(checkFile, i);
Expand Down
2 changes: 1 addition & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class AppComponent {
options: UploaderOptions;

constructor() {
this.options = { concurrency: 1 };
this.options = { concurrency: 1, maxUploads: 3 };
this.files = [];
this.uploadInput = new EventEmitter<UploadInput>();
this.humanizeBytes = humanizeBytes;
Expand Down

0 comments on commit f33b90a

Please sign in to comment.