2021-06-23 15:15:14 +03:00
|
|
|
const fs = require('fs');
|
|
|
|
const multer = require('multer');
|
2021-06-24 13:53:45 +03:00
|
|
|
|
|
|
|
if (!fs.existsSync('data/uploads')) {
|
2021-06-25 11:42:44 +03:00
|
|
|
fs.mkdirSync('data/uploads', { recursive: true });
|
2021-06-24 13:53:45 +03:00
|
|
|
}
|
2021-06-23 15:15:14 +03:00
|
|
|
|
|
|
|
const storage = multer.diskStorage({
|
|
|
|
destination: (req, file, cb) => {
|
|
|
|
cb(null, './data/uploads');
|
|
|
|
},
|
|
|
|
filename: (req, file, cb) => {
|
|
|
|
cb(null, Date.now() + '--' + file.originalname);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const supportedTypes = ['jpg', 'jpeg', 'png'];
|
|
|
|
|
|
|
|
const fileFilter = (req, file, cb) => {
|
|
|
|
if (supportedTypes.includes(file.mimetype.split('/')[1])) {
|
|
|
|
cb(null, true);
|
|
|
|
} else {
|
|
|
|
cb(null, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const upload = multer({ storage, fileFilter });
|
|
|
|
|
|
|
|
module.exports = upload.single('icon');
|