Skip to content

Commit

Permalink
fix: File upload fails when uploading base64 data (#1578)
Browse files Browse the repository at this point in the history
  • Loading branch information
musthafa1996 authored Nov 3, 2022
1 parent 8f94427 commit 03ee3ff
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
8 changes: 7 additions & 1 deletion integration/test/ParseFileTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Parse.File', () => {
assert.equal(data, 'ParseA==');
});

it('can get file data from base64', async () => {
it('can get file data from base64 (saved)', async () => {
const file = new Parse.File('parse-server-logo', { base64: 'ParseA==' });
await file.save();
let data = await file.getData();
Expand All @@ -76,6 +76,12 @@ describe('Parse.File', () => {
assert.equal(data, 'ParseA==');
});

it('can get file data from base64 (unsaved)', async () => {
const file = new Parse.File('parse-server-logo', { base64: 'ParseA==' });
const data = await file.getData();
assert.equal(data, 'ParseA==');
});

it('can get file data from full base64', async () => {
const file = new Parse.File('parse-server-logo', {
base64: 'data:image/jpeg;base64,ParseA==',
Expand Down
5 changes: 4 additions & 1 deletion src/ParseFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ class ParseFile {
} else if (data && typeof data.base64 === 'string') {
const base64 = data.base64.split(',').slice(-1)[0];
const dataType =
specifiedType || data.base64.split(';').slice(0, 1)[0].split(':').slice(1, 2)[0] || 'text/plain';
specifiedType ||
data.base64.split(';').slice(0, 1)[0].split(':').slice(1, 2)[0] ||
'text/plain';
this._data = base64;
this._source = {
format: 'base64',
base64,
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/ParseFile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,21 @@ describe('ParseFile', () => {
const file = new ParseFile('parse.txt', { base64: 'YWJj' });
expect(file._source.base64).toBe('YWJj');
expect(file._source.type).toBe('text/plain');
expect(file._data).toBe('YWJj');
});

it('can create files with base64 encoding (1 padding)', () => {
const file = new ParseFile('parse.txt', { base64: 'YWI=' });
expect(file._source.base64).toBe('YWI=');
expect(file._source.type).toBe('text/plain');
expect(file._data).toBe('YWI=');
});

it('can create files with base64 encoding (2 padding)', () => {
const file = new ParseFile('parse.txt', { base64: 'ParseA==' });
expect(file._source.base64).toBe('ParseA==');
expect(file._source.type).toBe('text/plain');
expect(file._data).toBe('ParseA==');
});

it('can set the default type to be text/plain when using base64', () => {
Expand All @@ -84,6 +87,7 @@ describe('ParseFile', () => {
});
expect(file._source.base64).toBe('ParseA==');
expect(file._source.type).toBe('text/plain');
expect(file._data).toBe('ParseA==');
});

it('can extract data type from base64', () => {
Expand All @@ -92,6 +96,7 @@ describe('ParseFile', () => {
});
expect(file._source.base64).toBe('ParseA==');
expect(file._source.type).toBe('image/png');
expect(file._data).toBe('ParseA==');
});

it('can extract data type from base64 with a filename parameter', () => {
Expand All @@ -100,6 +105,7 @@ describe('ParseFile', () => {
});
expect(file._source.base64).toBe('ParseA==');
expect(file._source.type).toBe('application/pdf');
expect(file._data).toBe('ParseA==');
});

it('can create files with file uri', () => {
Expand All @@ -116,6 +122,7 @@ describe('ParseFile', () => {
});
expect(file._source.base64).toBe('ParseA==');
expect(file._source.type).toBe('audio/m4a');
expect(file._data).toBe('ParseA==');
});

it('can extract data type from base64 with a complex mime type', () => {
Expand All @@ -124,6 +131,7 @@ describe('ParseFile', () => {
});
expect(file._source.base64).toBe('ParseA==');
expect(file._source.type).toBe('application/vnd.google-earth.kml+xml');
expect(file._data).toBe('ParseA==');
});

it('can extract data type from base64 with a charset param', () => {
Expand All @@ -132,18 +140,21 @@ describe('ParseFile', () => {
});
expect(file._source.base64).toBe('ParseA==');
expect(file._source.type).toBe('application/vnd.3gpp.pic-bw-var');
expect(file._data).toBe('ParseA==');
});

it('can create files with byte arrays', () => {
const file = new ParseFile('parse.txt', [61, 170, 236, 120]);
expect(file._source.base64).toBe('ParseA==');
expect(file._source.type).toBe('');
expect(file._data).toBe('ParseA==');
});

it('can create files with all types of characters', () => {
const file = new ParseFile('parse.txt', [11, 239, 191, 215, 80, 52]);
expect(file._source.base64).toBe('C++/11A0');
expect(file._source.type).toBe('');
expect(file._data).toBe('C++/11A0');
});

it('can create an empty file', () => {
Expand Down Expand Up @@ -341,6 +352,7 @@ describe('ParseFile', () => {
const file = new ParseFile('parse.txt', [61, 170, 236, 120], '', metadata, tags);
expect(file._source.base64).toBe('ParseA==');
expect(file._source.type).toBe('');
expect(file._data).toBe('ParseA==');
expect(file.metadata()).toBe(metadata);
expect(file.tags()).toBe(tags);
});
Expand Down

0 comments on commit 03ee3ff

Please sign in to comment.