Skip to content

Commit d20782f

Browse files
committed
fix(File): parse bytes array to base64 then treat the source as base64
fix #281
1 parent 0dcc442 commit d20782f

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

src/file.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,10 @@ module.exports = function(AV) {
372372
this._guessedType = guessedType;
373373

374374
if (_.isArray(data)) {
375-
this.attributes.base64 = encodeBase64(data);
376-
this._source = AV.Promise.as(this.attributes.base64, guessedType);
377375
this.attributes.metaData.size = data.length;
378-
} else if (data && data.base64) {
376+
data = { base64: encodeBase64(data) };
377+
}
378+
if (data && data.base64) {
379379
var parseBase64 = require('./browserify-wrapper/parse-base64');
380380
var dataBase64 = parseBase64(data.base64, guessedType);
381381
this.attributes.base64 = dataURLToBase64(data.base64);

test/file.js

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,56 +78,48 @@ describe('files', function() {
7878

7979

8080
describe('Saving array', function() {
81-
it('should be saved', function(done) {
82-
setTimeout(function() {
83-
var bytes = [ 0xBE, 0xEF, 0xCA, 0xFE ];
81+
it('should be saved', function() {
82+
var bytes = [108,101,97,110,99,108,111,117,100];
8483
var file = new AV.File('myfile.txt', bytes);
85-
file.save().then(function() {
86-
expect(file.size()).to.be(4);
84+
return file.save().then(function() {
85+
expect(file.size()).to.be(9);
8786
expect(file.ownerId()).to.be.ok();
8887
expect(file.id).to.be.ok();
89-
file.destroy().then(function() {
90-
done();
91-
}, function(error) {
92-
done(error);
88+
return new AV.Promise(function(resolve, reject) {
89+
request(file.url()).end(function(err, res) {
90+
if (err) {
91+
return reject(err);
92+
}
93+
resolve(res);
94+
});
9395
});
94-
}, function(error) {
95-
done(error);
96+
}).then(function(res) {
97+
expect(res.text).to.be('leancloud');
98+
return file.destroy();
9699
});
97-
}, 1000);
98100
});
99101
});
100102

101103
describe('Saving file with object', function() {
102-
it('should be saved', function(done) {
103-
var bytes = [ 0xBE, 0xEF, 0xCA, 0xFE ];
104+
it('should be saved', function() {
105+
var bytes = [108,101,97,110,99,108,111,117,100];
104106
var file = new AV.File('myfile.txt', bytes);
105-
file.save().then(function() {
107+
return file.save().then(function() {
106108
var jobApplication = new AV.Object('JobApplication');
107109
jobApplication.set('applicantName', 'Joe Smith');
108110
jobApplication.set('applicantResumeFile', file);
109-
jobApplication.save().then(function(result) {
111+
return jobApplication.save().then(function(result) {
110112
expect(result.id).to.be.ok();
111113
var query = new AV.Query('JobApplication');
112-
query.get(result.id).then(function(ja) {
114+
return query.get(result.id).then(function(ja) {
113115
expect(ja.id).to.be.ok();
114116
var arf = ja.get('applicantResumeFile');
115117
expect(arf).to.be.ok();
116-
expect(arf.size()).to.be(4);
118+
expect(arf.size()).to.be(9);
117119
expect(arf.ownerId()).to.be.ok();
118-
file.destroy().then(function() {
119-
done();
120-
}, function(error) {
121-
done(error);
122-
});
123-
}).catch(function(error) {
124-
done(error);
120+
return file.destroy();
125121
});
126-
}, function(obj, error) {
127-
done(error);
128122
});
129-
}, function(error) {
130-
done(error);
131123
});
132124
});
133125
});

0 commit comments

Comments
 (0)