This repository has been archived by the owner on Jan 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
thumbnail.ts
195 lines (173 loc) · 6.3 KB
/
thumbnail.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import {
AnnotationNormalized,
AnnotationPageNormalized,
CanvasNormalized,
ChoiceBody,
CollectionItemSchemas,
CollectionNormalized,
ContentResource,
DescriptiveNormalized,
ManifestNormalized,
Reference,
} from '@iiif/presentation-3';
import {
FixedSizeImage,
FixedSizeImageService,
getFixedSizeFromImage,
ImageCandidate,
ImageCandidateRequest,
ImageServiceLoader,
UnknownSizeImage,
VariableSizeImage,
} from '@atlas-viewer/iiif-image-api';
import { compatVault, CompatVault } from './compat';
export function createThumbnailHelper(
vault: CompatVault = compatVault,
dependencies: { imageServiceLoader?: ImageServiceLoader } = {}
) {
const imageServiceLoader = dependencies.imageServiceLoader || new ImageServiceLoader();
async function getBestThumbnailAtSize(
input:
| string
| Reference<CollectionItemSchemas>
| Reference<'Collection'>
| Reference<'Manifest'>
| Reference<'Canvas'>
| Reference<'Annotation'>
| Reference<'AnnotationPage'>
| Reference<'ContentResource'>
| CollectionNormalized
| ManifestNormalized
| CanvasNormalized
| AnnotationNormalized
| AnnotationPageNormalized
| ContentResource
| undefined,
request: ImageCandidateRequest,
dereference?: boolean,
candidates: Array<ImageCandidate> = [],
dimensions?: { width: number; height: number }
): Promise<{
best: null | undefined | FixedSizeImage | FixedSizeImageService | VariableSizeImage | UnknownSizeImage;
fallback: Array<ImageCandidate>;
log: string[];
}> {
const thumbnailNotFound = () =>
imageServiceLoader.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);
}
if (typeof input === 'string') {
const fixed = getFixedSizeFromImage(input as any);
if (fixed) {
candidates.push(fixed);
}
return await imageServiceLoader.getThumbnailFromResource(undefined as any, request, dereference, candidates);
}
// Run through from ref, just in case.
const fullInput:
| string
| ManifestNormalized
| CollectionNormalized
| CanvasNormalized
| AnnotationNormalized
| AnnotationPageNormalized
| ContentResource
| undefined = vault.get(input as any, { skipSelfReturn: false }) as any;
if (typeof fullInput === 'string') {
return { best: getFixedSizeFromImage(fullInput as any), fallback: [], log: [] };
}
if (!fullInput) {
return await thumbnailNotFound();
}
const parseThumbnail = async (resource: DescriptiveNormalized) => {
if (resource && resource.thumbnail && resource.thumbnail.length) {
const thumbnail = vault.get<ContentResource>(resource.thumbnail[0]);
const potentialThumbnails = await imageServiceLoader.getImageCandidates(thumbnail as any, dereference);
if (potentialThumbnails && potentialThumbnails.length) {
candidates.push(...potentialThumbnails);
}
}
};
await parseThumbnail(fullInput as any);
switch (fullInput.type) {
case 'Annotation': {
// Grab the body.
const contentResources = Array.isArray(fullInput.body) ? fullInput.body : [fullInput.body];
// @todo this could be configuration.
const firstContentResources = vault.get<ContentResource>(contentResources[0]);
if (dimensions && !(firstContentResources as any).width) {
(firstContentResources as any).width = dimensions.width;
(firstContentResources as any).height = dimensions.height;
}
return await imageServiceLoader.getThumbnailFromResource(
firstContentResources as any,
request,
dereference,
candidates
);
}
case 'Canvas': {
const canvas = fullInput as CanvasNormalized;
return getBestThumbnailAtSize(canvas.items[0], request, dereference, candidates, {
width: canvas.width,
height: canvas.height,
});
}
// Unsupported for now.
case 'AnnotationPage': {
const annotationPage = fullInput as AnnotationPageNormalized;
return getBestThumbnailAtSize(annotationPage.items[0], request, dereference, candidates, dimensions);
}
case 'Choice': {
const choice: ChoiceBody = fullInput as any;
if (!choice.items || choice.items[0]) {
return await thumbnailNotFound();
}
// @todo this could also be configuration, just choosing the first choice.
return getBestThumbnailAtSize(choice.items[0] as any, request, dereference, candidates, dimensions);
}
case 'Collection': {
// This one is tricky, as the manifests may not have been loaded. But we will give it a shot.
const collection = fullInput as CollectionNormalized;
const firstManifest = collection.items[0];
if (!firstManifest) {
return await thumbnailNotFound();
}
return getBestThumbnailAtSize(firstManifest, request, dereference, candidates, dimensions);
}
case 'Manifest': {
const manifest = fullInput as ManifestNormalized;
const firstCanvas = manifest.items[0];
if (!firstCanvas) {
return await thumbnailNotFound();
}
return getBestThumbnailAtSize(firstCanvas, request, dereference, candidates, dimensions);
}
case 'SpecificResource':
case 'Image':
case 'Dataset':
case 'Sound':
case 'Text':
case 'TextualBody':
case 'Video':
if (dimensions && !(fullInput as any).width) {
(fullInput as any).width = dimensions.width;
(fullInput as any).height = dimensions.height;
}
return imageServiceLoader.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.
case 'Range':
case 'AnnotationCollection':
case 'CanvasReference':
case 'ContentResource':
return await thumbnailNotFound();
}
return await thumbnailNotFound();
}
return {
getBestThumbnailAtSize,
};
}