-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.mjs
149 lines (133 loc) · 4.97 KB
/
main.mjs
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
/*
* Copyright 2024 Code Inc. <https://www.codeinc.co>
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
import express from 'express';
import multer from 'multer';
import * as path from 'path';
import * as fs from 'fs';
import uniqid from 'uniqid';
import Jimp from 'jimp';
const port = +(process.env.PORT ?? 3000);
const tempDir = 'temp';
const app = express();
const upload = multer({dest: tempDir});
const cpUpload = upload.fields([
{name: 'image', maxCount: 1},
{name: 'watermark', maxCount: 1},
]);
/**
* Health check endpoint
*/
app.get('/health', (req, res) => {
res.json({
status: "up",
timestamp: new Date().toISOString()
});
});
/**
* Apply watermark to an image
*/
app.post('/apply', cpUpload, async (req, res) => {
const imageFile = req.files['image'][0];
const watermarkFile = req.files['watermark'][0];
console.log(`Applying watermark "${watermarkFile.originalname}" to the image "${imageFile.originalname}"`);
try {
if (!imageFile?.filename || !watermarkFile?.filename) {
throw new Error('No file uploaded');
}
// converting the PDF file to images
const watermarkedImagePath = `${tempDir}/${uniqid()}.${req.body.format ?? 'png'}`;
const mainImage = await Jimp.read(imageFile.path);
const watermarkImage = await Jimp.read(watermarkFile.path);
// calculating the dimensions of the watermark
const ratio = (req.body.size ?? 75) / 100;
let newHeight, newWidth;
if ((mainImage.getHeight() / mainImage.getWidth()) < (watermarkImage.getHeight() / watermarkImage.getWidth())) {
newHeight = ratio * mainImage.getHeight();
newWidth = newHeight / watermarkImage.getHeight() * watermarkImage.getWidth();
} else {
newWidth = ratio * mainImage.getWidth();
newHeight = newWidth / watermarkImage.getWidth() * watermarkImage.getHeight();
}
// resizing the watermark
watermarkImage.resize(newWidth, newHeight, Jimp.RESIZE_BICUBIC);
// adds blur to the underlying image
if (req.body.blur) {
mainImage.blur(+(req.body.blur));
}
// calculating the position of the watermark
const position = req.body.position ?? 'center';
const padding = +(req.body.padding ?? 10);
let x, y;
switch (position) {
case 'top-left':
x = padding;
y = padding;
break;
case 'top-right':
x = mainImage.getWidth() - newWidth - padding;
y = padding;
break;
case 'top':
x = (mainImage.getWidth() - newWidth) / 2;
y = padding;
break;
case 'bottom-left':
x = padding;
y = mainImage.getHeight() - newHeight - padding;
break;
case 'bottom-right':
x = mainImage.getWidth() - newWidth - padding;
y = mainImage.getHeight() - newHeight - padding;
break;
case 'bottom':
x = (mainImage.getWidth() - newWidth) / 2;
y = mainImage.getHeight() - newHeight - padding;
break;
case 'left':
x = padding;
y = (mainImage.getHeight() - newHeight) / 2;
break;
case 'right':
x = mainImage.getWidth() - newWidth - padding;
y = (mainImage.getHeight() - newHeight) / 2;
break;
case 'center':
default:
x = (mainImage.getWidth() - newWidth) / 2;
y = (mainImage.getHeight() - newHeight) / 2;
break;
}
// applying the watermark to the image
await mainImage.composite(watermarkImage, x, y, {
opacitySource: ((req.body.opacity ?? 75) / 100),
opacityDest: 1,
mode: Jimp.BLEND_SOURCE_OVER,
});
// writing the watermarked image to the file system
await mainImage.quality(+(req.body.quality ?? 100)).writeAsync(watermarkedImagePath);
// sending the images as a response
res.sendFile(watermarkedImagePath, {root: path.resolve()}, () => {
fs.unlinkSync(imageFile.path);
fs.unlinkSync(watermarkFile.path);
fs.unlinkSync(watermarkedImagePath);
});
}
catch (e) {
console.error(`Error: ${e.message}`);
res.status(400);
res.send({error: e.message});
fs.unlinkSync(imageFile.path);
fs.unlinkSync(watermarkFile.path);
if (typeof watermarkedImagePath !== "undefined" && fs.existsSync(watermarkedImagePath)) {
fs.unlinkSync(watermarkedImagePath);
}
}
}, cpUpload);
app.listen(port, () => {
console.log(`The watermarker service is now listening on port ${port}`);
});