Spaces:
Runtime error
Runtime error
Update server.js
Browse files
server.js
CHANGED
|
@@ -5,8 +5,6 @@ import fs from "fs";
|
|
| 5 |
import path from "path";
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
-
|
| 9 |
-
// Use /tmp for uploads (writable on Hugging Face)
|
| 10 |
const upload = multer({ dest: "/tmp/uploads" });
|
| 11 |
|
| 12 |
app.post("/convert", upload.single("file"), async (req, res) => {
|
|
@@ -15,17 +13,15 @@ app.post("/convert", upload.single("file"), async (req, res) => {
|
|
| 15 |
return res.status(400).send("No file uploaded");
|
| 16 |
}
|
| 17 |
|
| 18 |
-
// Get original name without extension
|
| 19 |
const originalName = path.parse(req.file.originalname).name;
|
| 20 |
const outputFileName = `${originalName}.jpg`;
|
| 21 |
const outputPath = path.join("/tmp", outputFileName);
|
| 22 |
|
| 23 |
-
// Convert to
|
| 24 |
await sharp(req.file.path)
|
| 25 |
-
.
|
| 26 |
.toFile(outputPath);
|
| 27 |
|
| 28 |
-
// Send file directly as download
|
| 29 |
res.setHeader("Content-Type", "image/jpeg");
|
| 30 |
res.setHeader("Content-Disposition", `attachment; filename="${outputFileName}"`);
|
| 31 |
|
|
|
|
| 5 |
import path from "path";
|
| 6 |
|
| 7 |
const app = express();
|
|
|
|
|
|
|
| 8 |
const upload = multer({ dest: "/tmp/uploads" });
|
| 9 |
|
| 10 |
app.post("/convert", upload.single("file"), async (req, res) => {
|
|
|
|
| 13 |
return res.status(400).send("No file uploaded");
|
| 14 |
}
|
| 15 |
|
|
|
|
| 16 |
const originalName = path.parse(req.file.originalname).name;
|
| 17 |
const outputFileName = `${originalName}.jpg`;
|
| 18 |
const outputPath = path.join("/tmp", outputFileName);
|
| 19 |
|
| 20 |
+
// Convert to JPEG with max quality
|
| 21 |
await sharp(req.file.path)
|
| 22 |
+
.jpeg({ quality: 100, chromaSubsampling: "4:4:4" }) // Highest quality possible
|
| 23 |
.toFile(outputPath);
|
| 24 |
|
|
|
|
| 25 |
res.setHeader("Content-Type", "image/jpeg");
|
| 26 |
res.setHeader("Content-Disposition", `attachment; filename="${outputFileName}"`);
|
| 27 |
|