-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (46 loc) · 1.43 KB
/
server.js
File metadata and controls
47 lines (46 loc) · 1.43 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
const express = require('express');
const multer = require('multer');
const sharp = require('sharp');
const path = require('path');
const fs = require('fs');
const app = express();
const upload = multer({ dest: 'uploads/' });
app.use(express.static('public'));
app.post('/upload', upload.single('image'), async (req, res) => {
if (!req.file) {
return res.status(400).json({ error: 'No file uploaded.' });
}
const inputFilePath = req.file.path;
const outputFilePath = `resized/${Date.now()}_${req.file.originalname}`;
try {
await sharp(inputFilePath)
.resize(800, 800, {
fit: sharp.fit.inside,
withoutEnlargement: true
})
.toFile(outputFilePath);
fs.unlinkSync(inputFilePath);
res.json({ file: outputFilePath });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Error processing image.' });
}
});
app.get('/download/:filename', (req, res) => {
const filename = req.params.filename;
const file = path.join(__dirname, 'resized', filename);
res.download(file);
});
const createDirectories = () => {
const directories = ['uploads', 'resized'];
directories.forEach(dir => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
});
};
createDirectories();
const PORT = 4000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});