Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Files uploading but at zero bytes size #161

Closed
MorenoMdz opened this issue Aug 30, 2018 · 1 comment
Closed

Files uploading but at zero bytes size #161

MorenoMdz opened this issue Aug 30, 2018 · 1 comment

Comments

@MorenoMdz
Copy link

MorenoMdz commented Aug 30, 2018

const multerOptions = multer({
  storage: imager({
    accessKeyId: process.env.AWS_ACCESS,
    secretAccessKey: process.env.AWS_SECRET_KEY,
    bucket: process.env.AWS_BUCKET,
    region: process.env.AWSregion,
    /*acl: 'public-read',
    // Auto detect contet type
    contentType: multerS3.AUTO_CONTENT_TYPE, */
    dirname: 'images',
    filename: function(req, file, cb) {
      const extension = file.mimetype.split('/')[1]; // gets the extension
      const area = 'test_';
      const fileName = `${area}${uuid.v4()}.${extension}`;
      console.log('file name is ', fileName);
      cb(null, fileName);
    },
    gm: {
      pool: 5,
      format: 'png',
      scale: {
        width: 200,
        height: 200,
        type: 'contain',
      },
      crop: {
        width: 200,
        height: 200,
        x: 0,
        y: 0,
      },
      rotate: 'auto',
    },
   **// This part is not being executed.**
    fileFilter(req, file, next) {
      const isPhoto = file.mimetype.startsWith('image/');
      console.log(req.files);
      if (isPhoto) {
        next(null, true); // null for error means it worked and it is fine to continue to next()
        console.log(isPhoto);
      } else {
        next({ message: 'Fotos: Tipo de arquivo não suportado!' }, false); // with error
      }
    },
    s3: {
      Metadata: {
        ACL: 'public-read',
        customkey: 'data',
      },
    },
  }),
});

exports.upload = multer(multerOptions).array('photos', 5);

Files are going to S3 with the right name but at zero bytes, the commented part is not getting executed either. Ideas?

@Alexandre-io
Copy link
Owner

fileFilter must be at the same level as storage (https://github.com/expressjs/multer#multeropts) :

const upload = multer({
    storage: multerS3Storage,
    fileFilter: fileFilter
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants