Skip to content

Commit 03ee3ff

Browse files
authored
fix: File upload fails when uploading base64 data (#1578)
1 parent 8f94427 commit 03ee3ff

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

integration/test/ParseFileTest.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('Parse.File', () => {
6464
assert.equal(data, 'ParseA==');
6565
});
6666

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

79+
it('can get file data from base64 (unsaved)', async () => {
80+
const file = new Parse.File('parse-server-logo', { base64: 'ParseA==' });
81+
const data = await file.getData();
82+
assert.equal(data, 'ParseA==');
83+
});
84+
7985
it('can get file data from full base64', async () => {
8086
const file = new Parse.File('parse-server-logo', {
8187
base64: 'data:image/jpeg;base64,ParseA==',

src/ParseFile.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ class ParseFile {
137137
} else if (data && typeof data.base64 === 'string') {
138138
const base64 = data.base64.split(',').slice(-1)[0];
139139
const dataType =
140-
specifiedType || data.base64.split(';').slice(0, 1)[0].split(':').slice(1, 2)[0] || 'text/plain';
140+
specifiedType ||
141+
data.base64.split(';').slice(0, 1)[0].split(':').slice(1, 2)[0] ||
142+
'text/plain';
143+
this._data = base64;
141144
this._source = {
142145
format: 'base64',
143146
base64,

src/__tests__/ParseFile-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,21 @@ describe('ParseFile', () => {
6464
const file = new ParseFile('parse.txt', { base64: 'YWJj' });
6565
expect(file._source.base64).toBe('YWJj');
6666
expect(file._source.type).toBe('text/plain');
67+
expect(file._data).toBe('YWJj');
6768
});
6869

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

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

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

8993
it('can extract data type from base64', () => {
@@ -92,6 +96,7 @@ describe('ParseFile', () => {
9296
});
9397
expect(file._source.base64).toBe('ParseA==');
9498
expect(file._source.type).toBe('image/png');
99+
expect(file._data).toBe('ParseA==');
95100
});
96101

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)