如何在multer上将函数设置为限制参数?

时间:2016-09-30 08:53:17

标签: node.js file-upload multer

在这个问题之后: CanI upload many files in different folder using multer and a single html form?

我设置了multer参数dinamically并且代码适用于:destination,filename和filter但不在" limit"参数! 有人可以帮帮我吗?

var path = require('path');
var multer = require('multer');
var defaultMaxSize = 3 * 1024 * 1024;
var defaultFiles = 1;

exports.upload = function(obj, next) {
  var upld = {};
  var storage = {};
  var limits = {};
  for (key in obj) {
    switch (key) {
      case 'destination':
        storage.destination = function(request, file, cb) {
          cb(null, obj.destination[file.fieldname]);
        };
        break;
      case 'filename':
        storage.filename = function(request, file, cb) {
          if (obj.filename[file.fieldname] != undefined) {
            nome = obj.filename[file.fieldname];
            if (nome == undefined)
              nome = file.originalname.substring(0, file.originalname.lastIndexOf("."));

            nome += file.originalname.substring(file.originalname.lastIndexOf('.'), file.originalname.length);
          } else nome = file.originalname.split('/').pop().trim();

          cb(null, nome);
        };
        break;
      case 'FieldNameSize':
        limits.fieldNameSize  = function(request, file, cb) {
          if (obj.fieldNameSize[file.fieldname] != undefined)
            cb(null, obj.fieldNameSize[file.fieldname]);
          else return cb(null, true);
        };
        break;
      case 'fieldSize':
        limits.fieldSize = function(request, file, cb) {
          if (obj.fieldSize[file.fieldname] != undefined)
            cb(null, obj.fieldSize[file.fieldname]);
          else return cb(null, true);
        };
        break;
      case 'fields':
        limits.fields  = function(request, file, cb) {
          if (obj.fields[file.fieldname] != undefined)
            cb(null, obj.fields[file.fieldname]);
          else return cb(null, true);
        };
        break;
      case 'fileSize':
        limits.fileSize = function(request, file, cb) {
          if (obj.fileSize[file.fieldname] != undefined)
            cb(null, obj.fileSize[file.fieldname]);
          else return cb(null, true);
        };
        break;
      case 'files':
        limits.files  = function(request, file, cb) {
          if (obj.files[file.fieldname] != undefined)
            cb(null, obj.files[file.fieldname]);
          else return cb(null, true);
        };
        break;
      case 'parts':
        limits.parts  = function(request, file, cb) {
          if (obj.parts[file.fieldname] != undefined)
            cb(null, obj.parts[file.fieldname]);
          else return cb(null, true);
        };
        break;
      case 'filetypes':
        upld.fileFilter = function(req, file, cb) {
          if (obj.filetypes[file.fieldname] != undefined) {
            var f = obj.filetypes[file.fieldname];
            f = f.replace(",", "|");
            var re = new RegExp(f);
            var mimetype = re.test(file.mimetype);
            var extname = re.test(path.extname(file.originalname).toLowerCase());
            if (mimetype && extname) {
              return cb(null, true);
            }
            cb("Sono Accettate solo le seguenti estensioni: " + obj.filetypes[file.fieldname]);
          } else return cb(null, true);
        };
        break;
    }
  }
  if (storage.filename == undefined)
    storage.filename = function(request, file, cb) {
      nome = file.originalname.split('/').pop().trim();
      cb(null, Date.now() + "_" + nome);
    };
  if (limits.fileSize == undefined)
    limits.fileSize = defaultMaxSize;
  /*
  var maxSize=l.maxSize;
              limitsfileSize= function (request, file, cb) {
  if(file.size>(maxSize)) { cb(null,true); }
  else { cb("Il file non può pesare più di"+maxSize+" MB" ); }
              };
        */
  upld.storage = multer.diskStorage(storage);
  upld.limits = limits;


  return multer(upld).any();

};

0 个答案:

没有答案