Update server.js
Browse files
server.js
CHANGED
|
@@ -1,48 +1,47 @@
|
|
| 1 |
-
import express from
|
| 2 |
-
import multer from
|
| 3 |
-
import sharp from
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
-
const port = process.env.PORT || 7860;
|
| 9 |
-
|
| 10 |
-
// Create /tmp/uploads folder (Hugging Face allows this path)
|
| 11 |
-
const uploadFolder = '/tmp/uploads';
|
| 12 |
-
if (!fs.existsSync(uploadFolder)) {
|
| 13 |
-
fs.mkdirSync(uploadFolder, { recursive: true });
|
| 14 |
-
}
|
| 15 |
-
|
| 16 |
-
// Multer storage: keep original name
|
| 17 |
-
const storage = multer.diskStorage({
|
| 18 |
-
destination: (req, file, cb) => cb(null, uploadFolder),
|
| 19 |
-
filename: (req, file, cb) => cb(null, file.originalname)
|
| 20 |
-
});
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
app.post('/convert', upload.single('file'), async (req, res) => {
|
| 26 |
try {
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
.toFile(outputPath);
|
| 36 |
|
| 37 |
-
// Send
|
| 38 |
-
res.setHeader(
|
| 39 |
-
res.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
} catch (error) {
|
| 41 |
console.error(error);
|
| 42 |
-
res.status(500).
|
| 43 |
}
|
| 44 |
});
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
});
|
|
|
|
| 1 |
+
import express from "express";
|
| 2 |
+
import multer from "multer";
|
| 3 |
+
import sharp from "sharp";
|
| 4 |
+
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) => {
|
|
|
|
| 13 |
try {
|
| 14 |
+
if (!req.file) {
|
| 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 JPG
|
| 24 |
+
await sharp(req.file.path)
|
| 25 |
+
.toFormat("jpeg")
|
| 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 |
+
|
| 32 |
+
const fileStream = fs.createReadStream(outputPath);
|
| 33 |
+
fileStream.pipe(res);
|
| 34 |
+
|
| 35 |
+
fileStream.on("close", () => {
|
| 36 |
+
fs.unlinkSync(req.file.path);
|
| 37 |
+
fs.unlinkSync(outputPath);
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
} catch (error) {
|
| 41 |
console.error(error);
|
| 42 |
+
res.status(500).send("Conversion failed");
|
| 43 |
}
|
| 44 |
});
|
| 45 |
|
| 46 |
+
const PORT = process.env.PORT || 7860;
|
| 47 |
+
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|
|
|