Skip to content

Commit

Permalink
Better getThumbnail
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenwf committed Oct 2, 2023
1 parent dad48cf commit 8476bc8
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions src/thumbnail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,41 @@ import {
} from '@atlas-viewer/iiif-image-api';
import { compatVault, CompatVault } from './compat';

export const imageServiceLoader = new ImageServiceLoader();

let helper: ReturnType<typeof createThumbnailHelper> | null = null;
export function getThumbnail(
input:
| string
| Reference<CollectionItemSchemas>
| Reference<'Collection'>
| Reference<'Manifest'>
| Reference<'Canvas'>
| Reference<'Annotation'>
| Reference<'AnnotationPage'>
| Reference<'ContentResource'>
| CollectionNormalized
| ManifestNormalized
| CanvasNormalized
| AnnotationNormalized
| AnnotationPageNormalized
| ContentResource
| undefined,
{
vault = compatVault,
dereference = false,
...options
}: ImageCandidateRequest & { vault?: CompatVault; dereference?: boolean } = {}
) {
helper = helper || createThumbnailHelper(vault);
return helper.getBestThumbnailAtSize(input, options, dereference);
}

export function createThumbnailHelper(
vault: CompatVault = compatVault,
dependencies: { imageServiceLoader?: ImageServiceLoader } = {}
) {
const imageServiceLoader = dependencies.imageServiceLoader || new ImageServiceLoader();
const loader = dependencies.imageServiceLoader || imageServiceLoader;

async function getBestThumbnailAtSize(
input:
Expand Down Expand Up @@ -51,12 +81,11 @@ export function createThumbnailHelper(
fallback: Array<ImageCandidate>;
log: string[];
}> {
const thumbnailNotFound = () =>
imageServiceLoader.getThumbnailFromResource(undefined as any, request, dereference, candidates);
const thumbnailNotFound = () => loader.getThumbnailFromResource(undefined as any, request, dereference, candidates);

if (!input) {
// We might have candidates already to pick from.
return await imageServiceLoader.getThumbnailFromResource(undefined as any, request, dereference, candidates);
return await loader.getThumbnailFromResource(undefined as any, request, dereference, candidates);
}

if (typeof input === 'string') {
Expand All @@ -65,7 +94,7 @@ export function createThumbnailHelper(
candidates.push(fixed);
}

return await imageServiceLoader.getThumbnailFromResource(undefined as any, request, dereference, candidates);
return await loader.getThumbnailFromResource(undefined as any, request, dereference, candidates);
}

// Run through from ref, just in case.
Expand All @@ -91,7 +120,7 @@ export function createThumbnailHelper(
const parseThumbnail = async (resource: DescriptiveNormalized) => {
if (resource && resource.thumbnail && resource.thumbnail.length) {
const thumbnail = vault.get(resource.thumbnail[0]);
const potentialThumbnails = await imageServiceLoader.getImageCandidates(thumbnail as any, dereference);
const potentialThumbnails = await loader.getImageCandidates(thumbnail as any, dereference);
if (potentialThumbnails && potentialThumbnails.length) {
candidates.push(...potentialThumbnails);
}
Expand All @@ -111,12 +140,7 @@ export function createThumbnailHelper(
(firstContentResources as any).height = dimensions.height;
}

return await imageServiceLoader.getThumbnailFromResource(
firstContentResources as any,
request,
dereference,
candidates
);
return await loader.getThumbnailFromResource(firstContentResources as any, request, dereference, candidates);
}

case 'Canvas': {
Expand Down Expand Up @@ -173,7 +197,7 @@ export function createThumbnailHelper(
(fullInput as any).height = dimensions.height;
}

return imageServiceLoader.getThumbnailFromResource(fullInput as any, request, dereference, candidates);
return loader.getThumbnailFromResource(fullInput as any, request, dereference, candidates);

// Seems unlikely these would appear, but it would be an error..
// case 'Service': // @todo could do something with vault.
Expand Down

0 comments on commit 8476bc8

Please sign in to comment.