Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,94 +1,41 @@
|
|
| 1 |
-
import express from
|
| 2 |
-
import multer from
|
| 3 |
-
import sharp from
|
| 4 |
-
import
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
-
const
|
| 8 |
-
|
| 9 |
-
// Multer with memory storage (no disk needed)
|
| 10 |
-
const upload = multer({ storage: multer.memoryStorage() });
|
| 11 |
-
|
| 12 |
-
app.post('/convert', upload.any(), async (req, res) => {
|
| 13 |
-
console.log('Received POST /convert request'); // Log for debugging in HF logs
|
| 14 |
-
try {
|
| 15 |
-
const files = req.files || [];
|
| 16 |
-
if (files.length === 0) {
|
| 17 |
-
console.log('No files received');
|
| 18 |
-
return res.status(400).json({ error: 'No files uploaded' });
|
| 19 |
-
}
|
| 20 |
-
if (files.length > 10) {
|
| 21 |
-
return res.status(400).json({ error: 'Max 10 files per request' });
|
| 22 |
-
}
|
| 23 |
|
| 24 |
-
|
| 25 |
-
const bgHex = req.body.bg || '#FFFFFF';
|
| 26 |
-
let bgColor = [255, 255, 255]; // Default white
|
| 27 |
try {
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
// Base64 for response
|
| 56 |
-
const base64 = jpegBuffer.toString('base64');
|
| 57 |
-
const outputName = path.basename(file.originalname, path.extname(file.originalname)) + '.jpg';
|
| 58 |
-
|
| 59 |
-
results.push({ filename: outputName, data: base64 });
|
| 60 |
-
} catch (err) {
|
| 61 |
-
console.error(`Error processing ${file.originalname}: ${err.message}`);
|
| 62 |
-
errors.push({ filename: file.originalname, error: err.message });
|
| 63 |
-
}
|
| 64 |
-
}));
|
| 65 |
-
|
| 66 |
-
if (results.length === 0 && errors.length > 0) {
|
| 67 |
-
return res.status(500).json({ error: 'All conversions failed', details: errors });
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
console.log(`Conversion complete: ${results.length} successes, ${errors.length} errors`); // Debug log
|
| 71 |
-
|
| 72 |
-
// If single file, simplify response
|
| 73 |
-
if (results.length === 1 && errors.length === 0) {
|
| 74 |
-
return res.json({ message: 'Conversion successful', file: results[0] });
|
| 75 |
}
|
| 76 |
-
|
| 77 |
-
res.json({
|
| 78 |
-
message: 'Conversions successful',
|
| 79 |
-
files: results,
|
| 80 |
-
errors: errors.length > 0 ? errors : undefined,
|
| 81 |
-
});
|
| 82 |
-
} catch (err) {
|
| 83 |
-
console.error(`Server error: ${err.message}`);
|
| 84 |
-
res.status(500).json({ error: 'Server error' });
|
| 85 |
-
}
|
| 86 |
-
});
|
| 87 |
-
|
| 88 |
-
app.get('/', (req, res) => {
|
| 89 |
-
res.send('Server ready for image conversions!');
|
| 90 |
});
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
});
|
|
|
|
| 1 |
+
import express from "express";
|
| 2 |
+
import multer from "multer";
|
| 3 |
+
import sharp from "sharp";
|
| 4 |
+
import fs from "fs";
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
+
const upload = multer({ dest: "uploads/" });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
app.post("/convert", upload.single("file"), async (req, res) => {
|
|
|
|
|
|
|
| 10 |
try {
|
| 11 |
+
if (!req.file) {
|
| 12 |
+
return res.status(400).send("No file uploaded");
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
const outputPath = "converted.jpg";
|
| 16 |
+
|
| 17 |
+
// Convert to JPG
|
| 18 |
+
await sharp(req.file.path)
|
| 19 |
+
.toFormat("jpeg")
|
| 20 |
+
.toFile(outputPath);
|
| 21 |
+
|
| 22 |
+
// Set correct headers so n8n/Postman downloads file
|
| 23 |
+
res.setHeader("Content-Type", "image/jpeg");
|
| 24 |
+
res.setHeader("Content-Disposition", 'attachment; filename="converted.jpg"');
|
| 25 |
+
|
| 26 |
+
const fileStream = fs.createReadStream(outputPath);
|
| 27 |
+
fileStream.pipe(res);
|
| 28 |
+
|
| 29 |
+
fileStream.on("close", () => {
|
| 30 |
+
fs.unlinkSync(req.file.path);
|
| 31 |
+
fs.unlinkSync(outputPath);
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
} catch (error) {
|
| 35 |
+
console.error(error);
|
| 36 |
+
res.status(500).send("Conversion failed");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
});
|
| 39 |
|
| 40 |
+
const PORT = process.env.PORT || 7860;
|
| 41 |
+
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|
|
|