From 82ded73bb1bb4ca772544fefce2c9d95afc9c60f Mon Sep 17 00:00:00 2001 From: Matteo Padovano Date: Thu, 1 Aug 2019 12:51:19 +0200 Subject: [PATCH] match partial wildcard content types resolve #499 --- .../src/lib/ngx-uploader.class.spec.ts | 9 ++++++ .../src/lib/ngx-uploader.class.ts | 31 +++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/projects/ngx-uploader/src/lib/ngx-uploader.class.spec.ts b/projects/ngx-uploader/src/lib/ngx-uploader.class.spec.ts index deec3c97..9aa6e150 100644 --- a/projects/ngx-uploader/src/lib/ngx-uploader.class.spec.ts +++ b/projects/ngx-uploader/src/lib/ngx-uploader.class.spec.ts @@ -51,6 +51,15 @@ describe('isContentTypeAllowed function', () => { const uploader = new NgUploaderService(1, ['image/jpeg', 'image/png', 'image/gif']); expect(uploader.isContentTypeAllowed('image/webm')).toBeFalsy(); }); + + it('partial wildcard match', () => { + const uploader = new NgUploaderService(1, ['image/*', 'video/mp4']); + expect(uploader.isContentTypeAllowed('image/jpeg')).toBeTruthy(); + expect(uploader.isContentTypeAllowed('image/gif')).toBeTruthy(); + expect(uploader.isContentTypeAllowed('image/png')).toBeTruthy(); + expect(uploader.isContentTypeAllowed('video/avi')).toBeFalsy(); + expect(uploader.isContentTypeAllowed('video/mp4')).toBeTruthy(); + }); }); describe('allContentTypesAllowed function', () => { diff --git a/projects/ngx-uploader/src/lib/ngx-uploader.class.ts b/projects/ngx-uploader/src/lib/ngx-uploader.class.ts index 97375e88..9dbfe559 100644 --- a/projects/ngx-uploader/src/lib/ngx-uploader.class.ts +++ b/projects/ngx-uploader/src/lib/ngx-uploader.class.ts @@ -284,14 +284,41 @@ export class NgUploaderService { } allContentTypesAllowed(): boolean { - return this.contentTypes.find((type: string) => type === '*') !== undefined; + return this.contentTypes.includes('*'); + } + + partialContentTypesAllowed(mimetype: string): boolean { + const partialWildcardRegExp = /\w*\/\*/; + if (mimetype.includes('/')) { + const partialContentTypesAllowed = this.contentTypes + .filter((type: string) => type.match(partialWildcardRegExp)) + .map((type: string) => { + return type.split('/')[0]; + }); + + console.log(partialContentTypesAllowed, mimetype.split('/')[0]); + + if (partialContentTypesAllowed && partialContentTypesAllowed.length > 0) { + const partialMimetype = mimetype.split('/')[0]; + return partialContentTypesAllowed.includes(partialMimetype); + } + + return false; + } + + return false; } isContentTypeAllowed(mimetype: string): boolean { if (this.allContentTypesAllowed()) { return true; } - return this.contentTypes.find((type: string) => type === mimetype) !== undefined; + + if (this.partialContentTypesAllowed(mimetype)) { + return true; + } + + return this.contentTypes.includes(mimetype); } isFileSizeAllowed(fileSize: number): boolean {