Skip to content

Commit 04abcf0

Browse files
artolshanskyirmana
authored andcommitted
feat: max file size validation
1 parent c87a2f2 commit 04abcf0

File tree

6 files changed

+20
-6
lines changed

6 files changed

+20
-6
lines changed

projects/ngx-uploader/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export interface UploaderOptions {
8989
concurrency: number; // number of files uploaded at the same time
9090
allowedContentTypes?: string[]; // content types allowed (default *)
9191
maxUploads?: number; // max number of files the user can upload
92+
maxFileSize?: number; // max size of the file in bytes the user can upload
9293
}
9394
9495
export interface UploadProgress {
@@ -200,7 +201,7 @@ export class AppHomeComponent {
200201
dragOver: boolean;
201202
202203
constructor() {
203-
this.options = { concurrency: 1, maxUploads: 3 };
204+
this.options = { concurrency: 1, maxUploads: 3, maxFileSize: 1000000 };
204205
this.files = []; // local uploading files array
205206
this.uploadInput = new EventEmitter<UploadInput>(); // input events, we use this to emit data to ngx-uploader
206207
this.humanizeBytes = humanizeBytes;

projects/ngx-uploader/src/lib/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface UploaderOptions {
44
concurrency: number;
55
allowedContentTypes?: string[];
66
maxUploads?: number;
7+
maxFileSize?: number;
78
}
89

910
export interface BlobFile extends Blob {

projects/ngx-uploader/src/lib/ng-file-drop.directive.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export class NgFileDropDirective implements OnInit, OnDestroy {
2525
const concurrency = this.options && this.options.concurrency || Number.POSITIVE_INFINITY;
2626
const allowedContentTypes = this.options && this.options.allowedContentTypes || ['*'];
2727
const maxUploads = this.options && this.options.maxUploads || Number.POSITIVE_INFINITY;
28-
this.upload = new NgUploaderService(concurrency, allowedContentTypes, maxUploads);
28+
const maxFileSize = this.options && this.options.maxFileSize || Number.POSITIVE_INFINITY;
29+
this.upload = new NgUploaderService(concurrency, allowedContentTypes, maxUploads, maxFileSize);
2930

3031
this.el = this.elementRef.nativeElement;
3132

projects/ngx-uploader/src/lib/ng-file-select.directive.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export class NgFileSelectDirective implements OnInit, OnDestroy {
2525
const concurrency = this.options && this.options.concurrency || Number.POSITIVE_INFINITY;
2626
const allowedContentTypes = this.options && this.options.allowedContentTypes || ['*'];
2727
const maxUploads = this.options && this.options.maxUploads || Number.POSITIVE_INFINITY;
28-
this.upload = new NgUploaderService(concurrency, allowedContentTypes, maxUploads);
28+
const maxFileSize = this.options && this.options.maxFileSize || Number.POSITIVE_INFINITY;
29+
this.upload = new NgUploaderService(concurrency, allowedContentTypes, maxUploads, maxFileSize);
2930

3031
this.el = this.elementRef.nativeElement;
3132
this.el.addEventListener('change', this.fileListener, false);

projects/ngx-uploader/src/lib/ngx-uploader.class.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,21 @@ export class NgUploaderService {
2222
subs: { id: string, sub: Subscription }[];
2323
contentTypes: string[];
2424
maxUploads: number;
25+
maxFileSize: number;
2526

2627
constructor(
2728
concurrency: number = Number.POSITIVE_INFINITY,
2829
contentTypes: string[] = ['*'],
29-
maxUploads: number = Number.POSITIVE_INFINITY
30+
maxUploads: number = Number.POSITIVE_INFINITY,
31+
maxFileSize: number = Number.POSITIVE_INFINITY
3032
) {
3133
this.queue = [];
3234
this.serviceEvents = new EventEmitter<UploadOutput>();
3335
this.uploadScheduler = new Subject();
3436
this.subs = [];
3537
this.contentTypes = contentTypes;
3638
this.maxUploads = maxUploads;
39+
this.maxFileSize = maxFileSize;
3740

3841
this.uploadScheduler
3942
.pipe(
@@ -45,7 +48,7 @@ export class NgUploaderService {
4548
handleFiles(incomingFiles: FileList): void {
4649
const allowedIncomingFiles: File[] = [].reduce.call(incomingFiles, (acc: File[], checkFile: File, i: number) => {
4750
const futureQueueLength = acc.length + this.queue.length + 1;
48-
if (this.isContentTypeAllowed(checkFile.type) && futureQueueLength <= this.maxUploads) {
51+
if (this.isContentTypeAllowed(checkFile.type) && futureQueueLength <= this.maxUploads && this.isFileSizeAllowed(checkFile.size)) {
4952
acc = acc.concat(checkFile);
5053
} else {
5154
const rejectedFile: UploadFile = this.makeUploadFile(checkFile, i);
@@ -291,6 +294,13 @@ export class NgUploaderService {
291294
return this.contentTypes.find((type: string) => type === mimetype) !== undefined;
292295
}
293296

297+
isFileSizeAllowed(fileSize: number): boolean {
298+
if (!this.maxFileSize) {
299+
return true;
300+
}
301+
return fileSize <= this.maxFileSize;
302+
}
303+
294304
makeUploadFile(file: File, index: number): UploadFile {
295305
return {
296306
fileIndex: index,

src/app/app.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class AppComponent {
1515
options: UploaderOptions;
1616

1717
constructor() {
18-
this.options = { concurrency: 1, maxUploads: 3 };
18+
this.options = { concurrency: 1, maxUploads: 3, maxFileSize: 1000000 };
1919
this.files = [];
2020
this.uploadInput = new EventEmitter<UploadInput>();
2121
this.humanizeBytes = humanizeBytes;

0 commit comments

Comments
 (0)