-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprocessor.js
202 lines (174 loc) · 6.33 KB
/
processor.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
const debug = require('debug')('iiif-processor:main');
const mime = require('mime-types');
const path = require('path');
const sharp = require('sharp');
const { Operations } = require('./transform');
const IIIFError = require('./error');
const IIIFVersions = require('./versions');
const fixupSlashes = (path, leaveOne) => {
const replacement = leaveOne ? '/' : '';
return path?.replace(/^\/*/, replacement).replace(/\/*$/, replacement);
};
const getIIIFVersion = (url, opts = {}) => {
const uri = new URL(url);
try {
let { iiifVersion, pathPrefix } = opts;
if (!iiifVersion) {
const match = /^\/iiif\/(?<v>\d)\//.exec(uri.pathname);
iiifVersion = match.groups.v;
}
if (!pathPrefix) pathPrefix = `iiif/${iiifVersion}/`;
return { iiifVersion, pathPrefix };
} catch {
throw new IIIFError(`Cannot determine IIIF version from path ${uri.path}`);
}
};
class Processor {
constructor (url, streamResolver, opts = {}) {
const { iiifVersion, pathPrefix } = getIIIFVersion(url, opts);
if (typeof streamResolver !== 'function') {
throw new IIIFError('streamResolver option must be specified');
}
if (opts.max?.height && !opts.max?.width) {
throw new IIIFError('maxHeight cannot be specified without maxWidth');
};
const defaults = {
dimensionFunction: this.defaultDimensionFunction,
density: null
};
this
.setOpts({ ...defaults, ...opts, pathPrefix, iiifVersion })
.initialize(url, streamResolver);
}
setOpts (opts) {
this.errorClass = IIIFError;
this.dimensionFunction = opts.dimensionFunction;
this.max = { ...opts.max };
this.includeMetadata = !!opts.includeMetadata;
this.density = opts.density;
this.pathPrefix = fixupSlashes(opts.pathPrefix, true);
this.sharpOptions = { ...opts.sharpOptions };
this.version = opts.iiifVersion;
return this;
}
parseUrl (url) {
const parser = new RegExp(`(?<baseUrl>https?://[^/]+${this.pathPrefix})(?<path>.+)$`);
const { baseUrl, path } = parser.exec(url).groups;
const result = this.Implementation.Calculator.parsePath(path);
result.baseUrl = baseUrl;
return result;
}
initialize (url, streamResolver) {
this.Implementation = IIIFVersions[this.version];
if (!this.Implementation) {
throw new IIIFError(`No implementation found for IIIF Image API v${this.version}`);
}
const params = this.parseUrl(url);
debug('Parsed URL: %j', params);
Object.assign(this, params);
this.streamResolver = streamResolver;
if (this.quality && this.format) {
this.filename = [this.quality, this.format].join('.');
} else if (this.info) {
this.filename = 'info.json';
}
return this;
}
async withStream ({ id, baseUrl }, callback) {
debug('Requesting stream for %s', id);
if (this.streamResolver.length === 2) {
return await this.streamResolver({ id, baseUrl }, callback);
} else {
const stream = await this.streamResolver({ id, baseUrl });
return await callback(stream);
}
}
async defaultDimensionFunction ({ id, baseUrl }) {
const result = [];
let page = 0;
const target = sharp({ limitInputPixels: false, page });
return await this.withStream({ id, baseUrl }, async (stream) => {
stream.pipe(target);
const { width, height, pages } = await target.metadata();
result.push({ width, height });
for (page += 1; page < pages; page++) {
const scale = 1 / 2 ** page;
result.push({ width: Math.floor(width * scale), height: Math.floor(height * scale) });
}
return result;
});
}
async dimensions () {
const fallback = this.dimensionFunction !== this.defaultDimensionFunction;
if (!this.sizeInfo) {
debug('Attempting to use dimensionFunction to retrieve dimensions for %j', this.id);
const params = { id: this.id, baseUrl: this.baseUrl };
let dims = await this.dimensionFunction(params);
if (fallback && !dims) {
const warning =
'Unable to get dimensions for %s using custom function. Falling back to sharp.metadata().';
debug(warning, this.id);
console.warn(warning, this.id);
dims = await this.defaultDimensionFunction(params);
}
if (!Array.isArray(dims)) dims = [dims];
this.sizeInfo = dims;
}
return this.sizeInfo;
}
async infoJson () {
const [dim] = await this.dimensions();
const sizes = [];
for (let size = [dim.width, dim.height]; size.every((x) => x >= 64); size = size.map((x) => Math.floor(x / 2))) {
sizes.push({ width: size[0], height: size[1] });
}
const id = [fixupSlashes(this.baseUrl), fixupSlashes(this.id)].join('/');
const doc = this.Implementation.infoDoc({ id, ...dim, sizes, max: this.max });
for (const prop in doc) {
if (doc[prop] === null || doc[prop] === undefined) delete doc[prop];
}
// Serialize sets as arrays
const body = JSON.stringify(doc, (_key, value) =>
value?.constructor === Set ? [...value] : value
);
return { contentType: 'application/json', body };
}
operations (dim) {
const { sharpOptions: sharp, max } = this;
return new Operations(this.version, dim, { sharp, max })
.region(this.region)
.size(this.size)
.rotation(this.rotation)
.quality(this.quality)
.format(this.format, this.density)
.withMetadata(this.includeMetadata);
}
async iiifImage () {
const dim = await this.dimensions();
const operations = this.operations(dim);
const pipeline = await operations.pipeline();
const result = await this.withStream({ id: this.id, baseUrl: this.baseUrl }, async (stream) => {
debug('piping stream to pipeline');
const transformed = await stream.pipe(pipeline);
debug('converting to buffer');
return await transformed.toBuffer();
});
debug('returning %d bytes', result.length);
debug('baseUrl', this.baseUrl);
const canonicalUrl = new URL(path.join(this.id, operations.canonicalPath()), this.baseUrl);
return {
canonicalLink: canonicalUrl.toString(),
profileLink: this.Implementation.profileLink,
contentType: mime.lookup(this.format),
body: result
};
}
async execute () {
if (this.filename === 'info.json') {
return await this.infoJson();
} else {
return await this.iiifImage();
}
}
}
module.exports = Processor;