Skip to content

Commit 137be69

Browse files
authored
fix(File): decode Files properly (#427)
also this makes File compatible with Query
1 parent e134ff4 commit 137be69

File tree

4 files changed

+47
-20
lines changed

4 files changed

+47
-20
lines changed

src/av.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,9 @@ AV._decode = function(value, key) {
331331
}
332332
if (value.__type === 'File') {
333333
var file = new AV.File(value.name);
334-
file.attributes.metaData = value.metaData || {};
335-
file.attributes.url = value.url;
336-
file.id = value.objectId;
334+
const v = _.clone(value);
335+
delete v.__type;
336+
file._finishFetch(v);
337337
return file;
338338
}
339339
return _.mapObject(value, function(v, k) {

src/file.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -478,23 +478,25 @@ module.exports = function(AV) {
478478
var options = null;
479479

480480
var request = AVRequest('files', null, this.id, 'GET', options);
481-
return request.then((response) => {
482-
var value = AV.Object.prototype.parse(response);
483-
value.attributes = {
484-
name: value.name,
485-
url: value.url,
486-
mime_type: value.mime_type,
487-
};
488-
value.attributes.metaData = value.metaData || {};
489-
// clean
490-
delete value.objectId;
491-
delete value.metaData;
492-
delete value.url;
493-
delete value.name;
494-
delete value.mime_type;
495-
_.extend(this, value);
496-
return this;
497-
});
481+
return request.then(this._finishFetch.bind(this));
482+
},
483+
_finishFetch: function(response) {
484+
var value = AV.Object.prototype.parse(response);
485+
value.attributes = {
486+
name: value.name,
487+
url: value.url,
488+
mime_type: value.mime_type,
489+
};
490+
value.attributes.metaData = value.metaData || {};
491+
value.id = value.objectId;
492+
// clean
493+
delete value.objectId;
494+
delete value.metaData;
495+
delete value.url;
496+
delete value.name;
497+
delete value.mime_type;
498+
_.extend(this, value);
499+
return this;
498500
}
499501
};
500502
};

test/av.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,17 @@ describe('AV utils', () => {
3333
AV._decode(false).should.be.exactly(false);
3434
AV._decode('false').should.be.exactly('false');
3535
});
36+
37+
it('should decode File', () => {
38+
const fileId = '1111111';
39+
const json = { "mime_type": "image/png", "updatedAt": "2016-12-30T06:55:43.561Z", "key": "d7aaab5c477b289980fc.png", "name": "lc.png", "objectId": fileId, "createdAt": "2016-12-30T06:55:43.561Z", "__type": "File", "url": "", "bucket": "rYAutyUJ" };
40+
const file = AV._decode(json);
41+
expect(file).to.be.a(AV.File);
42+
expect(file.id).to.be(fileId);
43+
expect(file.name()).to.be('lc.png');
44+
expect(file.get('mime_type')).to.be('image/png');
45+
expect(typeof file.url()).to.be('string');
46+
expect(file.createdAt).to.be.a(Date);
47+
});
3648
});
3749
});

test/query.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ describe('Queries', function () {
4444

4545
});
4646

47+
it('#File Query', () => {
48+
const fileId = '52f9dd5ae4b019816c865985';
49+
query = new AV.Query(AV.File);
50+
query.equalTo('objectId', fileId);
51+
return query.find().then(([file]) => {
52+
expect(file).to.be.a(AV.File);
53+
expect(file.id).to.be(fileId);
54+
expect(file.name()).to.be('myfile.txt');
55+
expect(file.get('mime_type')).to.be('text/plain');
56+
expect(typeof file.url()).to.be('string');
57+
});
58+
});
59+
4760
describe('#cloudQuery', function () {
4861
it('should return results.', function () {
4962
return AV.Query.doCloudQuery('select * from GameScore').then(function (result) {

0 commit comments

Comments
 (0)