flame/middleware/multer.js

30 lines
688 B
JavaScript
Raw Normal View History

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