-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·123 lines (94 loc) · 2.94 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
#!/usr/bin/env node
import fs from 'fs'
import yaml from 'js-yaml'
import sharp from 'sharp'
import chalk from 'chalk'
import { parseAnnotation, generateAnnotation } from '@allmaps/annotation'
import { generateId } from '@allmaps/id'
import { scaleMap, createManifest } from './lib.js'
const serverBaseUrl = 'http://localhost:5550'
const annotationUrls = yaml.load(fs.readFileSync('./annotations.yaml', 'utf8'))
const manifests = {}
console.log(
chalk.bgCyanBright.black(' Creating IIIF Image API Level 0 tiles \n')
)
for (const {
url: annotationUrl,
manifests: manifestNames,
tags
} of annotationUrls) {
console.log('Annotation:', chalk.green(annotationUrl))
console.log(
' Fetching annotation:',
chalk.yellowBright.underline(annotationUrl)
)
const annotation = await fetch(annotationUrl).then((response) =>
response.json()
)
const maps = parseAnnotation(annotation)
const map = maps[0]
const imageId = map.resource.id
const allmapsImageId = await generateId(imageId)
const fullImageUrl = `${imageId}/full/full/0/default.jpg`
console.log(
' Fetching full size image:',
chalk.yellowBright.underline(fullImageUrl)
)
const imageArrayBuffer = await fetch(fullImageUrl).then((response) =>
response.arrayBuffer()
)
const newImageId = `${serverBaseUrl}/iiif/${allmapsImageId}`
console.log(
' Creating IIIF Image API Level 0 tiles:',
chalk.yellowBright.underline(`${newImageId}/info.json`)
)
const image = await sharp(imageArrayBuffer).tile({
layout: 'iiif',
size: 768,
id: `${serverBaseUrl}/iiif`
})
const metadata = await image.metadata()
const scaledMap = scaleMap(map, metadata.width, metadata.height)
await image.toFile(`./iiif/${allmapsImageId}`)
const newAnnotationId = `${serverBaseUrl}/annotations/${allmapsImageId}.json`
const newMap = {
...scaledMap,
id: newAnnotationId,
resource: {
...scaledMap.resource,
id: newImageId
}
}
const newAnnotation = generateAnnotation([newMap])
fs.writeFileSync(
`./annotations/${allmapsImageId}.json`,
JSON.stringify(newAnnotation, null, 2)
)
if (manifestNames) {
for (const manifestName of manifestNames) {
if (!manifests[manifestName]) {
manifests[manifestName] = []
}
manifests[manifestName].push({
imageId: newImageId,
width: metadata.width,
height: metadata.height
})
}
}
console.log(chalk.green(' Done!\n'))
}
console.log(chalk.bgCyanBright.black(' Creating IIIF Manifests \n'))
for (let [manifestName, images] of Object.entries(manifests)) {
const manifestId = `${serverBaseUrl}/manifests/${manifestName}.json`
const manifest = createManifest(manifestId, images)
console.log(
'Creating IIIF Manifest:',
chalk.yellowBright.underline(manifestId)
)
fs.writeFileSync(
`./manifests/${manifestName}.json`,
JSON.stringify(manifest, null, 2)
)
}
console.log(chalk.rgb(123, 200, 67).underline('Done!\n'))