index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. var makeMiddleware = require('./lib/make-middleware')
  2. var diskStorage = require('./storage/disk')
  3. var memoryStorage = require('./storage/memory')
  4. var MulterError = require('./lib/multer-error')
  5. function allowAll (req, file, cb) {
  6. cb(null, true)
  7. }
  8. function Multer (options) {
  9. if (options.storage) {
  10. this.storage = options.storage
  11. } else if (options.dest) {
  12. this.storage = diskStorage({ destination: options.dest })
  13. } else {
  14. this.storage = memoryStorage()
  15. }
  16. this.limits = options.limits
  17. this.preservePath = options.preservePath
  18. this.fileFilter = options.fileFilter || allowAll
  19. }
  20. Multer.prototype._makeMiddleware = function (fields, fileStrategy) {
  21. function setup () {
  22. var fileFilter = this.fileFilter
  23. var filesLeft = Object.create(null)
  24. fields.forEach(function (field) {
  25. if (typeof field.maxCount === 'number') {
  26. filesLeft[field.name] = field.maxCount
  27. } else {
  28. filesLeft[field.name] = Infinity
  29. }
  30. })
  31. function wrappedFileFilter (req, file, cb) {
  32. if ((filesLeft[file.fieldname] || 0) <= 0) {
  33. return cb(new MulterError('LIMIT_UNEXPECTED_FILE', file.fieldname))
  34. }
  35. filesLeft[file.fieldname] -= 1
  36. fileFilter(req, file, cb)
  37. }
  38. return {
  39. limits: this.limits,
  40. preservePath: this.preservePath,
  41. storage: this.storage,
  42. fileFilter: wrappedFileFilter,
  43. fileStrategy: fileStrategy
  44. }
  45. }
  46. return makeMiddleware(setup.bind(this))
  47. }
  48. Multer.prototype.single = function (name) {
  49. return this._makeMiddleware([{ name: name, maxCount: 1 }], 'VALUE')
  50. }
  51. Multer.prototype.array = function (name, maxCount) {
  52. return this._makeMiddleware([{ name: name, maxCount: maxCount }], 'ARRAY')
  53. }
  54. Multer.prototype.fields = function (fields) {
  55. return this._makeMiddleware(fields, 'OBJECT')
  56. }
  57. Multer.prototype.none = function () {
  58. return this._makeMiddleware([], 'NONE')
  59. }
  60. Multer.prototype.any = function () {
  61. function setup () {
  62. return {
  63. limits: this.limits,
  64. preservePath: this.preservePath,
  65. storage: this.storage,
  66. fileFilter: this.fileFilter,
  67. fileStrategy: 'ARRAY'
  68. }
  69. }
  70. return makeMiddleware(setup.bind(this))
  71. }
  72. function multer (options) {
  73. if (options === undefined) {
  74. return new Multer({})
  75. }
  76. if (typeof options === 'object' && options !== null) {
  77. return new Multer(options)
  78. }
  79. throw new TypeError('Expected object for argument options')
  80. }
  81. module.exports = multer
  82. module.exports.diskStorage = diskStorage
  83. module.exports.memoryStorage = memoryStorage
  84. module.exports.MulterError = MulterError