Skip to content

Commit

Permalink
Merge pull request #59 from internetarchive/issue-52
Browse files Browse the repository at this point in the history
Handling object which has multiple images but no image stack
  • Loading branch information
benwbrum authored May 16, 2024
2 parents 240826b + f98c322 commit e3ef404
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
49 changes: 48 additions & 1 deletion iiify/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ def getids(q, limit=1000, cursor=''):
}, allow_redirects=True, timeout=None)
return r.json()

def checkMultiItem(metadata):
# Maybe add call to book stack to see if that works first

# Count the number of each original file
file_types = {}
for file in metadata['files']:
if file['source'] == "original":
if file['format'] not in file_types:
file_types[file['format']] = 0

file_types[file['format']] += 1
#print (file_types)

# If there is multiple files of the same type then return the first format
# Will have to see if there are objects with multiple images and formats
for format in file_types:
if file_types[format] > 1 and format.lower() in valid_filetypes:
return (True, format)

return (False, None)


def to_mimetype(format):
formats = {
"VBR MP3": "audio/mp3",
Expand Down Expand Up @@ -363,6 +385,7 @@ def addMetadata(item, identifier, metadata, collection=False):
item.metadata = manifest_metadata



def create_manifest3(identifier, domain=None, page=None):
# Get item metadata
metadata = requests.get('%s/metadata/%s' % (ARCHIVE, identifier)).json()
Expand Down Expand Up @@ -448,7 +471,31 @@ def create_manifest3(identifier, domain=None, page=None):


elif mediatype == 'image':
singleImage(metadata, identifier, manifest, uri)
(multiFile, format) = checkMultiItem(metadata)
print (f"Checking multiFile {multiFile} {format}")
if multiFile:
# Create multi file manifest
pageCount = 0
for file in metadata['files']:
if file['source'] == "original" and file['format'] == format:
imgId = f"{identifier}/{file['name']}".replace('/','%2f')
imgURL = f"{IMG_SRV}/3/{imgId}"
pageCount += 1

try:
manifest.make_canvas_from_iiif(url=imgURL,
id=f"{URI_PRIFIX}/{identifier}${pageCount}/canvas",
label=f"{file['name']}",
anno_page_id=f"{uri}/annotationPage/1",
anno_id=f"{uri}/annotation/1")
except requests.exceptions.HTTPError as error:
print (f'Failed to get {imgURL}')
manifest.make_canvas(label=f"Failed to load {file['name']} from Image Server",
summary=f"Got {error}",
id=f"{URI_PRIFIX}/{identifier}/canvas",
height=1800, width=1200)
else:
singleImage(metadata, identifier, manifest, uri)
elif mediatype == 'audio' or mediatype == 'etree':
# sort the files into originals and derivatives, splitting the derivatives into buckets based on the original
originals = []
Expand Down
16 changes: 16 additions & 0 deletions tests/test_manifests.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ def test_metadata_array(self):
manifest = resp.json
self.assertTrue(len(manifest['summary']['none']) > 1, f"Expected multiple summary values, but got {manifest['summary']['none']}")

def test_multi_file_image(self):
resp = self.test_app.get("/iiif/3/arkivkopia.se-lms-G70-48.3/manifest.json")
self.assertEqual(resp.status_code, 200)
manifest = resp.json
self.assertEqual(len(manifest['items']),3, f"Expected three canvases, but got {len(manifest['items'])}")

firstCanvasId = manifest['items'][0]['id']
for i in range(1, len(manifest['items'])):
self.assertNotEqual(manifest['items'][i]['id'], firstCanvasId, 'Canvas Ids need to be unique')

def test_multi_file(self):
resp = self.test_app.get("/iiif/3/st-anthony-relics-01/manifest.json")
self.assertEqual(resp.status_code, 200)
manifest = resp.json
self.assertEqual(len(manifest['items']),6, f"Expected five canvases, but got {len(manifest['items'])}")


''' to test:
kaled_jalil (no derivatives)
Expand Down

0 comments on commit e3ef404

Please sign in to comment.