index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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 busboy = require('busboy'); // eslint-disable-line no-unused-vars
  7. const DEFAULT_OPTIONS = {
  8. debug: false,
  9. uploadTimeout: 60000,
  10. fileHandler: false,
  11. uriDecodeFileNames: false,
  12. safeFileNames: false,
  13. preserveExtension: false,
  14. abortOnLimit: false,
  15. responseOnLimit: 'File size limit has been reached',
  16. limitHandler: false,
  17. createParentPath: false,
  18. parseNested: false,
  19. useTempFiles: false,
  20. tempFileDir: path.join(process.cwd(), 'tmp')
  21. };
  22. /**
  23. * Expose the file upload middleware
  24. * @param {DEFAULT_OPTIONS & busboy.BusboyConfig} options - Middleware options.
  25. * @returns {Function} - express-fileupload middleware.
  26. */
  27. module.exports = (options) => {
  28. const uploadOptions = buildOptions(DEFAULT_OPTIONS, options);
  29. return (req, res, next) => {
  30. if (!isEligibleRequest(req)) {
  31. debugLog(uploadOptions, 'Request is not eligible for file upload!');
  32. return next();
  33. }
  34. processMultipart(uploadOptions, req, res, next);
  35. };
  36. };