This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (62 loc) · 1.8 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
const config = require("./config.js");
const fastify = require("fastify")({
logger: { level: config.get("logLevel") },
});
const sharp = require("sharp");
const crypto = require("crypto");
const path = require("path");
const fs = require("fs-extra");
// Add file uploads handler
fastify.register(require("fastify-multipart"), {
limits: {
fileSize: config.get("uploadMaxSize"),
},
});
// Handle thumbnail creation route
fastify.post("/api/thumbnail", async (req, reply) => {
const files = await req.saveRequestFiles();
if (files.length !== 1) {
throw new Error("There should be 1 image");
}
fastify.log.debug(`Temp file path: ${files[0].filepath}`);
// Temporary output file
const reqId = crypto.randomUUID();
const outputFile = path.join(config.get("outputDir"), `${reqId}.webp`);
fastify.log.debug(`Output file path: ${outputFile}`);
await sharp(files[0].filepath)
.resize(config.get("thumbnail.width"), config.get("thumbnail.height"), {
fit: sharp.fit.cover,
position: sharp.gravity.north,
})
.webp({
quality: config.get("thumbnail.quality"),
smartSubsample: true,
force: true,
reductionEffort: config.get("thumbnail.reductionEffort"),
})
.toFile(outputFile);
// Reading output file
const outputFileStream = fs.createReadStream(outputFile);
// Sending file
await reply
.code(200)
.header("content-type", "image/webp")
.send(outputFileStream);
// Removing temporary file
await fs.remove(outputFile);
});
/**
* Start web server.
*/
async function start() {
try {
const host = config.get("hostname");
const port = config.get("port");
fastify.log.debug(`Loaded config ${config}`);
await fastify.listen(port, host);
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
}
start();