Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,47 +1,48 @@
|
|
| 1 |
-
import express from
|
| 2 |
-
import multer from
|
| 3 |
-
import sharp from
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
|
| 7 |
const app = express();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
const upload = multer({ dest: "/tmp/uploads" });
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
try {
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
// Convert to JPG
|
| 24 |
-
await sharp(req.file.path)
|
| 25 |
-
.toFormat("jpeg")
|
| 26 |
.toFile(outputPath);
|
| 27 |
|
| 28 |
-
// Send
|
| 29 |
-
res.setHeader(
|
| 30 |
-
res.
|
| 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).
|
| 43 |
}
|
| 44 |
});
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
| 1 |
+
import express from 'express';
|
| 2 |
+
import multer from 'multer';
|
| 3 |
+
import sharp from 'sharp';
|
| 4 |
+
import path from 'path';
|
| 5 |
+
import fs from 'fs';
|
| 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 |
+
const upload = multer({ storage });
|
|
|
|
| 23 |
|
| 24 |
+
// Handle POST upload
|
| 25 |
+
app.post('/convert', upload.single('file'), async (req, res) => {
|
| 26 |
try {
|
| 27 |
+
const originalFile = req.file;
|
| 28 |
+
const ext = path.extname(originalFile.originalname);
|
| 29 |
+
const nameWithoutExt = path.basename(originalFile.originalname, ext);
|
| 30 |
+
const outputPath = path.join(uploadFolder, `${nameWithoutExt}.png`);
|
| 31 |
+
|
| 32 |
+
// Convert to PNG (lossless)
|
| 33 |
+
await sharp(originalFile.path)
|
| 34 |
+
.png({ compressionLevel: 0 })
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
.toFile(outputPath);
|
| 36 |
|
| 37 |
+
// Send back as file
|
| 38 |
+
res.setHeader('Content-Disposition', `attachment; filename="${nameWithoutExt}.png"`);
|
| 39 |
+
res.sendFile(outputPath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
} catch (error) {
|
| 41 |
console.error(error);
|
| 42 |
+
res.status(500).json({ error: 'Conversion failed' });
|
| 43 |
}
|
| 44 |
});
|
| 45 |
|
| 46 |
+
app.listen(port, () => {
|
| 47 |
+
console.log(`✅ Server running on port ${port}`);
|
| 48 |
+
});
|