-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
88 lines (69 loc) · 2.64 KB
/
app.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const express = require('express');
const path = require('path');
const multer = require('multer');
const bodyParser = require('body-parser');
const Jimp = require('jimp');
const archiver = require('archiver');
const fs = require('fs');
const app = express();
const port = process.env.PORT || 3000;
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json({ limit: '30mb' }));
app.use(bodyParser.urlencoded({ extended: true, limit: '30mb' }));
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
let processedImages = []; // Store processed images globally
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.post('/upload', upload.array('images', 20), async (req, res) => {
if (!req.files || req.files.length === 0) {
return res.status(400).send('No files uploaded.');
}
try {
processedImages = [];
for (const file of req.files) {
const originalImage = await Jimp.read(file.buffer);
// Create a placeholder image with the same dimensions as the original image
const placeholderImage = new Jimp(originalImage.getWidth(), originalImage.getHeight(), '#cccccc');
// Convert the placeholder image to a buffer
const placeholderBuffer = await placeholderImage.getBufferAsync(Jimp.MIME_PNG);
// Encode the placeholder image buffer to base64
const placeholderBase64 = placeholderBuffer.toString('base64');
// Store the processed image details
processedImages.push({
placeholderBase64: placeholderBase64,
originalSize: {
width: originalImage.getWidth(),
height: originalImage.getHeight(),
},
});
}
// Send the processed images data to the client
res.send(processedImages);
} catch (error) {
console.error('Error processing images:', error);
res.status(500).send('Internal Server Error');
}
});
app.get('/download-all', async (req, res) => {
try {
const archive = archiver('zip', { zlib: { level: 9 } });
// Set content-type to zip
res.header('Content-Type', 'application/zip');
// Set content-disposition to force download
res.attachment('placeholders.zip');
archive.pipe(res);
processedImages.forEach((imageData, index) => {
const buffer = Buffer.from(imageData.placeholderBase64, 'base64');
archive.append(buffer, { name: `placeholder_${index + 1}.png` });
});
archive.finalize();
} catch (error) {
console.error('Error creating zip file:', error);
res.status(500).send('Internal Server Error');
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});