Storage Engines, fileFilter, mixed behavour in v1/v2 and Docs
- Dominant language
- JavaScript
- Stars
- 12.1k
- Forks
- 1.1k
- Avg merge
- 8d 2h
- Merged PRs (30d)
- 21
Description
*Perhaps this should be 3 separate issues but I'm keeping as one issue for brevity.*
### fileFilter in v2.x
Has the fileFilter function been removed from the v2 release? It is no longer documented in the readme, but its removal hasn't been mentioned in the changelogs.
### fileFilter in v1.4.2
The latest version of `multer` available through npm is 1.4.2. The documentation for the `master` branch and the `1.4.2` tag indicates that it should be possible to use the MemoryStorage engine. When using the engine, a `buffer` property is added to the file object.
However, the buffer is not available on the file object that is being passed as the second argument to fileFilter.
The options:
```TypeScrypt
multerOpts: Multer.Options = {
storage: Multer.memoryStorage(),
fileFilter: this.imageFilter,
/* ... */
}
```
The filter function:
```TypeScript
async imageFilter(req: express.Request, file: any, ffcb: Multer.FileFilterCallback) {
log(req.file)) // => file object, no buffer
log(req.file.buffer)) // => undefined
log(file.buffer)) // => undefined
ffcb(null, true)
}
```
Has this been removed/disabled/broken, or am I doing something wrong? The fileFilter is obviously working up to the point where it logs the file or req.file object. If I use DiskStorage engine, the `file` object gets the appropriate additional fields like filename and path. When using MemoryStorage, these fields are missing as expected, but the buffer is nowhere to be seen. No Stream is available either.
### What is the proper way to use fileFilter?
My intended approach was to take the file buffer and pass it onto `file-type` to check mime, and then reject things with the wrong mime. Without access to the buffer, I cannot do this. If I use DiskStorage, when does fileFilter get triggered? Before or after the file is written to disk? If the latter, what's the point of using that function stead of using a separate middleware?
What do you use to detect mimetypes in v2.x? The docs state that some form of mime detection is going on, but I don't see `file-type` in the v2 dependencies. I believe `formidable` is just inferring from extension (eeeesh).
In any event, is there a way to filter based on mimetype in v2?
In the alternative, what's the dead-simplest, most lightweight parser out there so I can include my own limits/validations/"to keep or not to keep" checks.
Contributor guide
Research direction
Start with the README and changelogs for the v1.4.2 and v2.x behavior, then trace MemoryStorage and fileFilter to establish when filtering runs and which file fields are available. Compare the documented behavior with the reported examples; done should clarify or correct the version-specific behavior and document the supported MIME-filtering approach.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- express, javascript, node.js
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100