This repository was archived by the owner on Feb 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
87 lines (72 loc) · 3.42 KB
/
Copy pathapp.ts
File metadata and controls
87 lines (72 loc) · 3.42 KB
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
if (process.env.NODE_ENV !== 'production')
await import('dotenv/config');
import sharp from 'sharp';
import {join} from 'path';
import {parse} from 'url';
import {createServer} from 'http';
import {serveImage, NotImplementedError, RequestError} from '@archival-iiif/image-server-core';
sharp.concurrency(process.env.IIIF_IMAGE_CONCURRENCY ? parseInt(process.env.IIIF_IMAGE_CONCURRENCY) : 0);
if (process.env.NODE_ENV !== 'production')
sharp.queue.on('change', queueLength =>
console.log(`Image queue now contains ${queueLength} task(s)`));
console.log('Dependencies:');
console.table(sharp.versions);
console.log(`Available image input formats: ${(Object.values(sharp.format) as sharp.AvailableFormatInfo[])
.filter(format => format.input.file)
.map(format => format.id)
.join(', ')}`);
console.log(`Available image output formats: ${(Object.values(sharp.format) as sharp.AvailableFormatInfo[])
.filter(format => format.output.buffer)
.map(format => format.id)
.join(', ')}`);
console.log(`Number of image processing threads: ${sharp.concurrency()}`);
const server = createServer(async (req, res) => {
try {
const parsedUrl = parse(req.url as string, true);
const path = parsedUrl.pathname as string;
const parts = path.substring(1).split('/');
if (parts.length === 5 && parts[4].indexOf('.') >= 0) {
const imagePath = decodeURIComponent(parts[0]);
const region = parts[1];
const size = parts[2];
const rotation = parts[3];
const quality = parts[4].substring(0, parts[4].indexOf('.'));
const format = parts[4].substring(parts[4].indexOf('.') + 1);
process.env.NODE_ENV !== 'production' && console.log(`Received a request for an image on path ${imagePath}`);
const path = join(process.env.IIIF_IMAGE_ROOT_PATH as string, imagePath);
const maxSize = Array.isArray(parsedUrl.query.max)
? parseInt(parsedUrl.query.max[0])
: parsedUrl.query.max
? parseInt(parsedUrl.query.max)
: null;
const image = await serveImage(path, maxSize, {region, size, rotation, quality, format});
if (image.contentType) res.setHeader('Content-Type', image.contentType);
if (image.contentLength) res.setHeader('Content-Length', String(image.contentLength));
res.setHeader('Content-Disposition', `inline; filename="${imagePath}-${region}-${size}-${rotation}-${quality}.${format}"`);
res.write(image.image);
res.end();
process.env.NODE_ENV !== 'production' && console.log(`Sending an image on path ${imagePath}`);
}
else {
res.writeHead(404);
res.end();
}
}
catch (err: any) {
if (err instanceof RequestError) {
res.writeHead(400, err.message);
res.end();
}
else if (err instanceof NotImplementedError) {
res.writeHead(501, err.message);
res.end();
}
else {
console.error(`${err.status || 500} - ${req.method} - ${req.url} - ${err.message}`, {err});
res.writeHead(500, 'Internal Server Error');
res.end();
}
}
});
server.listen(parseInt(process.env.IIIF_IMAGE_PORT as string), () =>
console.log(`Image server started on http://localhost:${process.env.IIIF_IMAGE_PORT} 🚀`));