sam12345324 commited on
Commit
5c19840
·
verified ·
1 Parent(s): 98a947d

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +36 -35
server.js CHANGED
@@ -1,47 +1,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}`));
 
 
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
+ });