feat: add city avatar upload functionality and enhance city management features
This commit is contained in:
30
backend/src/middleware/cityUpload.js
Normal file
30
backend/src/middleware/cityUpload.js
Normal file
@@ -0,0 +1,30 @@
|
||||
const multer = require('multer');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
const uploadDir = path.join(__dirname, '../../public/uploads/cities');
|
||||
if (!fs.existsSync(uploadDir)) fs.mkdirSync(uploadDir, { recursive: true });
|
||||
|
||||
const allowedExtensions = new Set(['.jpg', '.jpeg', '.png', '.webp']);
|
||||
const allowedMimeTypes = new Set(['image/jpeg', 'image/png', 'image/webp']);
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: (_req, _file, callback) => callback(null, uploadDir),
|
||||
filename: (req, file, callback) => {
|
||||
const cityId = Number(req.params.cityId);
|
||||
const extension = path.extname(file.originalname).toLowerCase();
|
||||
callback(null, `city-${cityId}-${Date.now()}${extension}`);
|
||||
}
|
||||
});
|
||||
|
||||
function fileFilter(_req, file, callback) {
|
||||
const extension = path.extname(file.originalname).toLowerCase();
|
||||
if (allowedExtensions.has(extension) && allowedMimeTypes.has(file.mimetype)) return callback(null, true);
|
||||
return callback(new Error('Only JPEG, PNG, and WEBP images are allowed'));
|
||||
}
|
||||
|
||||
module.exports = multer({
|
||||
storage,
|
||||
limits: { fileSize: 5 * 1024 * 1024 },
|
||||
fileFilter
|
||||
});
|
||||
Reference in New Issue
Block a user