sam12345324 commited on
Commit
720516a
·
verified ·
1 Parent(s): bee570c

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +24 -24
server.js CHANGED
@@ -1,35 +1,35 @@
1
- // server.js
2
- import express from "express";
3
- import multer from "multer";
4
- import fs from "fs";
5
- import path from "path";
6
 
7
  const app = express();
8
- const upload = multer({ dest: "uploads/" });
9
 
10
- // POST route to handle multiple images from n8n
11
- app.post("/upload", upload.array("files", 20), (req, res) => {
12
- if (!req.files || req.files.length === 0) {
13
- return res.status(400).json({ error: "No files uploaded" });
14
- }
15
 
16
- console.log(`Received ${req.files.length} files from n8n`);
 
 
 
17
 
18
- // Example: save and log file names
19
- req.files.forEach(file => {
20
- const newPath = path.join("uploads", file.originalname);
21
- fs.renameSync(file.path, newPath);
22
- });
23
 
24
  res.json({
25
- message: "Files uploaded successfully",
26
- files: req.files.map(f => f.originalname)
27
  });
 
 
 
 
28
  });
29
 
30
- app.get("/", (req, res) => {
31
- res.send("🚀 Hugging Face Server is running!");
32
  });
33
-
34
- const PORT = process.env.PORT || 7860;
35
- 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
 
5
  const app = express();
6
+ const upload = multer({ storage: multer.memoryStorage() }); // store in memory
7
 
8
+ app.post('/convert', upload.array('files', 10), async (req, res) => {
9
+ try {
10
+ const results = [];
 
 
11
 
12
+ for (const file of req.files) {
13
+ const convertedBuffer = await sharp(file.buffer)
14
+ .jpeg({ quality: 90 })
15
+ .toBuffer();
16
 
17
+ results.push({
18
+ fileName: file.originalname.replace(/\.[^/.]+$/, '') + '.jpg',
19
+ file: convertedBuffer.toString('base64')
20
+ });
21
+ }
22
 
23
  res.json({
24
+ message: 'Conversion successful',
25
+ files: results
26
  });
27
+ } catch (error) {
28
+ console.error(error);
29
+ res.status(500).json({ error: 'Conversion failed' });
30
+ }
31
  });
32
 
33
+ app.listen(7860, () => {
34
+ console.log('Server running on port 7860');
35
  });