-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
49 lines (40 loc) · 1.4 KB
/
index.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
import express from 'express';
import sharp from 'sharp';
import { chooseClientForURL } from './utils.mjs'
const port = process.argv[2] || 8000;
const app = express();
app.get('/img/resize', (req, res) => {
const url = req.query.u;
if (!url) {
res.status(400).send({error: 'No url is provided'});
return;
}
try {
const decodedURL = decodeURIComponent(url);
const client = chooseClientForURL(decodedURL);
if (!client) {
res.status(400).send({error: `${protocol} is not supported`});
return;
}
const width = req.query.w && Number(req.query.w);
const height = req.query.h && Number(req.query.h);
const resizeStream = sharp().resize(width, height);
client.get(decodedURL, img => {
if (img.statusCode >= 200 && img.statusCode < 300) {
img.pipe(resizeStream).pipe(res);
} else if (img.statusCode == 404) {
res.sendStatus(404);
} else {
res.status(400).send({error: 'Could not resize image'});
}
});
} catch(e) {
console.warn(e);
const errorMessage = e instanceof TypeError
? 'Invalid url is provided'
: e.toString();
res.status(400).send({error: errorMessage});
return;
}
})
app.listen(port, () => console.log(`Listening on port ${port}`));