All files / nodejs-homework-API/helpers upload.js

53.85% Statements 7/13
0% Branches 0/2
0% Functions 0/3
53.85% Lines 7/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 281x 1x 1x 1x   1x                 1x                       1x  
const multer = require("multer");
const path = require("path");
require("dotenv").config();
const DIR_UPLOAD = path.join(process.cwd(), process.env.DIR_UPLOAD);
 
const storage = multer.diskStorage({
  destination: function (_req, _file, cb) {
    cb(null, DIR_UPLOAD);
  },
  filename: function (_req, file, cb) {
    cb(null, file.originalname);
  },
});
 
const upload = multer({
  storage: storage,
  limits: { fileSize: 2000000 },
  fileFilter: (_req, file, cb) => {
    if (file.mimetype.includes("image")) {
      cb(null, true);
      return;
    }
    cb(null, false);
  },
});
 
module.exports = upload;