Skip to content

Commit

Permalink
match partial wildcard content types
Browse files Browse the repository at this point in the history
resolve bleenco#499
  • Loading branch information
mrbatista committed Aug 1, 2019
1 parent 04abcf0 commit 317b501
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
9 changes: 9 additions & 0 deletions projects/ngx-uploader/src/lib/ngx-uploader.class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
29 changes: 27 additions & 2 deletions projects/ngx-uploader/src/lib/ngx-uploader.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,39 @@ 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];
});

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 {
Expand Down

0 comments on commit 317b501

Please sign in to comment.