File size: 1,363 Bytes
8e2e08f 1ff1550 8e2e08f 9fb7b1b 8e2e08f 8745eb1 8e2e08f 4487abf 8e2e08f 4487abf 9b99335 8e2e08f 9b99335 8e2e08f 9fb7b1b 1ff1550 8e2e08f |
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 |
import express from "express";
import multer from "multer";
import sharp from "sharp";
import fs from "fs";
import path from "path";
const app = express();
const upload = multer({ dest: "/tmp/uploads" });
app.post("/convert", upload.single("file"), async (req, res) => {
try {
if (!req.file) {
return res.status(400).send("No file uploaded");
}
const originalName = path.parse(req.file.originalname).name;
const outputFileName = `${originalName}.jpg`;
const outputPath = path.join("/tmp", outputFileName);
// Convert to JPEG with max quality
await sharp(req.file.path)
.jpeg({ quality: 100, chromaSubsampling: "4:4:4" }) // Highest quality possible
.toFile(outputPath);
res.setHeader("Content-Type", "image/jpeg");
res.setHeader("Content-Disposition", `attachment; filename="${outputFileName}"`);
const fileStream = fs.createReadStream(outputPath);
fileStream.pipe(res);
fileStream.on("close", () => {
fs.unlinkSync(req.file.path);
fs.unlinkSync(outputPath);
});
} catch (error) {
console.error(error);
res.status(500).send("Conversion failed");
}
});
const PORT = process.env.PORT || 7860;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|