如何在输入类型字段验证失败时停止上传图像?

时间:2017-09-25 11:54:09

标签: node.js sequelize.js multer

如果在访问Web服务后验证失败,则上载映像。我需要控制它。如果验证成功,则应上传图像。我已经在sequelize创建的模型中定义了验证代码。如果验证成功,如何防止它并上传图像?

模型代码是

name : {
        type : sequelize.STRING,
        validate : {
            notEmpty : {msg : 'The Name is required!'}
        }
    },
    email : {
        type : sequelize.STRING,
        validate : {
            notEmpty : {msg : 'The email field is required!'},
            isEmail : {msg : 'The email format is not correct!'}
        }
    },

上传图片的控制器代码是

var storage = multer.diskStorage({
    destination : function(req,file,cb){
        cb(null,'public/uploads/profile/');
    },
    filename : function(req,file,cb){
        if(!file.originalname.match(/\.(png|jpg|jpeg|gif|bmp)$/)){
            var err = new Error();
            err.code = 'filetype';
            return cb(err);
        }else{
            var fileExtention = mime.extension(file.mimetype);
            var uniqueFileName = uuidv1();
            var uniqueImageName = uniqueFileName + '.' + fileExtention;
            cb(null,uniqueImageName);
        }
    }
});
var upload = multer({
    storage : storage,
    limits : {fileSize : 10000000}
}).single('image');


EmployeeController.prototype.addEmployee = function(req,res){
    upload(req,res,function(err){
        if(err){
            console.log(err);
            if(err.code === 'LIMIT_FILE_SIZE'){
                 res.send({msg:'File sizee is too large. Max limit is 10mb!',status:'Failure',response:200,data:[]});
            }else if(err.code === 'filetype'){
                 res.send({msg:'File type is invalid!',status:'Failure',response:200,data:[]});
            }else{
                res.send({msg:'File was not able to uploaded!',status:'Failure',response:200,data:[]});
            }
        }else{
                if(typeof req.file === 'undefined'){
                    console.log('No Image Found');
                    var imageName = '';
                }else{
                    console.log('Image Found!');
                    var imageName = '/public/uploads/profile/'+req.file.filename;
                }
                async.waterfall([
                    function(callback){
                        var employeeCode = 'EMP1';
                        var employeeCodeObj = Employee.max('id').then(max => {
                            var maxid = 1;
                            if(max){
                                maxid = max + 1;
                            }
                            employeeCode = "EMP"+maxid;
                            callback(null,employeeCode);
                        }).catch(err=>{
                            callback(err,'Failure',500,'Unable to generate employee code!',[]);
                        });
                    },
                    function(employeeCode,callback){
                        var password = randomstring.generate({
                            length: 8,
                            charset: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*'
                        });
                        callback(null,employeeCode,password);
                    },
                    function(employeeCode,password,callback){
                        var params = req.body;
                        var name = Helper.trimInputValue(params.name);
                        var email = Helper.trimInputValue(params.email);
                        var image = imageName;
                        var dob = Helper.trimInputValue(params.dob);
                        Employee.findOrCreate({
                            where : {
                                email : email,
                            },
                            defaults : {
                                name : name,
                                email : email,
                                image : image
                            }
                        }).spread(function(employeeData,created){
                            console.log('Success');
                        }).catch(sequelize.ValidationError,function(err){
                            var data = err.errors;
                            var dataErrors = Array();
                            if(data.length){
                                for(var key in data){
                                    dataErrors.push({
                                        'key' : data[key].path,
                                        'msg' : data[key].message
                                    });
                                }
                            }
                            callback('error','Failure',200,dataErrors,[]);
                        })
                    }
                ],function(error,status,response,msg,data){
                    return res.send({msg:msg,status:status,response:response,data:data});                    
                });
        }
    });
};

0 个答案:

没有答案