Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions iiify/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,19 +563,19 @@ def addRendering(manifest, identifier, files):
"format": rendering['format']
})

def addThumbnails(manifest, identifier, files):
def addThumbnails(manifest, identifier, files, mediatype):
"""Creates thumbnails based on files.

If the file appears to be a thumbnail (by format or name) attempt to create a IIIF thumbnail via Cantaloupe.
If that fails or isn't possible, fall back to adding a static thumbnail.
"""
thumbnail_files = []
ia_thumb_files = []

for file in files:
name = file.get("name", "")
file_format = file.get("format", "")

if name == "__ia_thumb.jpg":
ia_thumb_files.append(file)
elif file_format in {"Thumbnail", "JPEG Thumb"}:
Expand All @@ -588,19 +588,29 @@ def addThumbnails(manifest, identifier, files):

elif ia_thumb_files:
files_to_process = ia_thumb_files

av_types = ("audio", "movies", "etree")
for file in files_to_process:
name = file.get("name", "")
encoded_name = quote(name.replace('/', '%2f'))
# Forward solidus before thumbnail uri must always be %2f
iiif_url = f"{IMG_SRV}/2/{identifier.strip()}%2f{encoded_name}"
try:
manifest.create_thumbnail_from_iiif(iiif_url)
except requests.HTTPError:
print(f"Failed to generate thumbnail from Cantaloupe: {iiif_url}")
if mediatype in av_types:
mimetype = "image/png" if name.endswith(".png") else "image/jpeg"
static_url = f"{ARCHIVE}/download/{quote(identifier)}/{quote(name)}"
manifest.add_thumbnail(static_url, format=mimetype)
manifest.add_thumbnail(
static_url,
format=mimetype,
**({"width": file["width"]} if "width" in file else {"width": 192}),
**({"height": file["height"]} if "height" in file else {"height": 108})
)
else:
encoded_name = quote(name.replace('/', '%2f'))
# Forward solidus before thumbnail uri must always be %2f
iiif_url = f"{IMG_SRV}/2/{identifier.strip()}%2f{encoded_name}"
try:
manifest.create_thumbnail_from_iiif(iiif_url)
except requests.HTTPError:
print(f"Failed to generate thumbnail from Cantaloupe: {iiif_url}")
mimetype = "image/png" if name.endswith(".png") else "image/jpeg"
static_url = f"{ARCHIVE}/download/{quote(identifier)}/{quote(name)}"
manifest.add_thumbnail(static_url, format=mimetype)
return

def addPartOfCollection(resource, collections, domain=None):
Expand Down Expand Up @@ -673,7 +683,7 @@ def create_manifest3(identifier, domain=None, page=None):
addMetadata(manifest, identifier, metadata['metadata'])
addSeeAlso(manifest, identifier, metadata['files'])
addRendering(manifest, identifier, metadata['files'])
addThumbnails(manifest, identifier, metadata['files'])
addThumbnails(manifest, identifier, metadata['files'], mediatype)
addPartOfCollection(manifest, metadata.get('metadata').get('collection', []), domain)

if mediatype == 'texts':
Expand Down
2 changes: 1 addition & 1 deletion tests/test_manifests.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_encoded_thumb(self):
self.assertEqual(resp.status_code, 200)
manifest = resp.json
self.assertEqual(len(manifest['thumbnail']),16, f"Expected 16 thumbnails, but got {len(manifest['thumbnail'])}")
self.assertEqual(manifest['thumbnail'][0]['id'],"https://iiif.archive.org/image/iiif/2/steamboat-willie-16mm-film-scan-4k-lossless%2fSteamboat%20Willie%20%5B16mm%20Film%20Scan%5D_ProRes%20%283400x2550%29.01_thumb.jpg/full/192,/0/default.jpg", f"Expected URL to be encoded")
self.assertEqual(manifest['thumbnail'][0]['id'],"https://archive.org/download/steamboat-willie-16mm-film-scan-4k-lossless/Steamboat%20Willie%20%5B16mm%20Film%20Scan%5D_ProRes%20%283400x2550%29.01_thumb.jpg", f"Expected URL to be encoded")

self.assertEqual(len(manifest['items']),1, f"Expected 1 canvas, but got {len(manifest['items'])}")

Expand Down