make-middleware.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. var is = require('type-is')
  2. var Busboy = require('busboy')
  3. var extend = require('xtend')
  4. var onFinished = require('on-finished')
  5. var appendField = require('append-field')
  6. var Counter = require('./counter')
  7. var MulterError = require('./multer-error')
  8. var FileAppender = require('./file-appender')
  9. var removeUploadedFiles = require('./remove-uploaded-files')
  10. function drainStream (stream) {
  11. stream.on('readable', stream.read.bind(stream))
  12. }
  13. function makeMiddleware (setup) {
  14. return function multerMiddleware (req, res, next) {
  15. if (!is(req, ['multipart'])) return next()
  16. var options = setup()
  17. var limits = options.limits
  18. var storage = options.storage
  19. var fileFilter = options.fileFilter
  20. var fileStrategy = options.fileStrategy
  21. var preservePath = options.preservePath
  22. req.body = Object.create(null)
  23. var busboy
  24. try {
  25. busboy = new Busboy({ headers: req.headers, limits: limits, preservePath: preservePath })
  26. } catch (err) {
  27. return next(err)
  28. }
  29. var appender = new FileAppender(fileStrategy, req)
  30. var isDone = false
  31. var readFinished = false
  32. var errorOccured = false
  33. var pendingWrites = new Counter()
  34. var uploadedFiles = []
  35. function done (err) {
  36. if (isDone) return
  37. isDone = true
  38. req.unpipe(busboy)
  39. drainStream(req)
  40. busboy.removeAllListeners()
  41. onFinished(req, function () { next(err) })
  42. }
  43. function indicateDone () {
  44. if (readFinished && pendingWrites.isZero() && !errorOccured) done()
  45. }
  46. function abortWithError (uploadError) {
  47. if (errorOccured) return
  48. errorOccured = true
  49. pendingWrites.onceZero(function () {
  50. function remove (file, cb) {
  51. storage._removeFile(req, file, cb)
  52. }
  53. removeUploadedFiles(uploadedFiles, remove, function (err, storageErrors) {
  54. if (err) return done(err)
  55. uploadError.storageErrors = storageErrors
  56. done(uploadError)
  57. })
  58. })
  59. }
  60. function abortWithCode (code, optionalField) {
  61. abortWithError(new MulterError(code, optionalField))
  62. }
  63. // handle text field data
  64. busboy.on('field', function (fieldname, value, fieldnameTruncated, valueTruncated) {
  65. if (fieldnameTruncated) return abortWithCode('LIMIT_FIELD_KEY')
  66. if (valueTruncated) return abortWithCode('LIMIT_FIELD_VALUE', fieldname)
  67. // Work around bug in Busboy (https://github.com/mscdex/busboy/issues/6)
  68. if (limits && Object.prototype.hasOwnProperty.call(limits, 'fieldNameSize')) {
  69. if (fieldname.length > limits.fieldNameSize) return abortWithCode('LIMIT_FIELD_KEY')
  70. }
  71. appendField(req.body, fieldname, value)
  72. })
  73. // handle files
  74. busboy.on('file', function (fieldname, fileStream, filename, encoding, mimetype) {
  75. // don't attach to the files object, if there is no file
  76. if (!filename) return fileStream.resume()
  77. // Work around bug in Busboy (https://github.com/mscdex/busboy/issues/6)
  78. if (limits && Object.prototype.hasOwnProperty.call(limits, 'fieldNameSize')) {
  79. if (fieldname.length > limits.fieldNameSize) return abortWithCode('LIMIT_FIELD_KEY')
  80. }
  81. var file = {
  82. fieldname: fieldname,
  83. originalname: filename,
  84. encoding: encoding,
  85. mimetype: mimetype
  86. }
  87. var placeholder = appender.insertPlaceholder(file)
  88. fileFilter(req, file, function (err, includeFile) {
  89. if (err) {
  90. appender.removePlaceholder(placeholder)
  91. return abortWithError(err)
  92. }
  93. if (!includeFile) {
  94. appender.removePlaceholder(placeholder)
  95. return fileStream.resume()
  96. }
  97. var aborting = false
  98. pendingWrites.increment()
  99. Object.defineProperty(file, 'stream', {
  100. configurable: true,
  101. enumerable: false,
  102. value: fileStream
  103. })
  104. fileStream.on('error', function (err) {
  105. pendingWrites.decrement()
  106. abortWithError(err)
  107. })
  108. fileStream.on('limit', function () {
  109. aborting = true
  110. abortWithCode('LIMIT_FILE_SIZE', fieldname)
  111. })
  112. storage._handleFile(req, file, function (err, info) {
  113. if (aborting) {
  114. appender.removePlaceholder(placeholder)
  115. uploadedFiles.push(extend(file, info))
  116. return pendingWrites.decrement()
  117. }
  118. if (err) {
  119. appender.removePlaceholder(placeholder)
  120. pendingWrites.decrement()
  121. return abortWithError(err)
  122. }
  123. var fileInfo = extend(file, info)
  124. appender.replacePlaceholder(placeholder, fileInfo)
  125. uploadedFiles.push(fileInfo)
  126. pendingWrites.decrement()
  127. indicateDone()
  128. })
  129. })
  130. })
  131. busboy.on('error', function (err) { abortWithError(err) })
  132. busboy.on('partsLimit', function () { abortWithCode('LIMIT_PART_COUNT') })
  133. busboy.on('filesLimit', function () { abortWithCode('LIMIT_FILE_COUNT') })
  134. busboy.on('fieldsLimit', function () { abortWithCode('LIMIT_FIELD_COUNT') })
  135. busboy.on('finish', function () {
  136. readFinished = true
  137. indicateDone()
  138. })
  139. req.pipe(busboy)
  140. }
  141. }
  142. module.exports = makeMiddleware