Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion StorageEngine.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ function getDestination (req, file, cb) {
}

function MyCustomStorage (opts) {
this.getDestination = (opts.destination || getDestination)
opts = opts || {}

if (typeof opts.destination === 'string') {
this.getDestination = function ($0, $1, cb) { cb(null, opts.destination) }
} else {
this.getDestination = (opts.destination || getDestination)
}
}

MyCustomStorage.prototype._handleFile = function _handleFile (req, file, cb) {
Expand Down
2 changes: 2 additions & 0 deletions storage/disk.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function getDestination (req, file, cb) {
}

function DiskStorage (opts) {
opts = opts || {}

this.getFilename = (opts.filename || getFilename)
this.flush = opts.flush

Expand Down
21 changes: 20 additions & 1 deletion test/disk-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,26 @@ describe('Disk Storage', function () {
// Verify that the file was stored in the system's temporary directory
assert.strictEqual(path.dirname(req.file.path), os.tmpdir())

done()
fs.unlink(req.file.path, done)
})
})

it('should use default destination and filename when options are omitted', function (done) {
var storage = multer.diskStorage()
var upload = multer({ storage: storage })
var parser = upload.single('file')
var form = new FormData()

form.append('file', util.file('small0.dat'))

util.submitForm(parser, form, (err, req) => {
assert.ifError(err)

// Verify that the file was stored in the system's temporary directory
assert.strictEqual(path.dirname(req.file.path), os.tmpdir())
assert.strictEqual(req.file.size, util.fileSizeByName('small0.dat'))

fs.unlink(req.file.path, done)
})
})

Expand Down