You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
39 lines
1.1 KiB
4 years ago
|
'use strict';
|
||
|
|
||
|
const path = require('path');
|
||
|
const processMultipart = require('./processMultipart');
|
||
|
const isEligibleRequest = require('./isEligibleRequest');
|
||
|
const { buildOptions, debugLog } = require('./utilities');
|
||
|
|
||
|
const DEFAULT_OPTIONS = {
|
||
|
debug: false,
|
||
|
uploadTimeout: 60000,
|
||
|
fileHandler: false,
|
||
|
uriDecodeFileNames: false,
|
||
|
safeFileNames: false,
|
||
|
preserveExtension: false,
|
||
|
abortOnLimit: false,
|
||
|
responseOnLimit: 'File size limit has been reached',
|
||
|
limitHandler: false,
|
||
|
createParentPath: false,
|
||
|
parseNested: false,
|
||
|
useTempFiles: false,
|
||
|
tempFileDir: path.join(process.cwd(), 'tmp')
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Expose the file upload middleware
|
||
|
* @param {Object} options - Middleware options.
|
||
|
* @returns {Function} - express-fileupload middleware.
|
||
|
*/
|
||
|
module.exports = (options) => {
|
||
|
const uploadOptions = buildOptions(DEFAULT_OPTIONS, options);
|
||
|
return (req, res, next) => {
|
||
|
if (!isEligibleRequest(req)) {
|
||
|
debugLog(uploadOptions, 'Request is not eligible for file upload!');
|
||
|
return next();
|
||
|
}
|
||
|
processMultipart(uploadOptions, req, res, next);
|
||
|
};
|
||
|
};
|