-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
58 lines (47 loc) · 1.29 KB
/
app.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import express from "express";
import rateLimit from "express-rate-limit";
import cors from "cors";
import heicConvert from "heic-convert";
import axios from "axios";
const PORT = 4000;
const app = express();
app.use(cors()).use(
rateLimit({
windowMs: 1 * 1000,
max: 100,
})
);
app.get("/", async (_, res) =>
res.redirect("https://github.com/Noppawat3939/heic-image-service")
);
app.get("/convert/heic", async (req, res) => {
const { url = "", format = "png" } = req.query;
if (
!url ||
url?.search(".heic") === -1 ||
(format && !["png", "jpeg"].includes(format.toLowerCase()))
)
return res.send("failed convert heic image");
try {
const { data: buffer } = await axios({ url, responseType: "arraybuffer" });
const arrBuffer = await heicConvert({
buffer,
format: format.toUpperCase(),
});
res.setHeader("Content-Type", `image/${format}`);
res.send(arrBuffer);
} catch (err) {
console.debug(err);
return res.send("failed convert heic image");
}
});
app.get("/check", async (_, res) => {
return res.status(200).json({
success: true,
message: "server is running smoothly",
timestampt: new Date().toISOString().split("T").join(" "),
});
});
app.listen(PORT, () => {
console.log(`server is running on port ${PORT}`);
});