-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (35 loc) · 1.14 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
const express = require('express');
const axios = require('axios');
const sharp = require('sharp');
const app = express();
function toQueryParams(obj) {
return Object.keys(obj).map(key => `${key}=${obj[key].trim()}`).join('&')
}
app.get('/ie/:path(*)', async (req, res) => {
const path = req.params.path
const query = req.query
const format = req.query.format || 'avif'
delete req.query.format;
const width = req.query.width || 368
delete req.query.width;
try {
const response = await axios({
method: 'get',
url: path + '?' + toQueryParams(query),
responseType: 'arraybuffer'
});
let imageData = response.data;
let processedImage = await sharp(imageData)
.resize(width, null)
.toFormat(format)
.toBuffer();
res.set('Content-Type', `image/${format}`);
res.send(processedImage);
} catch (error) {
console.error(error);
res.status(404).send('An error occurred while retrieving the image');
}
});
app.listen(3000, () => {
console.log('Express server listening on port 3000');
});