Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

multer

Package Overview
Dependencies
Maintainers
2
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

multer - npm Package Compare versions

Comparing version 0.1.7 to 0.1.8

28

index.js

@@ -15,2 +15,3 @@ var os = require('os');

options.inMemory = options.inMemory || false;
options.putSingleFilesInArray = options.putSingleFilesInArray || false;

@@ -28,5 +29,10 @@ // if the destination directory does not exist then assign uploads to the operating system's temporary directory

// renaming function for the destination directory
var changeDest = options.changeDest || function(dest, req, res) {
return dest;
};
// renaming function for the uploaded file - need not worry about the extension
// ! if you want to keep the original filename, write a renamer function which does that
var rename = options.rename || function(fieldname, filename) {
var rename = options.rename || function(fieldname, filename, req, res) {
var random_string = fieldname + filename + Date.now() + Math.random();

@@ -85,4 +91,4 @@ return crypto.createHash('md5').update(random_string).digest('hex');

newFilename = rename(fieldname, filename.replace(ext, '')) + ext;
newFilePath = path.join(dest, newFilename);
newFilename = rename(fieldname, filename.replace(ext, ''), req, res) + ext;
newFilePath = path.join(changeDest(dest, req, res), newFilename);

@@ -96,3 +102,3 @@ var file = {

path: newFilePath,
extension: (ext === null) ? null : ext.replace('.', ''),
extension: (ext === '') ? '' : ext.replace('.', ''),
size: 0,

@@ -105,3 +111,3 @@ truncated: null,

if (options.onFileUploadStart) {
var proceed = options.onFileUploadStart(file);
var proceed = options.onFileUploadStart(file, req, res);
// if the onFileUploadStart handler returned null, it means we should proceed further, discard the file!

@@ -128,3 +134,3 @@ if (proceed == false) {

// trigger "file data" event
if (options.onFileUploadData) { options.onFileUploadData(file, data); }
if (options.onFileUploadData) { options.onFileUploadData(file, data, req, res); }
});

@@ -139,3 +145,3 @@

// trigger "file end" event
if (options.onFileUploadComplete) { options.onFileUploadComplete(file); }
if (options.onFileUploadComplete) { options.onFileUploadComplete(file, req, res); }

@@ -199,5 +205,7 @@ // defines has completed processing one more file

for (var field in req.files) {
if (req.files[field].length === 1) {
req.files[field] = req.files[field][0];
if (!options.putSingleFilesInArray) {
for (var field in req.files) {
if (req.files[field].length === 1) {
req.files[field] = req.files[field][0];
}
}

@@ -204,0 +212,0 @@ }

{
"name": "multer",
"description": "Middleware for handling `multipart/form-data`.",
"version": "0.1.7",
"version": "0.1.8",
"contributors": [
"Hage Yaapa <captain@hacksparrow.com> (http://www.hacksparrow.com)"
"Hage Yaapa <captain@hacksparrow.com> (http://www.hacksparrow.com)",
"Jaret Pfluger <https://github.com/jpfluger>"
],

@@ -8,0 +9,0 @@ "license": "MIT",

@@ -59,6 +59,7 @@ # Multer [![Build Status](https://travis-ci.org/expressjs/multer.svg?branch=master)](https://travis-ci.org/expressjs/multer) [![NPM version](https://badge.fury.io/js/multer.svg)](https://badge.fury.io/js/multer)

* `inMemory`
* `rename(fieldname, filename)`
* `onFileUploadStart(file)`
* `onFileUploadData(file, data)`
* `onFileUploadComplete(file)`
* `rename(fieldname, filename, req, res)`
* `changeDest(dest, req, res)`
* `onFileUploadStart(file, req, res)`
* `onFileUploadData(file, data, req, res)`
* `onFileUploadComplete(file, req, res)`
* `onParseStart()`

@@ -123,2 +124,30 @@ * `onParseEnd(req, next)`

### putSingleFilesInArray
**NOTE** In the next major version, `putSingleFilesInArray` will go away and all `req.files` key-value pairs will point to an array of file objects. Begin migrating your code to use `putSingleFilesInArray: true`. This will become the default in the next version. An explanation follows.
In the current version `putSingleFilesInArray` is false. Activate it by setting the property to true.
```js
putSingleFilesInArray: true
```
Some applications or libraries, such as Object Modelers, expect `req.files` key-value pairs to always point to arrays. If `putSingleFilesInArray` is true, multer will ensure all values point to an array.
```js
// the value points to a single file object
req.files['file1'] = [fileObject1]
// the value points to an array of file objects
req.files['file1'] = [fileObject1, fileObject2]
```
Contrast this with Multer's default behavior, where `putSingleFilesInArray` is false. If the value for any key in `req.files` is a single file, then the value will equal a single file object. And if the value points to multiple files, then the value will equal an array of file objects.
```js
// the value points to a single file object
req.files['file1'] = fileObject1
// the value points to an array of file objects
req.files['file1'] = [fileObject1, fileObject2]
```
### inMemory

@@ -132,5 +161,5 @@

**WARNING**: Uploading very large files, or relatively small files in large numbers very quickly, can cause your application to run out of memory when `inMemory` is set to`tue`.
**WARNING**: Uploading very large files, or relatively small files in large numbers very quickly, can cause your application to run out of memory when `inMemory` is set to `true`.
### rename(fieldname, filename)
### rename(fieldname, filename, req, res)

@@ -140,3 +169,3 @@ Function to rename the uploaded files. Whatever the function returns will become the new name of the uploaded file (extension is not included). The `fieldname` and `filename` of the file will be available in this function, use them if you need to.

```js
rename: function (fieldname, filename) {
rename: function (fieldname, filename, req, res) {
return fieldname + filename + Date.now()

@@ -146,8 +175,32 @@ }

### onFileUploadStart(file)
Note that [req.body Warnings](#reqbody-warnings) applies to this function.
Event handler triggered when a file starts to be uploaded. A file object with the following properties are available to this function: `fieldname`, `originalname`, `name`, `encoding`, `mimetype`, `path`, `extension`.
### changeDest(dest, req, res)
Function to rename the directory in which to place uploaded files. The `dest` parameter is the default value originally assigned or passed into multer. The `req` and `res` parameters are also passed into the function because they may contain information (eg session data) needed to create the path (eg get userid from the session).
```js
onFileUploadStart: function (file) {
changeDest: function(dest, req, res) {
return dest + '/user1';
}
```
You might want to check that the subdirectory has been created. Here is a synchronous way to do it. The [mkdirp](https://www.npmjs.com/package/mkdirp) module can be used to automatically create nested child directories.
```js
changeDest: function(dest, req, res) {
dest += '/user1';
if (!fs.existsSync(dest)) fs.mkdirSync(dest);
return dest;
}
```
Note that [req.body Warnings](#reqbody-warnings) applies to this function.
### onFileUploadStart(file, req, res)
Event handler triggered when a file starts to be uploaded. A file object, with the following properties, is available to this function: `fieldname`, `originalname`, `name`, `encoding`, `mimetype`, `path`, and `extension`.
```js
onFileUploadStart: function (file, req, res) {
console.log(file.fieldname + ' is starting ...')

@@ -160,3 +213,3 @@ }

```js
onFileUploadStart: function (file) {
onFileUploadStart: function (file, req, res) {
if (file.originalname == 'virus.exe') return false;

@@ -166,8 +219,10 @@ }

### onFileUploadData(file, data)
Note that [req.body Warnings](#reqbody-warnings) applies to this function.
### onFileUploadData(file, data, req, res)
Event handler triggered when a chunk of buffer is received. A buffer object along with a file object is available to the function.
```js
onFileUploadData: function (file, data) {
onFileUploadData: function (file, data, req, res) {
console.log(data.length + ' of ' + file.fieldname + ' arrived')

@@ -177,8 +232,10 @@ }

### onFileUploadComplete(file)
Note that [req.body Warnings](#reqbody-warnings) applies to this function.
### onFileUploadComplete(file, req, res)
Event handler trigger when a file is completely uploaded. A file object is available to the function.
```js
onFileUploadComplete: function (file) {
onFileUploadComplete: function (file, req, res) {
console.log(file.fieldname + ' uploaded to ' + file.path)

@@ -188,2 +245,4 @@ }

Note that [req.body Warnings](#reqbody-warnings) applies to this function.
### onParseStart()

@@ -269,2 +328,6 @@

## req.body Warnings
**WARNING**: `req.body` is fully parsed after file uploads have finished. Accessing `req.body` prematurely may cause errors. The `req` and `res` parameters are added to some functions to allow the developer to access properties other than `req.body`, such as session variables or socket.io objects. You have been forwarned! :)
## [MIT Licensed](LICENSE)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc