Skip to content

Commit d7242d3

Browse files
AppOrchestramontymxb
authored andcommitted
Update ParseFile to handle a data URI containing a number (#483)
* Update ParseFile to handle datauri containing a number * Improve the regex to accept all valid mime types, and to handle the charset param
1 parent f71142a commit d7242d3

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/ParseFile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export type FileSource = {
2525
};
2626

2727
var dataUriRegexp =
28-
/^data:([a-zA-Z]*\/[a-zA-Z+.-]*);(charset=[a-zA-Z0-9\-\/\s]*,)?base64,/;
28+
/^data:([a-zA-Z]+\/[-a-zA-Z0-9+.]+)(;charset=[a-zA-Z0-9\-\/]*)?;base64,/;
2929

3030
function b64Digit(number: number): string {
3131
if (number < 26) {

src/__tests__/ParseFile-test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,38 @@ describe('ParseFile', () => {
3838
expect(file._source.type).toBe('');
3939
});
4040

41-
it('can extact data type from base64', () => {
41+
it('can extract data type from base64', () => {
4242
var file = new ParseFile('parse.txt', {
4343
base64: 'data:image/png;base64,ParseA=='
4444
});
4545
expect(file._source.base64).toBe('ParseA==');
4646
expect(file._source.type).toBe('image/png');
4747
});
4848

49+
it('can extract data type from base64 with data type containing a number', () => {
50+
var file = new ParseFile('parse.m4a', {
51+
base64: 'data:audio/m4a;base64,ParseA=='
52+
});
53+
expect(file._source.base64).toBe('ParseA==');
54+
expect(file._source.type).toBe('audio/m4a');
55+
});
56+
57+
it('can extract data type from base64 with a complex mime type', () => {
58+
var file = new ParseFile('parse.kml', {
59+
base64: 'data:application/vnd.google-earth.kml+xml;base64,ParseA=='
60+
});
61+
expect(file._source.base64).toBe('ParseA==');
62+
expect(file._source.type).toBe('application/vnd.google-earth.kml+xml');
63+
});
64+
65+
it('can extract data type from base64 with a charset param', () => {
66+
var file = new ParseFile('parse.kml', {
67+
base64: 'data:application/vnd.3gpp.pic-bw-var;charset=utf-8;base64,ParseA=='
68+
});
69+
expect(file._source.base64).toBe('ParseA==');
70+
expect(file._source.type).toBe('application/vnd.3gpp.pic-bw-var');
71+
});
72+
4973
it('can create files with byte arrays', () => {
5074
var file = new ParseFile('parse.txt', [61, 170, 236, 120]);
5175
expect(file._source.base64).toBe('ParseA==');

0 commit comments

Comments
 (0)