Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Mirador Video
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
IIIF
Mirador
Mirador Video
Commits
709dae6f
Commit
709dae6f
authored
Feb 24, 2021
by
Mark A. Matney, Jr
Browse files
Options
Downloads
Patches
Plain Diff
Fix failing tests
Also simplify and DRY up ThumbnailFactory helper methods.
parent
80e252b8
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/lib/ThumbnailFactory.js
+81
-72
81 additions, 72 deletions
src/lib/ThumbnailFactory.js
with
81 additions
and
72 deletions
src/lib/ThumbnailFactory.js
+
81
−
72
View file @
709dae6f
...
...
@@ -66,6 +66,16 @@ class ThumbnailFactory {
return
{
height
:
resource
.
getProperty
(
'
height
'
),
url
:
resource
.
id
,
width
:
resource
.
getProperty
(
'
width
'
)
};
}
/**
* Selects the image resource that is representative of the given canvas.
* @param {Object} canvas A Manifesto Canvas
* @return {Object} A Manifesto Image Resource
*/
static
getPreferredImage
(
canvas
)
{
const
miradorCanvas
=
new
MiradorCanvas
(
canvas
);
return
miradorCanvas
.
iiifImageResources
[
0
]
||
miradorCanvas
.
imageResource
;
}
/**
* Chooses the best available image size based on a target area (w x h) value.
* @param {Object} service A IIIF Image API service that has a `sizes` array
...
...
@@ -114,8 +124,9 @@ class ThumbnailFactory {
}
/**
* Creates a canonical image request for a thumb
* @param {Number} height
* Determines the appropriate thumbnail to use to represent an Image Resource.
* @param {Object} resource The Image Resource from which to derive a thumbnail
* @return {Object} The thumbnail URL and any spatial dimensions that can be determined
*/
iiifThumbnailUrl
(
resource
)
{
let
size
;
...
...
@@ -131,27 +142,26 @@ class ThumbnailFactory {
const
service
=
iiifImageService
(
resource
);
if
(
!
service
)
return
undefined
;
if
(
!
service
)
return
ThumbnailFactory
.
staticImageUrl
(
resource
)
;
const
aspectRatio
=
resource
.
getWidth
()
&&
resource
.
getHeight
()
&&
(
resource
.
getWidth
()
/
resource
.
getHeight
());
// just bail to a static image, even though sizes might provide something better
if
(
isLevel0ImageProfile
(
service
))
{
const
target
=
(
requestedMaxWidth
&&
requestedMaxHeight
)
?
requestedMaxWidth
*
requestedMaxHeight
:
maxHeight
*
maxWidth
;
const
closestSize
=
ThumbnailFactory
.
selectBestImageSize
(
service
,
target
);
/** Bail if the best available size is the full size.. maybe we'll get lucky with the @id */
if
(
!
closestSize
&&
!
service
.
getProperty
(
'
height
'
)
&&
!
service
.
getProperty
(
'
width
'
))
{
return
ThumbnailFactory
.
staticImageUrl
(
resource
);
}
if
(
closestSize
)
{
// Embedded service advertises an appropriate size
width
=
closestSize
.
width
;
height
=
closestSize
.
height
;
size
=
`
${
width
}
,
${
height
}
`
;
}
else
if
(
isLevel0ImageProfile
(
service
))
{
/** Bail if the best available size is the full size.. maybe we'll get lucky with the @id */
if
(
!
service
.
getProperty
(
'
height
'
)
&&
!
service
.
getProperty
(
'
width
'
))
{
return
ThumbnailFactory
.
staticImageUrl
(
resource
);
}
}
else
if
(
requestedMaxHeight
&&
requestedMaxWidth
)
{
// IIIF level 2, no problem.
if
(
isLevel2ImageProfile
(
service
))
{
...
...
@@ -195,77 +205,76 @@ class ThumbnailFactory {
};
}
/** */
getThumbnail
(
resource
,
{
requireIiif
,
quirksMode
})
{
if
(
!
resource
)
return
undefined
;
const
thumb
=
resource
.
getThumbnail
();
if
(
thumb
&&
iiifImageService
(
thumb
))
return
this
.
iiifThumbnailUrl
(
thumb
);
if
(
requireIiif
)
return
undefined
;
if
(
thumb
&&
typeof
thumb
.
__jsonld
!==
'
string
'
)
return
ThumbnailFactory
.
staticImageUrl
(
thumb
);
if
(
!
quirksMode
)
return
undefined
;
return
(
thumb
&&
typeof
thumb
.
__jsonld
===
'
string
'
)
?
{
url
:
thumb
.
__jsonld
}
:
undefined
;
/**
* Determines the content resource from which to derive a thumbnail to represent a given resource.
* This method is recursive.
* @param {Object} resource A IIIF resource to derive a thumbnail from
* @return {Object|undefined} The Image Resource to derive a thumbnail from, or undefined
* if no appropriate resource exists
*/
getSourceContentResource
(
resource
)
{
const
thumbnail
=
resource
.
getThumbnail
();
// Any resource type may have a thumbnail
if
(
thumbnail
)
{
if
(
typeof
thumbnail
.
__jsonld
===
'
string
'
)
return
thumbnail
.
__jsonld
;
// Prefer an image's ImageService over its image's thumbnail
// Note that Collection, Manifest, and Canvas don't have `getType()`
if
(
!
resource
.
isCollection
()
&&
!
resource
.
isManifest
()
&&
!
resource
.
isCanvas
())
{
if
(
resource
.
getType
()
===
'
image
'
&&
iiifImageService
(
resource
)
&&
!
iiifImageService
(
thumbnail
))
{
return
resource
;
}
/** */
getResourceThumbnail
(
resource
)
{
const
thumb
=
this
.
getThumbnail
(
resource
,
{
requireIiif
:
true
});
if
(
thumb
)
return
thumb
;
if
(
iiifImageService
(
resource
))
return
this
.
iiifThumbnailUrl
(
resource
);
if
([
'
image
'
,
'
dctypes:Image
'
].
includes
(
resource
.
getProperty
(
'
type
'
)))
return
ThumbnailFactory
.
staticImageUrl
(
resource
);
return
this
.
getThumbnail
(
resource
,
{
quirksMode
:
true
,
requireIiif
:
false
});
}
/** */
getIIIFThumbnail
(
canvas
)
{
const
thumb
=
this
.
getThumbnail
(
canvas
,
{
requireIiif
:
true
});
if
(
thumb
)
return
thumb
;
const
miradorCanvas
=
new
MiradorCanvas
(
canvas
);
return
thumbnail
;
}
const
preferredCanvasResource
=
miradorCanvas
.
iiifImageResources
[
0
]
||
canvas
.
imageResource
;
if
(
resource
.
isCollection
())
{
const
firstManifest
=
resource
.
getManifests
()[
0
];
if
(
firstManifest
)
return
this
.
getSourceContentResource
(
firstManifest
);
return
(
preferredCanvasResource
&&
this
.
getResourceThumbnail
(
preferredCanvasResource
))
||
this
.
getThumbnail
(
canvas
,
{
quirksMode
:
true
,
requireIiif
:
false
});
return
undefined
;
}
/** */
getManifestThumbnail
(
manifest
)
{
const
thumb
=
this
.
getThumbnail
(
manifest
,
{
requireIiif
:
true
});
if
(
thumb
)
return
thumb
;
const
miradorManifest
=
new
MiradorManifest
(
manifest
);
if
(
resource
.
isManifest
())
{
const
miradorManifest
=
new
MiradorManifest
(
resource
);
const
canvas
=
miradorManifest
.
startCanvas
||
miradorManifest
.
canvasAt
(
0
);
if
(
canvas
)
return
this
.
getSourceContentResource
(
canvas
);
return
(
canvas
&&
this
.
getIIIFThumbnail
(
canvas
))
||
this
.
getThumbnail
(
manifest
,
{
quirksMode
:
true
,
requireIiif
:
false
});
return
undefined
;
}
/** */
getCollectionThumbnail
(
collection
)
{
const
thumb
=
this
.
getThumbnail
(
collection
,
{
requireIiif
:
true
});
if
(
thumb
)
return
thumb
;
if
(
resource
.
isCanvas
())
{
const
image
=
ThumbnailFactory
.
getPreferredImage
(
resource
);
if
(
image
)
return
this
.
getSourceContentResource
(
image
);
const
firstManifest
=
this
.
resource
.
getManifests
()[
0
];
return
undefined
;
}
return
(
firstManifest
&&
this
.
getManifestThumbnail
(
firstManifest
))
||
this
.
getThumbnail
(
collection
,
{
quirksMode
:
true
,
requireIiif
:
false
})
;
if
(
resource
.
getType
()
===
'
image
'
)
{
return
resource
;
}
/** */
return
undefined
;
}
/**
* Gets a thumbnail representing the resource.
* @return {Object|undefined} A thumbnail representing the resource, or undefined if none could
* be determined
*/
get
()
{
if
(
!
this
.
resource
)
return
undefined
;
if
(
this
.
resource
.
isCanvas
())
return
this
.
getIIIFThumbnail
(
this
.
resource
);
if
(
this
.
resource
.
isManifest
())
return
this
.
getManifestThumbnail
(
this
.
resource
);
if
(
this
.
resource
.
isCollection
())
return
this
.
getCollectionThumbnail
(
this
.
resource
);
return
this
.
getResourceThumbnail
(
this
.
resource
,
{
requireIiif
:
true
});
// Determine which content resource we should use to derive a thumbnail
const
sourceContentResource
=
this
.
getSourceContentResource
(
this
.
resource
);
if
(
!
sourceContentResource
)
return
undefined
;
// Special treatment for external resources
if
(
typeof
sourceContentResource
===
'
string
'
)
return
{
url
:
sourceContentResource
};
return
this
.
iiifThumbnailUrl
(
sourceContentResource
);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment