-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
254 lines (220 loc) Β· 7.82 KB
/
server.js
File metadata and controls
254 lines (220 loc) Β· 7.82 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import http from 'http';
import sharp from 'sharp';
import https from 'https';
const PORT = parseInt(process.env.PORT || '4013', 10);
// βββ Helpers βββ
function fetchImage(url) {
return new Promise((resolve, reject) => {
const client = url.startsWith('https') ? https : http;
client.get(url, { headers: { 'User-Agent': 'REPIC/1.0' } }, (res) => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
return fetchImage(res.headers.location).then(resolve, reject);
}
if (res.statusCode !== 200) {
return reject(new Error(`HTTP ${res.statusCode}`));
}
const chunks = [];
res.on('data', (c) => chunks.push(c));
res.on('end', () => resolve(Buffer.concat(chunks)));
res.on('error', reject);
}).on('error', reject);
});
}
async function getImageBuffer(body) {
if (body.base64) return Buffer.from(body.base64, 'base64');
if (body.url) return fetchImage(body.url);
throw new Error('Provide url or base64');
}
function readBody(req) {
return new Promise((resolve, reject) => {
const chunks = [];
req.on('data', (c) => chunks.push(c));
req.on('end', () => {
try { resolve(JSON.parse(Buffer.concat(chunks).toString())); }
catch { reject(new Error('Invalid JSON')); }
});
req.on('error', reject);
});
}
function json(res, status, data) {
const payload = JSON.stringify(data);
res.writeHead(status, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
});
res.end(payload);
}
// βββ Image Processing βββ
async function removeBackground(buf) {
const image = sharp(buf);
const { data, info } = await image.ensureAlpha().raw().toBuffer({ resolveWithObject: true });
for (let i = 0; i < data.length; i += 4) {
const r = data[i], g = data[i + 1], b = data[i + 2];
const brightness = (r + g + b) / 3;
const saturation = Math.max(r, g, b) - Math.min(r, g, b);
if (brightness > 200 && saturation < 30) {
data[i + 3] = 0;
}
}
return sharp(data, { raw: { width: info.width, height: info.height, channels: 4 } })
.png()
.toBuffer();
}
async function generateFavicon(buf) {
const sizes = [16, 32, 180, 192, 512];
const results = {};
for (const size of sizes) {
const resized = await sharp(buf)
.resize(size, size, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })
.png()
.toBuffer();
results[`${size}x${size}`] = resized.toString('base64');
}
return results;
}
async function resizeImage(buf, opts) {
let pipeline = sharp(buf).resize(
opts.width || null,
opts.height || null,
{ fit: opts.fit || 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } }
);
const fmt = opts.format || 'png';
pipeline = pipeline[fmt === 'jpeg' ? 'jpeg' : fmt === 'webp' ? 'webp' : fmt === 'avif' ? 'avif' : 'png']();
return pipeline.toBuffer();
}
async function convertImage(buf, opts) {
const fmt = opts.format;
const quality = opts.quality || undefined;
let pipeline = sharp(buf);
if (fmt === 'jpeg') pipeline = pipeline.jpeg({ quality });
else if (fmt === 'webp') pipeline = pipeline.webp({ quality });
else if (fmt === 'avif') pipeline = pipeline.avif({ quality });
else pipeline = pipeline.png();
return pipeline.toBuffer();
}
async function cropImage(buf, opts) {
return sharp(buf)
.extract({ left: opts.left, top: opts.top, width: opts.width, height: opts.height })
.toBuffer();
}
async function getMetadata(buf) {
const meta = await sharp(buf).metadata();
return {
width: meta.width,
height: meta.height,
format: meta.format,
channels: meta.channels,
hasAlpha: meta.hasAlpha,
size: buf.length,
};
}
async function compositeImages(body) {
const baseBuf = body.base64
? Buffer.from(body.base64, 'base64')
: await fetchImage(body.url);
const overlayBuf = body.overlay_base64
? Buffer.from(body.overlay_base64, 'base64')
: await fetchImage(body.overlay_url);
const gravityMap = {
north: 'north', south: 'south', east: 'east', west: 'west',
northeast: 'northeast', northwest: 'northwest',
southeast: 'southeast', southwest: 'southwest',
centre: 'centre', center: 'centre',
};
const gravity = gravityMap[body.gravity] || 'southeast';
let overlay = sharp(overlayBuf);
if (body.opacity != null && body.opacity < 1) {
const meta = await sharp(overlayBuf).metadata();
const { data, info } = await sharp(overlayBuf).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
for (let i = 3; i < data.length; i += 4) {
data[i] = Math.round(data[i] * body.opacity);
}
overlay = sharp(data, { raw: { width: info.width, height: info.height, channels: 4 } });
}
const overlayPng = await overlay.png().toBuffer();
return sharp(baseBuf)
.composite([{ input: overlayPng, gravity }])
.png()
.toBuffer();
}
// βββ Routes βββ
const routes = {
'GET /api/health': async (_req, res) => {
json(res, 200, { status: 'ok', service: 'repic' });
},
'POST /api/remove-background': async (req, res) => {
const body = await readBody(req);
const buf = await getImageBuffer(body);
const result = await removeBackground(buf);
json(res, 200, { base64: result.toString('base64'), format: 'png' });
},
'POST /api/favicon': async (req, res) => {
const body = await readBody(req);
const buf = await getImageBuffer(body);
const favicons = await generateFavicon(buf);
json(res, 200, { favicons });
},
'POST /api/resize': async (req, res) => {
const body = await readBody(req);
const buf = await getImageBuffer(body);
const result = await resizeImage(buf, body);
const fmt = body.format || 'png';
json(res, 200, { base64: result.toString('base64'), format: fmt });
},
'POST /api/convert': async (req, res) => {
const body = await readBody(req);
if (!body.format) return json(res, 400, { error: 'format is required' });
const buf = await getImageBuffer(body);
const result = await convertImage(buf, body);
json(res, 200, { base64: result.toString('base64'), format: body.format });
},
'POST /api/crop': async (req, res) => {
const body = await readBody(req);
if (body.left == null || body.top == null || body.width == null || body.height == null) {
return json(res, 400, { error: 'left, top, width, height are required' });
}
const buf = await getImageBuffer(body);
const result = await cropImage(buf, body);
json(res, 200, { base64: result.toString('base64'), format: 'png' });
},
'POST /api/metadata': async (req, res) => {
const body = await readBody(req);
const buf = await getImageBuffer(body);
const meta = await getMetadata(buf);
json(res, 200, meta);
},
'POST /api/composite': async (req, res) => {
const body = await readBody(req);
const result = await compositeImages(body);
json(res, 200, { base64: result.toString('base64'), format: 'png' });
},
};
// βββ Server βββ
const server = http.createServer(async (req, res) => {
// CORS preflight
if (req.method === 'OPTIONS') {
res.writeHead(204, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
});
return res.end();
}
const url = new URL(req.url, `http://localhost:${PORT}`);
const key = `${req.method} ${url.pathname}`;
const handler = routes[key];
if (!handler) {
return json(res, 404, { error: 'Not found' });
}
try {
await handler(req, res);
} catch (err) {
console.error(`[repic] ${key} error:`, err.message);
json(res, 500, { error: err.message });
}
});
server.listen(PORT, () => {
console.log(`[repic] Image processing server running on port ${PORT}`);
});