upload.js 619 B

12345678910111213141516171819202122232425
  1. const multer = require('multer');
  2. const path = require('path');
  3. require('dotenv').config();
  4. const DIR_UPLOAD = path.join(process.cwd(), process.env.DIR_UPLOAD);
  5. const storage = multer.diskStorage({
  6. destination: function (_req, _file, cb) {
  7. cb(null, DIR_UPLOAD);
  8. },
  9. filename: function (_req, file, cb) {
  10. cb(null, file.originalname);
  11. },
  12. });
  13. const uploadImg = multer({
  14. storage: storage,
  15. limits: { fileSize: 2000000 },
  16. fileFilter: (_req, file, cb) => {
  17. if (file.mimetype === 'image/png' || file.mimetype === 'image/jpeg')
  18. return cb(null, true);
  19. cb(null, false);
  20. },
  21. });
  22. module.exports = { uploadImg };