multer-orm 1.0.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +17 -17
- package/README.md +347 -347
- package/index.js +104 -104
- package/lib/counter.js +28 -28
- package/lib/feature.js +1 -0
- package/lib/file-appender.js +67 -67
- package/lib/make-middleware.js +194 -194
- package/lib/multer-error.js +24 -24
- package/lib/remove-uploaded-files.js +28 -28
- package/package.json +57 -57
- package/storage/disk.js +66 -66
- package/storage/memory.js +21 -21
package/storage/disk.js
CHANGED
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
var fs = require('fs')
|
|
2
|
-
var os = require('os')
|
|
3
|
-
var path = require('path')
|
|
4
|
-
var crypto = require('crypto')
|
|
5
|
-
var mkdirp = require('mkdirp')
|
|
6
|
-
|
|
7
|
-
function getFilename (req, file, cb) {
|
|
8
|
-
crypto.randomBytes(16, function (err, raw) {
|
|
9
|
-
cb(err, err ? undefined : raw.toString('hex'))
|
|
10
|
-
})
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function getDestination (req, file, cb) {
|
|
14
|
-
cb(null, os.tmpdir())
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function DiskStorage (opts) {
|
|
18
|
-
this.getFilename = (opts.filename || getFilename)
|
|
19
|
-
|
|
20
|
-
if (typeof opts.destination === 'string') {
|
|
21
|
-
mkdirp.sync(opts.destination)
|
|
22
|
-
this.getDestination = function ($0, $1, cb) { cb(null, opts.destination) }
|
|
23
|
-
} else {
|
|
24
|
-
this.getDestination = (opts.destination || getDestination)
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
DiskStorage.prototype._handleFile = function _handleFile (req, file, cb) {
|
|
29
|
-
var that = this
|
|
30
|
-
|
|
31
|
-
that.getDestination(req, file, function (err, destination) {
|
|
32
|
-
if (err) return cb(err)
|
|
33
|
-
|
|
34
|
-
that.getFilename(req, file, function (err, filename) {
|
|
35
|
-
if (err) return cb(err)
|
|
36
|
-
|
|
37
|
-
var finalPath = path.join(destination, filename)
|
|
38
|
-
var outStream = fs.createWriteStream(finalPath)
|
|
39
|
-
|
|
40
|
-
file.stream.pipe(outStream)
|
|
41
|
-
outStream.on('error', cb)
|
|
42
|
-
outStream.on('finish', function () {
|
|
43
|
-
cb(null, {
|
|
44
|
-
destination: destination,
|
|
45
|
-
filename: filename,
|
|
46
|
-
path: finalPath,
|
|
47
|
-
size: outStream.bytesWritten
|
|
48
|
-
})
|
|
49
|
-
})
|
|
50
|
-
})
|
|
51
|
-
})
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
DiskStorage.prototype._removeFile = function _removeFile (req, file, cb) {
|
|
55
|
-
var path = file.path
|
|
56
|
-
|
|
57
|
-
delete file.destination
|
|
58
|
-
delete file.filename
|
|
59
|
-
delete file.path
|
|
60
|
-
|
|
61
|
-
fs.unlink(path, cb)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
module.exports = function (opts) {
|
|
65
|
-
return new DiskStorage(opts)
|
|
66
|
-
}
|
|
1
|
+
var fs = require('fs')
|
|
2
|
+
var os = require('os')
|
|
3
|
+
var path = require('path')
|
|
4
|
+
var crypto = require('crypto')
|
|
5
|
+
var mkdirp = require('mkdirp')
|
|
6
|
+
|
|
7
|
+
function getFilename (req, file, cb) {
|
|
8
|
+
crypto.randomBytes(16, function (err, raw) {
|
|
9
|
+
cb(err, err ? undefined : raw.toString('hex'))
|
|
10
|
+
})
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function getDestination (req, file, cb) {
|
|
14
|
+
cb(null, os.tmpdir())
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function DiskStorage (opts) {
|
|
18
|
+
this.getFilename = (opts.filename || getFilename)
|
|
19
|
+
|
|
20
|
+
if (typeof opts.destination === 'string') {
|
|
21
|
+
mkdirp.sync(opts.destination)
|
|
22
|
+
this.getDestination = function ($0, $1, cb) { cb(null, opts.destination) }
|
|
23
|
+
} else {
|
|
24
|
+
this.getDestination = (opts.destination || getDestination)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
DiskStorage.prototype._handleFile = function _handleFile (req, file, cb) {
|
|
29
|
+
var that = this
|
|
30
|
+
|
|
31
|
+
that.getDestination(req, file, function (err, destination) {
|
|
32
|
+
if (err) return cb(err)
|
|
33
|
+
|
|
34
|
+
that.getFilename(req, file, function (err, filename) {
|
|
35
|
+
if (err) return cb(err)
|
|
36
|
+
|
|
37
|
+
var finalPath = path.join(destination, filename)
|
|
38
|
+
var outStream = fs.createWriteStream(finalPath)
|
|
39
|
+
|
|
40
|
+
file.stream.pipe(outStream)
|
|
41
|
+
outStream.on('error', cb)
|
|
42
|
+
outStream.on('finish', function () {
|
|
43
|
+
cb(null, {
|
|
44
|
+
destination: destination,
|
|
45
|
+
filename: filename,
|
|
46
|
+
path: finalPath,
|
|
47
|
+
size: outStream.bytesWritten
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
DiskStorage.prototype._removeFile = function _removeFile (req, file, cb) {
|
|
55
|
+
var path = file.path
|
|
56
|
+
|
|
57
|
+
delete file.destination
|
|
58
|
+
delete file.filename
|
|
59
|
+
delete file.path
|
|
60
|
+
|
|
61
|
+
fs.unlink(path, cb)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = function (opts) {
|
|
65
|
+
return new DiskStorage(opts)
|
|
66
|
+
}
|
package/storage/memory.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
var concat = require('concat-stream')
|
|
2
|
-
|
|
3
|
-
function MemoryStorage (opts) {}
|
|
4
|
-
|
|
5
|
-
MemoryStorage.prototype._handleFile = function _handleFile (req, file, cb) {
|
|
6
|
-
file.stream.pipe(concat({ encoding: 'buffer' }, function (data) {
|
|
7
|
-
cb(null, {
|
|
8
|
-
buffer: data,
|
|
9
|
-
size: data.length
|
|
10
|
-
})
|
|
11
|
-
}))
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
MemoryStorage.prototype._removeFile = function _removeFile (req, file, cb) {
|
|
15
|
-
delete file.buffer
|
|
16
|
-
cb(null)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
module.exports = function (opts) {
|
|
20
|
-
return new MemoryStorage(opts)
|
|
21
|
-
}
|
|
1
|
+
var concat = require('concat-stream')
|
|
2
|
+
|
|
3
|
+
function MemoryStorage (opts) {}
|
|
4
|
+
|
|
5
|
+
MemoryStorage.prototype._handleFile = function _handleFile (req, file, cb) {
|
|
6
|
+
file.stream.pipe(concat({ encoding: 'buffer' }, function (data) {
|
|
7
|
+
cb(null, {
|
|
8
|
+
buffer: data,
|
|
9
|
+
size: data.length
|
|
10
|
+
})
|
|
11
|
+
}))
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
MemoryStorage.prototype._removeFile = function _removeFile (req, file, cb) {
|
|
15
|
+
delete file.buffer
|
|
16
|
+
cb(null)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = function (opts) {
|
|
20
|
+
return new MemoryStorage(opts)
|
|
21
|
+
}
|