-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
351 lines (310 loc) · 14.7 KB
/
index.js
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import { promises as fsPromises } from 'fs';
import config from './config.js';
import path from 'path';
import { fetchMiniatureAll, fetchMicrograph, fetchAllMaXrf, fetchFileObject, downloadImage } from './directus.js';
import micrographBuildManifest from './micrograph.js';
async function main() {
const { fieldMap } = config;
//Download all miniature ovjects from their API listing
const miniatureAll = await fetchMiniatureAll();
//Download all MA XRF Scan objects from API listing
const maXrfAll = await fetchAllMaXrf();
const urlSafeRegex = /[^a-zA-Z0-9_. ]/g;
//For each miniature
for (let i = 0; i < miniatureAll.length; i++) {
const data = miniatureAll[i];
const miniatureId = data[fieldMap.publicPath].replace(urlSafeRegex, '-');
const basePath = config.basePath + miniatureId + "/";
const manifestId = basePath + "manifest.json";
const canvasId = basePath + "canvas/0";
if (data[fieldMap.published] != "published") continue;
console.log(`miniature: ${i} ${miniatureId}`);
//Create output directory for this miniature
const outputFilePath = path.join(config.outputDir, miniatureId);
try {
await fsPromises.mkdir(outputFilePath, { recursive: true });
}
catch (error) {
}
//Download directus object data for each image.
//Including format, width/height
const images = [];
for (let j = 0; j < fieldMap.image.length; j++) {
const item = fieldMap.image[j];
if (!data[item.key]) continue;
console.log("image", j);
const imageData = await fetchFileObject(data[item.key]);
//Optionaly download image file and create iiif image info.json
if (config.imageDownload) {
const imageBasePath = config.image.publicPath + imageData[fieldMap.imagePublicPath].toUpperCase().replace(/\s/g, '_');
const imageFullPath = imageBasePath + "/full/max/0";
const imageId = config.basePath + path.join(miniatureId, imageFullPath, "default.jpg");
const imageServiceId = config.basePath + path.join(miniatureId, imageBasePath);
const imageInfoFile = path.join(outputFilePath, imageBasePath, "info.json");
images.push({
"id": imageId,
"type": "Image",
"format": imageData.type,
"height": imageData.height,
"width": imageData.width,
"label": {
"en": [
item.label
]
},
"service": [
{
"id": imageServiceId,
"type": "ImageService3",
"profile": "level0"
}
]
});
let workingImageWidth = imageData.width;
let workingImageHeight = imageData.height;
const imageScaleList = [];
do {
const imageSizePath = path.join(
imageBasePath,
"full",
workingImageWidth == imageData.width ? "max" : workingImageWidth + "," + workingImageHeight,
"0"
);
const imageDownloadDir = path.join(outputFilePath, imageSizePath);
const imageDownloadFile = path.join(outputFilePath, imageSizePath, "default.jpg");
try {
await fsPromises.mkdir(imageDownloadDir, { recursive: true });
}
catch (error) {
}
await downloadImage(imageData.id, imageDownloadFile, { width: workingImageWidth, height: workingImageHeight });
imageScaleList.push({
"width": workingImageWidth,
"height": workingImageHeight,
})
workingImageWidth = Math.ceil(workingImageWidth * config.image.scaleIncrement);
workingImageHeight = Math.ceil(workingImageHeight * config.image.scaleIncrement);
} while (config.image.scale && workingImageWidth > config.image.scaleMinWidth)
const imageInfo = {
"@context": "http://iiif.io/api/image/3/context.json",
"id": imageServiceId,
"type": "ImageService3",
"profile": "level0",
"protocol": "http://iiif.io/api/image",
"height": imageData.height,
"width": imageData.width,
"extraFormats": [
"jpg"
],
"extraQualities": [
"default"
],
"extraFeatures": [],
"tiles": [
{
"scaleFactors": [
1
],
"height": imageData.height,
"width": imageData.width,
}
],
"sizes": imageScaleList
}
await fsPromises.writeFile(imageInfoFile, JSON.stringify(imageInfo, null, " "));
}
if (config.imageAPI) {
const imageExtension = imageData.type == "image/tiff" ? "tif" : "jpg";
const imageAPIBase = config.imageAPI + imageData.filename_disk;
const imageId = imageAPIBase + "/full/max/0/default." + imageExtension;
images.push({
"id": imageId,
"type": "Image",
"format": imageData.type,
"height": imageData.height,
"width": imageData.width,
"label": {
"en": [
item.label
]
},
"service": [
{
"id": imageAPIBase,
"type": "ImageService3",
"profile": "level2"
}
]
});
}
}
//add MA-XRF Scan image
if (config.imageAPI && data[config.fieldMap.maXrf]) {
for (let j = 0; j < data[config.fieldMap.maXrf].length; j++) {
const foundScan = maXrfAll.find(s => s.id == data[config.fieldMap.maXrf][j]);
if (foundScan && foundScan.ma_xrf_scan) {
console.log("MA-XRF", j);
const imageData = await fetchFileObject(foundScan.ma_xrf_scan);
const imageExtension = imageData.type == "image/tiff" ? "tif" : "jpg";
const imageAPIBase = config.imageAPI + imageData.filename_disk;
const imageId = imageAPIBase + "/full/max/0/default." + imageExtension;
const maXrfLabel = 'MA-XRF ' + foundScan.element_investigated;
images.push({
"id": imageId,
"type": "Image",
"format": imageData.type,
"height": imageData.height,
"width": imageData.width,
"label": {
"en": [
maXrfLabel
]
},
"service": [
{
"id": imageAPIBase,
"type": "ImageService3",
"profile": "level2"
}
]
});
}
}
}
const canvasHeight = images[0]?.height || 1800;
const canvasWidth = images[0]?.width || 1200;
const annotationPageId = basePath + "page/0/0";
const annotationPaintingId = basePath + "painting/0";
//Calculate physical scale - used for ruler.
const physicalScale = +(data[fieldMap.dimensionsHeight] / canvasHeight).toFixed(4);
//Build iiif annotation for each miniature annotation marked with 'hotspot'.
//Including coordinates.
const annotationItems = [];
if (Array.isArray(data[fieldMap.annotation.key])) {
for (let j = 0; j < data[fieldMap.annotation.key].length; j++) {
const micrographId = data[fieldMap.annotation.key][j];
console.log("micrograph index", j);
console.log("micrographId", micrographId);
const currentItem = await fetchMicrograph(micrographId);
if (!currentItem || !currentItem.hotspot || !currentItem.micrograph) continue
console.log("micrograph hotspot", true);
const annotationItemId = basePath + path.join("annotation/tag/", currentItem.id.toString());
const targetCoords = `${currentItem[fieldMap.annotation.x]},${currentItem[fieldMap.annotation.y]},${currentItem[fieldMap.annotation.w]},${currentItem[fieldMap.annotation.h]}`;
const target = canvasId + "#xywh=" + targetCoords;
const micrographManifestId = `${config.basePath}${miniatureId}/micrograph/${micrographId}/manifest.json`;
const micrographManifestUrl = encodeURI(micrographManifestId);
const micrographDownloadUrl = config.micrographBasePath + currentItem[fieldMap.annotation.uuid] + '?' + fieldMap.annotation.fullSizeURLParameters;
const micrographThumbnailUrl = config.micrographBasePath + currentItem[fieldMap.annotation.uuid] + '?' + fieldMap.annotation.thumbnailURLParameters;
const micrographLabel = currentItem[fieldMap.annotation.description];
//https://github.com/ProjectMirador/mirador/blob/master/src/config/settings.js#L239
//https://github.com/ProjectMirador/mirador/blob/master/src/lib/htmlRules.js
const value = `<div>
<p>${micrographLabel}</p>
<a href=${micrographManifestUrl} alt="Open in Mirador"><img src="${micrographThumbnailUrl}" alt="${micrographLabel}"/></a>
<a href=${micrographManifestUrl} target="__blank">Open iiif Manifest URL</a>
<a href=${micrographDownloadUrl} target="__blank">Download image</a>
</div>`;
annotationItems.push({
"id": annotationItemId,
"type": "Annotation",
"motivation": "commenting",
"body": {
"type": "TextualBody",
"value": value,
"language": "en",
"format": "text/html"
},
"target": target
});
if (config.micrographBuildManifest) {
await micrographBuildManifest(
currentItem,
miniatureId,
data[fieldMap.label] + ' - ' + micrographLabel
);
}
}
}
//Build iiif presentation manifest
const manifest = {
"@context": "http://iiif.io/api/presentation/3/context.json",
"id": manifestId,
"type": "Manifest",
"label": {
"en": [
data[fieldMap.label]
]
},
"summary": {
"en": [
...fieldMap.description.map(item => data[item]).filter(item => item)
]
},
"requiredStatement": {
"label": {
"en": [
"Collection"
]
},
"value": {
"en": [
config.collection
]
}
},
"rights": config.license,
"items": [
{
"id": canvasId,
"type": "Canvas",
"height": canvasHeight,
"width": canvasWidth,
"items": [
{
"id": annotationPageId,
"type": "AnnotationPage",
"items": [
{
"id": annotationPaintingId,
"type": "Annotation",
"motivation": "painting",
"body": {
"type": "Choice",
"items": images
},
"target": canvasId
}
]
}
],
//This is an old definition of Physical Dimensions which fails validation
//https://iiif.io/api/annex/services/#physical-dimensions
// "service": {
// "@context": "http://iiif.io/api/annex/services/physdim/1/context.json",
// "profile": "http://iiif.io/api/annex/services/physdim",
// "physicalScale": physicalScale,
// "physicalUnits": config.dimensionsUnits
// },
//Using new extension style decleration described here:
//https://github.com/IIIF/api/issues/1358
//https://gist.github.com/workergnome/01a3c617b0f5a6ee8a2fb51fc44666bf
"physicalDimensions": {
"type": "PhysicalDimension",
"profile": "http://iiif.io/api/annex/extensions/physdim",
"physicalScale": physicalScale,
"physicalUnits": config.dimensionsUnits
},
"annotations": [
{
"id": annotationPageId,
"type": "AnnotationPage",
"items": annotationItems
}
]
},
],
}
await fsPromises.writeFile(path.join(outputFilePath, "manifest.json"), JSON.stringify(manifest, null, " "));
}
}
main();