Skip to content

Commit 317b501

Browse files
committed
match partial wildcard content types
resolve #499
1 parent 04abcf0 commit 317b501

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ describe('isContentTypeAllowed function', () => {
5151
const uploader = new NgUploaderService(1, ['image/jpeg', 'image/png', 'image/gif']);
5252
expect(uploader.isContentTypeAllowed('image/webm')).toBeFalsy();
5353
});
54+
55+
it('partial wildcard match', () => {
56+
const uploader = new NgUploaderService(1, ['image/*', 'video/mp4']);
57+
expect(uploader.isContentTypeAllowed('image/jpeg')).toBeTruthy();
58+
expect(uploader.isContentTypeAllowed('image/gif')).toBeTruthy();
59+
expect(uploader.isContentTypeAllowed('image/png')).toBeTruthy();
60+
expect(uploader.isContentTypeAllowed('video/avi')).toBeFalsy();
61+
expect(uploader.isContentTypeAllowed('video/mp4')).toBeTruthy();
62+
});
5463
});
5564

5665
describe('allContentTypesAllowed function', () => {

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,39 @@ export class NgUploaderService {
284284
}
285285

286286
allContentTypesAllowed(): boolean {
287-
return this.contentTypes.find((type: string) => type === '*') !== undefined;
287+
return this.contentTypes.includes('*');
288+
}
289+
290+
partialContentTypesAllowed(mimetype: string): boolean {
291+
const partialWildcardRegExp = /\w*\/\*/;
292+
if (mimetype.includes('/')) {
293+
const partialContentTypesAllowed = this.contentTypes
294+
.filter((type: string) => type.match(partialWildcardRegExp))
295+
.map((type: string) => {
296+
return type.split('/')[0];
297+
});
298+
299+
if (partialContentTypesAllowed && partialContentTypesAllowed.length > 0) {
300+
const partialMimetype = mimetype.split('/')[0];
301+
return partialContentTypesAllowed.includes(partialMimetype);
302+
}
303+
304+
return false;
305+
}
306+
307+
return false;
288308
}
289309

290310
isContentTypeAllowed(mimetype: string): boolean {
291311
if (this.allContentTypesAllowed()) {
292312
return true;
293313
}
294-
return this.contentTypes.find((type: string) => type === mimetype) !== undefined;
314+
315+
if (this.partialContentTypesAllowed(mimetype)) {
316+
return true;
317+
}
318+
319+
return this.contentTypes.includes(mimetype);
295320
}
296321

297322
isFileSizeAllowed(fileSize: number): boolean {

0 commit comments

Comments
 (0)