Skip to content

Commit 42a9e3b

Browse files
committed
Switch to stream based API
1 parent d437a4a commit 42a9e3b

31 files changed

Lines changed: 672 additions & 1339 deletions

.travis.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- "0.10"
5-
- "0.12"
6-
- "iojs-v1.8.4"
7-
- "iojs-v2.5.0"
8-
- "iojs-v3.3.0"
9-
- "4"
4+
- '6'
5+
- '4'
6+
- '0.12'

README.md

Lines changed: 17 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on top of [busboy](https://github.com/mscdex/busboy) for maximum efficiency.
88
## Installation
99

1010
```sh
11-
$ npm install --save multer
11+
npm install --save multer
1212
```
1313

1414
## Usage
@@ -18,11 +18,11 @@ Multer adds a `body` object and a `file` or `files` object to the `request` obje
1818
Basic usage example:
1919

2020
```javascript
21+
var multer = require('multer')
2122
var express = require('express')
22-
var multer = require('multer')
23-
var upload = multer({ dest: 'uploads/' })
2423

2524
var app = express()
25+
var upload = multer()
2626

2727
app.post('/profile', upload.single('avatar'), function (req, res, next) {
2828
// req.file is the `avatar` file
@@ -46,15 +46,16 @@ app.post('/cool-profile', cpUpload, function (req, res, next) {
4646
})
4747
```
4848

49-
In case you need to handle a text-only multipart form, you can use any of the multer methods (`.single()`, `.array()`, `fields()`). Here is an example using `.array()`:
49+
In case you need to handle a text-only multipart form, you can use the `.none()` method, example:
5050

5151
```javascript
52+
var multer = require('multer')
5253
var express = require('express')
54+
5355
var app = express()
54-
var multer = require('multer')
5556
var upload = multer()
5657

57-
app.post('/profile', upload.array(), function (req, res, next) {
58+
app.post('/profile', upload.none(), function (req, res, next) {
5859
// req.body contains the text fields
5960
})
6061
```
@@ -65,45 +66,21 @@ app.post('/profile', upload.array(), function (req, res, next) {
6566

6667
Each file contains the following information:
6768

68-
Key | Description | Note
69-
--- | --- | ---
70-
`fieldname` | Field name specified in the form |
71-
`originalname` | Name of the file on the user's computer |
72-
`encoding` | Encoding type of the file |
73-
`mimetype` | Mime type of the file |
74-
`size` | Size of the file in bytes |
75-
`destination` | The folder to which the file has been saved | `DiskStorage`
76-
`filename` | The name of the file within the `destination` | `DiskStorage`
77-
`path` | The full path to the uploaded file | `DiskStorage`
78-
`buffer` | A `Buffer` of the entire file | `MemoryStorage`
79-
80-
### `multer(opts)`
81-
82-
Multer accepts an options object, the most basic of which is the `dest`
83-
property, which tells Multer where to upload the files. In case you omit the
84-
options object, the files will be kept in memory and never written to disk.
85-
86-
By default, Multer will rename the files so as to avoid naming conflicts. The
87-
renaming function can be customized according to your needs.
88-
89-
The following are the options that can be passed to Multer.
90-
9169
Key | Description
9270
--- | ---
93-
`dest` or `storage` | Where to store the files
94-
`fileFilter` | Function to control which files are accepted
95-
`limits` | Limits of the uploaded data
71+
`fieldName` | Field name specified in the form
72+
`originalName` | Name of the file on the user's computer
73+
`size` | Size of the file in bytes
74+
`stream` | Stream of file
9675

97-
In an average web app, only `dest` might be required, and configured as shown in
98-
the following example.
76+
### `multer(opts)`
9977

100-
```javascript
101-
var upload = multer({ dest: 'uploads/' })
102-
```
78+
Multer accepts an options object, the following are the options that can be
79+
passed to Multer.
10380

104-
If you want more control over your uploads, you'll want to use the `storage`
105-
option instead of `dest`. Multer ships with storage engines `DiskStorage`
106-
and `MemoryStorage`; More engines are available from third parties.
81+
Key | Description
82+
-------- | -----------
83+
`limits` | Limits of the uploaded data [(full description)](#limits)
10784

10885
#### `.single(fieldname)`
10986

@@ -146,67 +123,6 @@ Never add multer as a global middleware since a malicious user could upload
146123
files to a route that you didn't anticipate. Only use this function on routes
147124
where you are handling the uploaded files.
148125

149-
### `storage`
150-
151-
#### `DiskStorage`
152-
153-
The disk storage engine gives you full control on storing files to disk.
154-
155-
```javascript
156-
var storage = multer.diskStorage({
157-
destination: function (req, file, cb) {
158-
cb(null, '/tmp/my-uploads')
159-
},
160-
filename: function (req, file, cb) {
161-
cb(null, file.fieldname + '-' + Date.now())
162-
}
163-
})
164-
165-
var upload = multer({ storage: storage })
166-
```
167-
168-
There are two options available, `destination` and `filename`. They are both
169-
functions that determine where the file should be stored.
170-
171-
`destination` is used to determine within which folder the uploaded files should
172-
be stored. This can also be given as a `string` (e.g. `'/tmp/uploads'`). If no
173-
`destination` is given, the operating system's default directory for temporary
174-
files is used.
175-
176-
**Note:** You are responsible for creating the directory when providing
177-
`destination` as a function. When passing a string, multer will make sure that
178-
the directory is created for you.
179-
180-
`filename` is used to determine what the file should be named inside the folder.
181-
If no `filename` is given, each file will be given a random name that doesn't
182-
include any file extension.
183-
184-
**Note:** Multer will not append any file extension for you, your function
185-
should return a filename complete with an file extension.
186-
187-
Each function gets passed both the request (`req`) and some information about
188-
the file (`file`) to aid with the decision.
189-
190-
Note that `req.body` might not have been fully populated yet. It depends on the
191-
order that the client transmits fields and files to the server.
192-
193-
#### `MemoryStorage`
194-
195-
The memory storage engine stores the files in memory as `Buffer` objects. It
196-
doesn't have any options.
197-
198-
```javascript
199-
var storage = multer.memoryStorage()
200-
var upload = multer({ storage: storage })
201-
```
202-
203-
When using memory storage, the file info will contain a field called
204-
`buffer` that contains the entire file.
205-
206-
**WARNING**: Uploading very large files, or relatively small files in large
207-
numbers very quickly, can cause your application to run out of memory when
208-
memory storage is used.
209-
210126
### `limits`
211127

212128
An object specifying the size limits of the following optional properties. Multer passes this object into busboy directly, and the details of the properties can be found on [busboy's page](https://github.com/mscdex/busboy#busboy-methods).
@@ -225,29 +141,6 @@ Key | Description | Default
225141

226142
Specifying the limits can help protect your site against denial of service (DoS) attacks.
227143

228-
### `fileFilter`
229-
230-
Set this to a function to control which files should be uploaded and which
231-
should be skipped. The function should look like this:
232-
233-
```javascript
234-
function fileFilter (req, file, cb) {
235-
236-
// The function should call `cb` with a boolean
237-
// to indicate if the file should be accepted
238-
239-
// To reject this file pass `false`, like so:
240-
cb(null, false)
241-
242-
// To accept the file pass `true`, like so:
243-
cb(null, true)
244-
245-
// You can always pass an error if something goes wrong:
246-
cb(new Error('I don\'t have a clue!'))
247-
248-
}
249-
```
250-
251144
## Error handling
252145

253146
When encountering an error, multer will delegate the error to express. You can
@@ -270,12 +163,3 @@ app.post('/profile', function (req, res) {
270163
})
271164
})
272165
```
273-
274-
## Custom storage engine
275-
276-
See [the documentation here](/StorageEngine.md) if you want to build your own
277-
storage engine.
278-
279-
## License
280-
281-
[MIT](LICENSE)

StorageEngine.md

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)