index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. const path = require('path');
  3. const processMultipart = require('./processMultipart');
  4. const isEligibleRequest = require('./isEligibleRequest');
  5. const { buildOptions, debugLog } = require('./utilities');
  6. const DEFAULT_OPTIONS = {
  7. debug: false,
  8. uploadTimeout: 60000,
  9. fileHandler: false,
  10. uriDecodeFileNames: false,
  11. safeFileNames: false,
  12. preserveExtension: false,
  13. abortOnLimit: false,
  14. responseOnLimit: 'File size limit has been reached',
  15. limitHandler: false,
  16. createParentPath: false,
  17. parseNested: false,
  18. useTempFiles: false,
  19. tempFileDir: path.join(process.cwd(), 'tmp')
  20. };
  21. /**
  22. * Expose the file upload middleware
  23. * @param {Object} options - Middleware options.
  24. * @returns {Function} - express-fileupload middleware.
  25. */
  26. module.exports = (options) => {
  27. const uploadOptions = buildOptions(DEFAULT_OPTIONS, options);
  28. return (req, res, next) => {
  29. if (!isEligibleRequest(req)) {
  30. debugLog(uploadOptions, 'Request is not eligible for file upload!');
  31. return next();
  32. }
  33. processMultipart(uploadOptions, req, res, next);
  34. };
  35. };