Skip to content

Commit 16027ba

Browse files
committed
Merge pull request #279 from leeyeh/refactor-file-data
fix(File): add support to following use cases
2 parents d74ca44 + 781b85b commit 16027ba

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

src/file.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,15 +385,14 @@ module.exports = function(AV) {
385385
data.blob.type = guessedType;
386386
}
387387
this._source = AV.Promise.as(data.blob, guessedType);
388-
} else if (typeof(File) !== "undefined" && data instanceof global.File) {
388+
} else if (typeof File !== "undefined" && data instanceof global.File) {
389389
this._source = AV.Promise.as(data, guessedType);
390-
} else if (avConfig.isNode && global.Buffer.isBuffer(data)) {
390+
} else if (typeof Buffer !== "undefined" && global.Buffer.isBuffer(data)) {
391391
// use global.Buffer to prevent browserify pack Buffer module
392-
this.attributes.base64 = data.toString('base64');
393-
this._source = AV.Promise.as(this.attributes.base64, guessedType);
394392
this.attributes.metaData.size = data.length;
393+
this._source = AV.Promise.as(data, guessedType);
395394
} else if (_.isString(data)) {
396-
throw "Creating a AV.File from a String is not yet supported.";
395+
throw new Error("Creating a AV.File from a String is not yet supported.");
397396
}
398397
};
399398

@@ -720,6 +719,9 @@ module.exports = function(AV) {
720719
if (this.attributes.base64) {
721720
data.base64 = this.attributes.base64;
722721
return AV._request('files', this.attributes.name, null, 'POST', data);
722+
} else if (typeof Buffer !== "undefined" && global.Buffer.isBuffer(file)) {
723+
data.base64 = file.toString('base64');
724+
return AV._request('files', this.attributes.name, null, 'POST', data);
723725
} else {
724726
return readAsync(file).then(function(base64) {
725727
data.base64 = base64;

test/file.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,15 @@ describe('files', function() {
5353
});
5454
});
5555

56-
if(AV._isNode){
56+
if(typeof Buffer !== 'undefined'){
5757
describe('Saving buffer in node.js', function() {
58-
it('should be saved', function(done) {
58+
it('should be saved', function() {
5959
var file = new AV.File('myfile.txt', new Buffer('hello world'));
60-
file.save().then(function() {
60+
return file.save().then(function() {
6161
expect(file.size()).to.be(11);
6262
expect(file.ownerId()).to.be.ok();
6363
expect(file.id).to.be.ok();
64-
expect(file.thumbnailURL(200, 100)).to.be(file.url() + '?imageView/2/w/200/h/100/q/100/format/png');
65-
file.destroy().then(function() {
66-
done();
67-
}, function(error) {
68-
done(error);
69-
});
70-
}, function(error) {
71-
done(error);
64+
return file.destroy();
7265
});
7366
});
7467
});

0 commit comments

Comments
 (0)