-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.js
36 lines (33 loc) · 955 Bytes
/
proxy.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
import Canvas from "canvas";
/**
* Proxy image and transform the image format to jpg at the backend,
* so as to satisfy the format requirement of shotit
* search api under certain scenarios.
*/
export default async (req, res) => {
const { url = "" } = req.query;
function error500(e, res) {
console.log(e);
res.status(500).send("Internal Server Error");
}
if (req.method === "GET") {
if (!url) {
res.status(403).send("No URL attached");
return;
}
const width = 640;
const height = 360;
const canvas = Canvas.createCanvas(width, height);
const ctx = canvas.getContext("2d");
const image = await Canvas.loadImage(url).catch((e) => {
error500(e, res);
});
if (image) {
ctx.drawImage(image, 0, 0, width, height);
res.setHeader("Content-Type", "image/jpeg");
canvas.createJPEGStream().pipe(res);
} else {
// loadImage has been error500 above
}
}
};