index.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. const fileFactory = require('./fileFactory');
  3. const processMultipart = require('./processMultipart');
  4. const isEligibleRequest = require('./isEligibleRequest');
  5. const processNested = require('./processNested');
  6. const {buildOptions} = require('./utilities');
  7. const fileUploadOptionsDefaults = {
  8. safeFileNames: false,
  9. preserveExtension: false,
  10. abortOnLimit: false,
  11. responseOnLimit: 'File size limit has been reached',
  12. limitHandler: false,
  13. createParentPath: false,
  14. parseNested: false,
  15. useTempFiles: false,
  16. tempFileDir: '/tmp'
  17. };
  18. /**
  19. * Expose the file upload middleware
  20. */
  21. module.exports = (fileUploadOptions) => {
  22. fileUploadOptions = buildOptions(fileUploadOptionsDefaults, fileUploadOptions);
  23. return function(req, res, next){
  24. if (!isEligibleRequest(req)) {
  25. return next();
  26. }
  27. processMultipart(fileUploadOptions, req, res, next);
  28. };
  29. };
  30. /**
  31. * Quietly expose fileFactory and processNested; useful for testing
  32. */
  33. module.exports.fileFactory = fileFactory;
  34. module.exports.processNested = processNested;