const multer = require("multer");

const upload = multer({
  storage: multer.memoryStorage(),

  limits: {
    fileSize: 20 * 1024 * 1024,
    files: 2
  },

  fileFilter: (req, file, callback) => {
    // The cover image must be PNG.
    if (file.fieldname === "image") {
      if (file.mimetype !== "image/png") {
        return callback(
          new Error("Cover image must be a PNG")
        );
      }

      return callback(null, true);
    }

    // The payload file can be any type.
    if (file.fieldname === "file") {
      return callback(null, true);
    }

    callback(
      new Error(`Unexpected file field: ${file.fieldname}`)
    );
  }
});

module.exports = upload;