commit 42ca3df08b19e976a02970b91337174e53648c85 Author: Matthew Jackson Date: Mon Dec 29 19:12:34 2025 -0800 Create server.js diff --git a/server.js b/server.js new file mode 100644 index 0000000..c71f2f7 --- /dev/null +++ b/server.js @@ -0,0 +1,34 @@ +import express from "express"; +import sharp from "sharp"; + +const app = express(); + +app.use(express.raw({ type: "*/*", limit: "30mb" })); + +app.get("/health", (_req, res) => res.status(200).send("ok")); + +app.post("/convert", async (req, res) => { + try { + const token = process.env.CONVERTER_TOKEN; + const auth = req.headers.authorization || ""; + if (!token || auth !== `Bearer ${token}`) { + return res.status(401).send("Unauthorized"); + } + + const input = req.body; // Buffer + if (!input || input.length === 0) return res.status(400).send("Empty body"); + + const jpeg = await sharp(input, { failOnError: false }) + .rotate() // respect EXIF orientation + .jpeg({ quality: 85 }) + .toBuffer(); + + res.setHeader("Content-Type", "image/jpeg"); + res.status(200).send(jpeg); + } catch (e) { + res.status(500).send(String(e?.stack || e)); + } +}); + +const port = process.env.PORT || 8080; +app.listen(port, () => console.log(`converter listening on ${port}`));