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 max size file #481

Open
wants to merge 1 commit into
base: master
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 projects/ngx-uploader/src/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface UploaderOptions {
concurrency: number;
allowedContentTypes?: string[];
maxUploads?: number;
maxSizeFile?: 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 @@ -25,7 +25,8 @@ export class NgFileDropDirective implements OnInit, OnDestroy {
const concurrency = this.options && this.options.concurrency || Number.POSITIVE_INFINITY;
const allowedContentTypes = this.options && this.options.allowedContentTypes || ['*'];
const maxUploads = this.options && this.options.maxUploads || Number.POSITIVE_INFINITY;
this.upload = new NgUploaderService(concurrency, allowedContentTypes, maxUploads);
const maxSizeFile = this.options && this.options.maxSizeFile || Number.POSITIVE_INFINITY;
this.upload = new NgUploaderService(concurrency, allowedContentTypes, maxUploads, maxSizeFile);

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 @@ -25,7 +25,8 @@ export class NgFileSelectDirective implements OnInit, OnDestroy {
const concurrency = this.options && this.options.concurrency || Number.POSITIVE_INFINITY;
const allowedContentTypes = this.options && this.options.allowedContentTypes || ['*'];
const maxUploads = this.options && this.options.maxUploads || Number.POSITIVE_INFINITY;
this.upload = new NgUploaderService(concurrency, allowedContentTypes, maxUploads);
const maxSizeFile = this.options && this.options.maxSizeFile || Number.POSITIVE_INFINITY;
this.upload = new NgUploaderService(concurrency, allowedContentTypes, maxUploads, maxSizeFile);

this.el = this.elementRef.nativeElement;
this.el.addEventListener('change', this.fileListener, false);
Expand Down
10 changes: 8 additions & 2 deletions projects/ngx-uploader/src/lib/ngx-uploader.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ export class NgUploaderService {
subs: { id: string, sub: Subscription }[];
contentTypes: string[];
maxUploads: number;
maxSizeFile: number;

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

this.uploadScheduler
.pipe(
Expand All @@ -41,7 +43,7 @@ export class NgUploaderService {
handleFiles(incomingFiles: FileList): void {
const allowedIncomingFiles: File[] = [].reduce.call(incomingFiles, (acc: File[], checkFile: File, i: number) => {
const futureQueueLength = acc.length + this.queue.length + 1;
if (this.isContentTypeAllowed(checkFile.type) && futureQueueLength <= this.maxUploads) {
if (this.isContentTypeAllowed(checkFile.type) && this.isSizeAllowed(checkFile.size) && futureQueueLength <= this.maxUploads) {
acc = acc.concat(checkFile);
} else {
const rejectedFile: UploadFile = this.makeUploadFile(checkFile, i);
Expand Down Expand Up @@ -282,6 +284,10 @@ export class NgUploaderService {
return this.contentTypes.find((type: string) => type === mimetype) !== undefined;
}

isSizeAllowed(size: number): boolean {
return !(this.maxSizeFile && size > this.maxSizeFile);
}

makeUploadFile(file: File, index: number): UploadFile {
return {
fileIndex: index,
Expand Down