-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
106 lines (89 loc) · 3.18 KB
/
server.js
File metadata and controls
106 lines (89 loc) · 3.18 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const express = require('express');
const multer = require('multer');
const path = require('path');
const sharp = require('sharp');
const fs = require('fs-extra');
const app = express();
const port = process.env.PORT || 3000;
// Đảm bảo thư mục uploads tồn tại
const uploadDir = path.join(__dirname, 'public/uploads');
fs.ensureDirSync(uploadDir);
// Cấu hình multer để xử lý upload
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, uploadDir);
},
filename: (req, file, cb) => {
cb(null, Date.now() + path.extname(file.originalname));
}
});
const upload = multer({
storage: storage,
fileFilter: (req, file, cb) => {
const filetypes = /jpeg|jpg|png/;
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
const mimetype = filetypes.test(file.mimetype);
if (mimetype && extname) {
return cb(null, true);
}
cb(new Error('Chỉ chấp nhận file ảnh (jpg, jpeg, png)!'));
}
});
// Cấu hình middleware
app.use(express.static('public'));
app.use(express.json());
app.set('view engine', 'ejs');
// Routes
app.get('/', (req, res) => {
res.render('index');
});
app.post('/upload', upload.single('image'), async (req, res) => {
try {
if (!req.file) {
return res.status(400).json({ error: 'Không có file được tải lên' });
}
const imageInfo = await sharp(req.file.path).metadata();
res.json({
filename: req.file.filename,
originalWidth: imageInfo.width,
originalHeight: imageInfo.height
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.post('/resize', async (req, res) => {
try {
const { filename, width, height, crop } = req.body;
const inputPath = path.join(__dirname, 'public/uploads', filename);
const outputPath = path.join(__dirname, 'public/uploads', `resized-${filename}`);
let sharpInstance = sharp(inputPath);
// Nếu có thông tin crop, thực hiện crop trước
if (crop) {
// Đảm bảo các giá trị crop là số nguyên dương
const cropOptions = {
left: Math.max(0, Math.round(crop.x)),
top: Math.max(0, Math.round(crop.y)),
width: Math.max(1, Math.round(crop.width)),
height: Math.max(1, Math.round(crop.height))
};
sharpInstance = sharpInstance.extract({
...cropOptions
});
}
await sharpInstance
.resize(Math.max(1, parseInt(width)), Math.max(1, parseInt(height)), {
fit: 'contain',
background: { r: 255, g: 255, b: 255, alpha: 1 }
})
.toFile(outputPath);
res.json({ success: true, resizedImage: `resized-${filename}` });
} catch (error) {
console.error('Resize error:', error);
res.status(500).json({ error: error.message });
}
});
// Khởi động server
app.listen(port, () => {
console.log(`Server đang chạy tại http://localhost:${port}`);
});