cphill
cphill

Reputation: 5914

Expressjs Multipart Upload Fail but POST Persists

I have an issue where the error statement in the upload() function is being triggered, but not stopping the form submission from being processed and sending a redirect as indicated in the error half of my if statement. What is missing from my .post route that is allowing the remainder of my route methods to run?

Illegal file extension format in the upload variable definition is what is throwing the error.

POST:

.post(function(req, res){
        var s3FilePath = [];

        upload(req, res, function(err){
            if(err){
                req.flash('error', err);
                res.redirect(req.get('referer'));
            }

            var uploadedFiles = req.files;

            //Configure Uploaded S3 File Path strings based on environment for use in DB
            for (var prop in uploadedFiles){
                console.log(uploadedFiles[prop].key);
                if (app.get('env') === 'production' || app.get('env') === 'staging'){
                    s3FilePath = 'https://files.test-site.com/' + uploadedFiles[prop].key;
                } else {
                    s3FilePath.push(uploadedFiles[prop].location);
                }
            }

            models.Blog.create({
                title: req.body.title,
                type: req.body.type,
                userId: req.user.userId     
            }).then(function(blog){
                var files = _.map(s3FilePath, function (file) {
                    return {
                        file: file,
                        blogId: blog.blogId
                    };
                });
                return models.BlogFile.bulkCreate(files); 
            }).then(function(){
                req.flash('info', 'Blog was successfully created.');
                res.redirect('/app');
            });
        });
    });

upload variable definition:

   var upload = multer({
        storage: multerS3({
            s3: s3,
            bucket: options.Bucket,
            contentType: multerS3.AUTO_CONTENT_TYPE,
            acl: options.ACL,
            key: function(req, file, cb){
                var fileNameFormatted = file.originalname.replace(/\s+/g, '-').toLowerCase();
                cb(null, req.user.organizationId + '/' + uploadDate + '/' + fileNameFormatted);
            }
        }),
        fileFilter: function(req, file, cb){
            if(!file.originalname.match(/\.(jpg|jpeg|png|gif|csv|xls|xlsb|xlsm|xlsx)$/)){
                return cb('One of your selected files is not supported', false);
            }
            cb(null, true);
        }
    }).array('fileUpload', 5);

Upvotes: 0

Views: 32

Answers (1)

Remario
Remario

Reputation: 3863

Add a return statement after redirect. remember the script is imperative it will continue executing. or use res.end() to terminate all subsequent request.

  if(err){
            req.flash('error', err);
            res.redirect(req.get('referer'));
            return;
        }

the redirect call does not alter the state flow of your code, it only modifies the route, thus the next line is executed which is var uploadedFiles = req.files; which is illegal and throws a Illegal file extension format.

Upvotes: 1

Related Questions