Skip to content

Commit

Permalink
Merge pull request #1207 from tomcrane/check-compliance-for-thumbnail
Browse files Browse the repository at this point in the history
If canvas.thumbnail is level 0, don't use
  • Loading branch information
rsinghal authored Jan 4, 2017
2 parents a06570c + fd07b24 commit 128b479
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 12 deletions.
21 changes: 15 additions & 6 deletions js/src/manifests/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
},
getThumbnailForCanvas : function(canvas, width) {
var version = "1.1",
compliance = -1,
service,
thumbnailUrl;

Expand All @@ -93,12 +94,20 @@
if (typeof(canvas.thumbnail) == 'string') {
thumbnailUrl = canvas.thumbnail;
} else if (canvas.thumbnail.hasOwnProperty('service')) {
// Get the IIIF Image API via the @context
service = canvas.thumbnail.service;
if (service.hasOwnProperty('@context')) {
version = $.Iiif.getVersionFromContext(service['@context']);
}
thumbnailUrl = $.Iiif.makeUriWithWidth(service['@id'], width, version);
service = canvas.thumbnail.service;
if(service.hasOwnProperty('profile')) {
compliance = $.Iiif.getComplianceLevelFromProfile(service.profile);
}
if(compliance === 0){
// don't change existing behaviour unless compliance is explicitly 0
thumbnailUrl = canvas.thumbnail['@id'];
} else {
// Get the IIIF Image API via the @context
if (service.hasOwnProperty('@context')) {
version = $.Iiif.getVersionFromContext(service['@context']);
}
thumbnailUrl = $.Iiif.makeUriWithWidth(service['@id'], width, version);
}
} else {
thumbnailUrl = canvas.thumbnail['@id'];
}
Expand Down
26 changes: 26 additions & 0 deletions js/src/utils/iiif.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@
}
},

getComplianceLevelFromProfile: function(profile) {
// what to return if we can't determine profile? 0 is not a good idea
// would be better to have $.Iiif.supports(profile, feature) but that needs a lot more!
var compliance = -1;
var complianceString = null;
if(profile) {
if(typeof(profile) === 'string'){
complianceString = profile;
} else if (typeof(profile) === 'object'){
complianceString = profile[0];
}
switch(complianceString){
case "http://iiif.io/api/image/2/level0.json":
compliance = 0;
break;
case "http://iiif.io/api/image/2/level1.json":
compliance = 1;
break;
case "http://iiif.io/api/image/2/level2.json":
compliance = 2;
break;
}
}
return compliance;
},

makeUriWithWidth: function(uri, width, version) {
uri = uri.replace(/\/$/, '');
if (version[0] == '1') {
Expand Down
18 changes: 13 additions & 5 deletions js/src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

$.getThumbnailForCanvas = function(canvas, width) {
var version = "1.1",
compliance = -1,
service,
thumbnailUrl;

Expand All @@ -34,13 +35,20 @@
if (typeof(canvas.thumbnail) == 'string') {
thumbnailUrl = canvas.thumbnail;
} else if (canvas.thumbnail.hasOwnProperty('service')) {
// Get the IIIF Image API via the @context
service = canvas.thumbnail.service;
if (service.hasOwnProperty('@context')) {
version = $.Iiif.getVersionFromContext(service['@context']);
console.log('version');
if(service.hasOwnProperty('profile')) {
compliance = $.Iiif.getComplianceLevelFromProfile(service.profile);
}
if(compliance === 0){
// don't change existing behaviour unless compliance is explicitly 0
thumbnailUrl = canvas.thumbnail['@id'];
} else {
// Get the IIIF Image API via the @context
if (service.hasOwnProperty('@context')) {
version = $.Iiif.getVersionFromContext(service['@context']);
}
thumbnailUrl = $.Iiif.makeUriWithWidth(service['@id'], width, version);
}
thumbnailUrl = $.Iiif.makeUriWithWidth(service['@id'], width, version);
} else {
thumbnailUrl = canvas.thumbnail['@id'];
}
Expand Down
20 changes: 19 additions & 1 deletion spec/manifests/manifest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,25 @@ describe('Manifest', function() {
expect(thumbnail).toEqual(thumbnailUrl);
});

it('when canvas.thumbnail has a service', function () {

it('when canvas.thumbnail has a level 0 service', function () {
var thumbnailUrl = 'http://www.example.org/iiif/book1/thumbnail/p1.jpg';
var canvas = {
thumbnail: {
'@id': thumbnailUrl,
service: {
'@id': 'http://example.org/images/book1-page1',
profile: 'http://iiif.io/api/image/2/level0.json'
}
}
};
var manifestInstance = new Mirador.Manifest(null, null, {});
var thumbnail = manifestInstance.getThumbnailForCanvas(canvas);
expect(thumbnail).toEqual(thumbnailUrl);
});


it('when canvas.thumbnail has a level 1 or level 2 service', function () {
var canvas = {
thumbnail: {
'@id': 'http://example.org/images/book1-page1/full/80,100/0/default.jpg',
Expand Down
29 changes: 29 additions & 0 deletions spec/utils/iiif.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,35 @@ describe('Iiif', function () {
});
});

describe('getComplianceLevelFromProfile', function () {
it('should identify 0 from string', function() {
var profile = 'http://iiif.io/api/image/2/level0.json';
expect(Mirador.Iiif.getComplianceLevelFromProfile(profile)).toEqual(0);
});
it('should identify 2 from array', function() {
var profile = [
"http://iiif.io/api/image/2/level2.json",
{
"formats" : [ "gif", "pdf" ],
"qualities" : [ "color", "gray" ],
"supports" : [
"canonicalLinkHeader", "rotationArbitrary", "profileLinkHeader", "http://example.com/feature/"
]
}
];
expect(Mirador.Iiif.getComplianceLevelFromProfile(profile)).toEqual(2);
});
it('should return -1 for empty profile', function() {
var profile = null;
expect(Mirador.Iiif.getComplianceLevelFromProfile(profile)).toEqual(-1);
});
it('should return -1 for unrecognised profile', function() {
var profile = "http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level0";
expect(Mirador.Iiif.getComplianceLevelFromProfile(profile)).toEqual(-1);
});
});


describe('makeUriWithWidth', function () {
it('should return native.jpg URL for IIIF v1.x', function() {
expect(Mirador.Iiif.makeUriWithWidth('http://images.waahoo.com/iiif/MYTEST', 512, '1.1')).toEqual('http://images.waahoo.com/iiif/MYTEST/full/512,/0/native.jpg');
Expand Down

0 comments on commit 128b479

Please sign in to comment.