-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (61 loc) · 1.89 KB
/
Copy pathserver.js
File metadata and controls
71 lines (61 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const express = require('express');
const multer = require('multer');
const { exec } = require('child_process');
const Queue = require('bull');
const cors = require('cors');
const path = require('path');
const app = express();
const upload = multer({ dest: 'uploads/' });
const imageQueue = new Queue('image processing');
// Configuración del puerto
const PORT = process.env.PORT || 3000;
// Configurar CORS
app.use(cors());
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
// Procesamiento de la cola
imageQueue.process(async (job, done) => {
const { path, options } = job.data;
const output = `uploads/output-${Date.now()}.jpg`; // Nombre del archivo de salida
let command = `magick ${path} ${options} ${output}`;
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return done(new Error(error.message));
}
if (stderr) {
console.error(`Stderr: ${stderr}`);
return done(new Error(stderr));
}
console.log(`Stdout: ${stdout}`);
done(null, output);
});
});
// Endpoint para subir y procesar imágenes
app.post('/upload', upload.array('image', 10), async (req, res) => { // Permitir múltiples archivos
const files = req.files;
const { resize, quality } = req.body;
const processingPromises = files.map(file => {
return new Promise((resolve) => {
let options = '';
if (resize) {
options += `-resize ${resize} `;
}
if (quality) {
options += `-quality ${quality} `;
}
imageQueue.add({
path: file.path,
options: options.trim()
}).then(job => {
job.finished().then(output => {
resolve(output);
});
});
});
});
const results = await Promise.all(processingPromises);
res.json(results);
});
app.listen(PORT, () => {
console.log(`Servidor escuchando en el puerto ${PORT}`);
});