diff --git a/server.js b/server.js index c71f2f7..68886a9 100644 --- a/server.js +++ b/server.js @@ -3,20 +3,26 @@ import sharp from "sharp"; const app = express(); +// Raw binary uploads (HEIC/JPEG/etc) app.use(express.raw({ type: "*/*", limit: "30mb" })); +// Friendly "is it up" endpoints +app.get("/", (_req, res) => res.status(200).send("postconvert: ok")); 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"); + if (!input || input.length === 0) { + return res.status(400).send("Empty body"); + } const jpeg = await sharp(input, { failOnError: false }) .rotate() // respect EXIF orientation @@ -24,11 +30,23 @@ app.post("/convert", async (req, res) => { .toBuffer(); res.setHeader("Content-Type", "image/jpeg"); - res.status(200).send(jpeg); + return res.status(200).send(jpeg); } catch (e) { - res.status(500).send(String(e?.stack || e)); + return res.status(500).send(String(e?.stack || e)); } }); -const port = process.env.PORT || 8080; -app.listen(port, () => console.log(`converter listening on ${port}`)); +// Express will throw for oversized bodies; return a clean 413 +app.use((err, _req, res, next) => { + if (err?.type === "entity.too.large") { + return res.status(413).send("Payload too large (max 30mb)"); + } + return next(err); +}); + +const port = Number(process.env.PORT) || 8080; + +// IMPORTANT for Fly.io: bind to 0.0.0.0 (not localhost) +app.listen(port, "0.0.0.0", () => { + console.log(`converter listening on ${port}`); +});