8

I'm trying to make sure the uploaded file is an image, while using multer to handle the file upload.

The fileFilter function takes the form of the following:

  var fileType = require('file-type');

  fileFilter: function(req, file, cb){
    // Check filetype based on file content
    var filetype = fileType(// File buffer here);
    // Callback - file OK
    cb(null, true)
  }

The problem is file only contains the following:

  { fieldname: 'photo',
  originalname: 'photo.png',
  encoding: '7bit',
  mimetype: 'image/png' }

Is there a way to actually get the file's content (Buffer) in the fileFilter function so that I can inspect the file's content to determine its type?

edit: If this can be done using busboy (the module multer uses), I'd be open to a solution that involves that.

Ash
  • 6,483
  • 7
  • 29
  • 37
  • 2
    Yes, that's possible using the *MemoryStorage* rather than the *DiskStorage* from Multer. See the docs: https://github.com/expressjs/multer#memorystorage – Lars de Bruijn Apr 06 '16 at 06:50
  • I've had a look at that, my only concern is that it might be similar to the memory-store (session store), as in - not production ready and doesn't scale well. My understanding is that it's used as more of a development tool that something widely used. – Ash Apr 06 '16 at 14:56
  • Do you recall if you've found a solution for this issue? – Sérgio Reis Jan 10 '19 at 17:31
  • Same problem here – leoschet Jul 05 '19 at 15:15
  • I wanted to achieve exactly the same but unfortunately I couldn't find a clean way of doing it in 1.x, but should be possible with 2.x as it provides a stream based API - https://github.com/expressjs/multer/issues/723 It is still in alpha though. – Ritesh Chandnani Nov 09 '19 at 23:31

0 Answers0