upload.js 590 B

123456789101112131415161718192021222324
  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 upload = multer({
  14. storage: storage,
  15. limits: { fileSize: 2000000 },
  16. fileFilter: (_req, file, cb) => {
  17. if (file.mimetype.includes("image")) return cb(null, true);
  18. cb(null, false);
  19. },
  20. });
  21. module.exports = upload;