Skip to content

Commit 82ded73

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

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-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: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,41 @@ 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+
console.log(partialContentTypesAllowed, mimetype.split('/')[0]);
300+
301+
if (partialContentTypesAllowed && partialContentTypesAllowed.length > 0) {
302+
const partialMimetype = mimetype.split('/')[0];
303+
return partialContentTypesAllowed.includes(partialMimetype);
304+
}
305+
306+
return false;
307+
}
308+
309+
return false;
288310
}
289311

290312
isContentTypeAllowed(mimetype: string): boolean {
291313
if (this.allContentTypesAllowed()) {
292314
return true;
293315
}
294-
return this.contentTypes.find((type: string) => type === mimetype) !== undefined;
316+
317+
if (this.partialContentTypesAllowed(mimetype)) {
318+
return true;
319+
}
320+
321+
return this.contentTypes.includes(mimetype);
295322
}
296323

297324
isFileSizeAllowed(fileSize: number): boolean {

0 commit comments

Comments
 (0)