-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get rid of manifesto's canonical URI creation method, improve API com…
…pliance TODOs - tests for added IIIF image functions - CanvasDownloadLinks with different API versions
- Loading branch information
Showing
4 changed files
with
183 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
function getArrayFromInfoResponse(imageInfo, key) { | ||
const value = imageInfo && imageInfo[key]; | ||
let valueArray; | ||
if (Array.isArray(value)) { | ||
valueArray = value; | ||
} else if (typeof value === 'string') { | ||
valueArray = [value]; | ||
} | ||
return valueArray; | ||
} | ||
|
||
export function getComplianceLevel(imageInfo) { | ||
const profile = getArrayFromInfoResponse(imageInfo, 'profile'); | ||
switch (profile && profile[0]) { | ||
case 'http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level0': | ||
case 'http://iiif.io/api/image/1/level0.json': | ||
case 'http://iiif.io/api/image/2/level0.json': | ||
case 'level0': | ||
return 0; | ||
case 'http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level1': | ||
case 'http://iiif.io/api/image/1/level1.json': | ||
case 'http://iiif.io/api/image/2/level1.json': | ||
case 'level1': | ||
return 1; | ||
case 'http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level2': | ||
case 'http://iiif.io/api/image/1/level2.json': | ||
case 'http://iiif.io/api/image/2/level2.json': | ||
case 'level2': | ||
return 2; | ||
default: | ||
return undefined; | ||
} | ||
} | ||
|
||
export function getImageApiVersion(imageInfo) { | ||
const context = getArrayFromInfoResponse(imageInfo, '@context'); | ||
if (!context) { | ||
return undefined; | ||
} | ||
if (context.indexOf('http://iiif.io/api/image/3/context.json') > -1) { | ||
return 3; | ||
} | ||
if (context.indexOf('http://iiif.io/api/image/2/context.json') > -1) { | ||
return 2; | ||
} | ||
if (context.indexOf('http://iiif.io/api/image/1/context.json') > -1 | ||
|| context.indexOf('http://library.stanford.edu/iiif/image-api/1.1/context.json') > -1) { | ||
return 1; | ||
} | ||
return undefined; | ||
} | ||
|
||
function supportsAdditonalFeature(imageInfo, feature) { | ||
const version = getImageApiVersion(imageInfo); | ||
switch (version) { | ||
case 2: { | ||
const profile = getArrayFromInfoResponse(imageInfo, 'profile'); | ||
return profile | ||
&& profile.length > 1 | ||
&& profile[1].supports | ||
&& profile[1].supports.indexOf(feature) > -1; | ||
} | ||
case 3: | ||
return imageInfo.extraFeatures && imageInfo.extraFeatures.indexOf(feature) > -1; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
function supportsArbitrarySizeInCanonicalForm(imageInfo) { | ||
const level = getComplianceLevel(imageInfo); | ||
const version = getImageApiVersion(imageInfo); | ||
// everything but undefined or 0 is fine | ||
if (!!level | ||
|| (version < 3 && supportsAdditonalFeature(imageInfo, 'sizeByW')) | ||
|| (version === 3 && supportsAdditonalFeature(imageInfo, 'sizeByWh'))) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
export function calculateHeightForWidth(imageInfo, width) { | ||
if (!imageInfo) { | ||
return undefined; | ||
} | ||
if (imageInfo.width === width) { | ||
return imageInfo.width; | ||
} | ||
return Math.floor((imageInfo.height * width) / imageInfo.width); | ||
} | ||
|
||
export function createCanonicalImageUrl(imageInfo, region, width, height) { | ||
const version = getImageApiVersion(imageInfo); | ||
let baseUri = imageInfo['@id'] || imageInfo.id; | ||
baseUri = baseUri && baseUri.replace(/\/$/, ''); | ||
let size = `${width},${version === 3 ? height : ''}`; | ||
const quality = version === 1 ? 'native' : 'default'; | ||
if (version < 3 && imageInfo.width === width && imageInfo.height === height) { | ||
size = 'full'; | ||
} | ||
if (!supportsArbitrarySizeInCanonicalForm(imageInfo)) { | ||
// TODO check if requested size is available for level 0, return undefined otherwise | ||
} | ||
// TODO check if size exceeds maximum width / height / area | ||
return `${baseUri}/${region}/${size}/0/${quality}.jpg`; | ||
} |