Multer - get underlying file's content during fileFilter
Asked Answered
S

0

9

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.

Seppuku answered 5/4, 2016 at 23:9 Comment(5)
Yes, that's possible using the MemoryStorage rather than the DiskStorage from Multer. See the docs: github.com/expressjs/multer#memorystorageGnosticism
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.Seppuku
Do you recall if you've found a solution for this issue?Knurl
Same problem hereSycosis
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 - github.com/expressjs/multer/issues/723 It is still in alpha though.Throughout

© 2022 - 2024 — McMap. All rights reserved.