Skip to content

Commit

Permalink
feat: negotiate local files in DMAssetResource SDK-40
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-scherzinger committed Apr 6, 2018
1 parent 8ac7b5d commit d7b8381
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
22 changes: 18 additions & 4 deletions src/resources/publicAPI/DMAssetResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ class DMAssetResource extends Resource {
getFileVariant(size?: number, thumb: boolean = false): Promise<string> {
return Promise.resolve()
.then(() => {
if (!size || !thumb) {
return this.file.url;
}

let file;
if (thumb) {
file = this.thumbnails.find(t => t.dimension === size);
} else {
file = this.fileVariants.find(v => Math.max(v.resolution.width, v.resolution.height) === size);
}
if (file) {
return file.url;
}

const request = this.newRequest();
if (thumb) {
request.follow('ec:dm-asset/thumbnail');
Expand All @@ -154,10 +168,10 @@ class DMAssetResource extends Resource {
templateParams.size = size;
}
request.withTemplateParameters(templateParams);
return get(this[environmentSymbol], request);
})
.then(([res]) => {
return res.url;
return get(this[environmentSymbol], request)
.then(([res]) => {
return res.url;
});
});
}

Expand Down
33 changes: 16 additions & 17 deletions test/publicAPI/DMAssetResource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ describe('DMAsset ListResource', () => {
return resolve(JSON.parse(res));
});
})
.then((json) => {
listJson = json;
});
.then((json) => {
listJson = json;
});
});
beforeEach(() => {
list = new DMAssetList(listJson);
Expand Down Expand Up @@ -60,9 +60,9 @@ describe('DMAsset Resource', () => {
return resolve(JSON.parse(res));
});
})
.then((json) => {
resourceJson = json;
});
.then((json) => {
resourceJson = json;
});
});
beforeEach(() => {
resource = new DMAssetResource(resourceJson);
Expand All @@ -81,20 +81,19 @@ describe('DMAsset Resource', () => {
});
it('should get original file', () => {
resource.getOriginalFile().should.deep.equal({
"url": "https://cdn1.entrecode.de/beefbeef/test1/7mGEhlUXvdxuoCf0vQWtLNQW.jpg",
"size": 3644378,
"resolution": {
"width": 2736,
"height": 4864
url: 'https://cdn1.entrecode.de/beefbeef/test1/7mGEhlUXvdxuoCf0vQWtLNQW.jpg',
size: 3644378,
resolution: {
width: 2736,
height: 4864,
},
});
});
it('should get image variant', () => {
const stub = sinon.stub(helper, 'get');
stub.returns(resolver('dm-asset-bestfile.json'));

return resource.getImageUrl(500)
.should.eventually.equal('https://cdn1.buffalo.entrecode.de/ab5047fc/test1/7mGEhlUXvdxuoCf0vQWtLNQW.jpg')
.should.eventually.equal('https://cdn1.entrecode.de/beefbeef/test1/7mGEhlUXvdxuoCf0vQWtLNQW.jpg')
.notify(() => {
stub.restore();
});
Expand All @@ -104,10 +103,10 @@ describe('DMAsset Resource', () => {
stub.returns(resolver('dm-asset-bestfile.json'));

return resource.getImageThumbUrl(50)
.should.eventually.equal('https://cdn1.buffalo.entrecode.de/ab5047fc/test1/7mGEhlUXvdxuoCf0vQWtLNQW.jpg')
.notify(() => {
stub.restore();
});
.should.eventually.equal('https://cdn1.buffalo.entrecode.de/ab5047fc/test1/7mGEhlUXvdxuoCf0vQWtLNQW.jpg')
.notify(() => {
stub.restore();
});
});

const dateGetter = ['created', 'modified'];
Expand Down

0 comments on commit d7b8381

Please sign in to comment.