Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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