multermate 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +211 -0
- package/dist/esm/index.js +202 -0
- package/dist/esm/package.json +1 -0
- package/index.d.ts +1 -0
- package/index.js +2 -0
- package/index.mjs +15 -0
- package/package.json +48 -12
- package/readme.md +262 -86
- package/types/index.d.ts +58 -0
- package/babel.config.js +0 -12
- package/dist/index.cjs.js +0 -197
- package/dist/index.js +0 -158
- package/dist/index.mjs +0 -167
- package/src/index.js +0 -167
package/dist/index.js
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.uploadSingle = exports.uploadMultiple = exports.uploadFields = exports.configureStorage = exports.configureMulter = exports.configureFileFilter = exports.ALLOWED_FILE_TYPES = void 0;
|
|
7
|
-
var _multer = _interopRequireDefault(require("multer"));
|
|
8
|
-
var _path = _interopRequireDefault(require("path"));
|
|
9
|
-
var _uuid = require("uuid");
|
|
10
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
|
-
// Constants for allowed MIME types
|
|
12
|
-
const ALLOWED_MIME_TYPES = {
|
|
13
|
-
images: ["image/jpeg", "image/jpg", "image/png", "image/gif"],
|
|
14
|
-
videos: ["video/mp4", "video/mpeg", "video/ogg", "video/webm", "video/avi"],
|
|
15
|
-
pdfs: ["application/pdf"],
|
|
16
|
-
all: ["image/jpeg", "image/jpg", "image/png", "image/gif", "video/mp4", "video/mpeg", "video/ogg", "video/webm", "video/avi", "application/pdf"]
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Function to configure storage for Multer.
|
|
21
|
-
*
|
|
22
|
-
* @param {string} destination - The destination folder where files will be stored. Default is "uploads".
|
|
23
|
-
* @returns {object} - Multer storage configuration object.
|
|
24
|
-
*/
|
|
25
|
-
const configureStorage = destination => {
|
|
26
|
-
return _multer.default.diskStorage({
|
|
27
|
-
destination: (req, file, cb) => {
|
|
28
|
-
cb(null, destination || "uploads"); // Default folder is "uploads" if none is provided.
|
|
29
|
-
},
|
|
30
|
-
filename: (req, file, cb) => {
|
|
31
|
-
const sanitizedFilename = file.originalname.replace(/\\/g, "/");
|
|
32
|
-
const extension = _path.default.extname(sanitizedFilename);
|
|
33
|
-
const fieldName = file.fieldname || "file"; // Use the field name as part of the filename.
|
|
34
|
-
const uniqueName = (0, _uuid.v4)(); // Generate a unique name using uuid.
|
|
35
|
-
let fileName = `${uniqueName}-${fieldName}${extension}`;
|
|
36
|
-
|
|
37
|
-
// Replace backslashes with forward slashes in the final filename
|
|
38
|
-
fileName = fileName.replace(/\\/g, "/");
|
|
39
|
-
cb(null, fileName); // Set the final filename.
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Function to configure file filter for Multer.
|
|
46
|
-
*
|
|
47
|
-
* @param {Array} allowedMimeTypes - Array of allowed MIME types.
|
|
48
|
-
* @returns {function} - File filter function for Multer.
|
|
49
|
-
*/
|
|
50
|
-
exports.configureStorage = configureStorage;
|
|
51
|
-
const configureFileFilter = allowedMimeTypes => {
|
|
52
|
-
return (req, file, cb) => {
|
|
53
|
-
if (allowedMimeTypes.includes(file.mimetype)) {
|
|
54
|
-
cb(null, true); // Allow the file if its MIME type is allowed.
|
|
55
|
-
} else {
|
|
56
|
-
cb(new Error("Invalid file type. Only specified file types are allowed."), false); // Reject the file if its MIME type is not allowed.
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Function to configure Multer with the provided options.
|
|
63
|
-
*
|
|
64
|
-
* @param {object} options - Configuration options for Multer.
|
|
65
|
-
* @param {string} [options.destination] - Destination folder for files. Default is "uploads".
|
|
66
|
-
* @param {string} [options.filename] - Custom filename template for saved files.
|
|
67
|
-
* @param {Array<string>} [options.fileTypes] - Array of file types to allow (e.g., ['images', 'videos']).
|
|
68
|
-
* @param {Array<string>} [options.customMimeTypes] - Array of custom MIME types to allow.
|
|
69
|
-
* @param {number} [options.fileSizeLimit] - Maximum file size allowed (in bytes). Default is 50MB.
|
|
70
|
-
* @param {boolean} [options.preservePath] - Preserve the full path of files. Default is false.
|
|
71
|
-
* @returns {object} - Multer instance configured with the provided options.
|
|
72
|
-
*/
|
|
73
|
-
exports.configureFileFilter = configureFileFilter;
|
|
74
|
-
const configureMulter = ({
|
|
75
|
-
destination,
|
|
76
|
-
filename,
|
|
77
|
-
fileTypes = [],
|
|
78
|
-
customMimeTypes = [],
|
|
79
|
-
fileSizeLimit,
|
|
80
|
-
preservePath = false
|
|
81
|
-
}) => {
|
|
82
|
-
const storage = configureStorage(destination);
|
|
83
|
-
|
|
84
|
-
// Combine allowed MIME types based on fileTypes array
|
|
85
|
-
let allowedMimeTypes = [];
|
|
86
|
-
if (customMimeTypes.length > 0) {
|
|
87
|
-
// Use custom MIME types if provided
|
|
88
|
-
allowedMimeTypes = customMimeTypes;
|
|
89
|
-
} else {
|
|
90
|
-
// Use default MIME types for specified fileTypes
|
|
91
|
-
fileTypes.forEach(type => {
|
|
92
|
-
if (ALLOWED_MIME_TYPES[type]) {
|
|
93
|
-
allowedMimeTypes = allowedMimeTypes.concat(ALLOWED_MIME_TYPES[type]);
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
// If no specific file types are provided, use all allowed MIME types
|
|
98
|
-
if (allowedMimeTypes.length === 0) {
|
|
99
|
-
allowedMimeTypes = ALLOWED_MIME_TYPES.all;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
const fileFilter = configureFileFilter(allowedMimeTypes);
|
|
103
|
-
return (0, _multer.default)({
|
|
104
|
-
storage,
|
|
105
|
-
fileFilter,
|
|
106
|
-
limits: {
|
|
107
|
-
fileSize: fileSizeLimit || 1024 * 1024 * 50
|
|
108
|
-
},
|
|
109
|
-
// Default 50MB file size limit
|
|
110
|
-
preservePath
|
|
111
|
-
});
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Function to handle multiple fields in a single form submission.
|
|
116
|
-
*
|
|
117
|
-
* @param {Array} fields - Array of field configurations, each containing:
|
|
118
|
-
* @param {string} fields.name - The name of the form field.
|
|
119
|
-
* @param {number} [fields.maxCount=10] - The maximum number of files to accept per field.
|
|
120
|
-
* @param {Array<string>} [fields.fileTypes] - Array of file types to allow for this field (e.g., ['images']).
|
|
121
|
-
* @returns {function} - Multer instance configured to handle multiple fields.
|
|
122
|
-
*/
|
|
123
|
-
exports.configureMulter = configureMulter;
|
|
124
|
-
const uploadFields = fields => {
|
|
125
|
-
const fieldConfigs = fields.map(field => ({
|
|
126
|
-
name: field.name,
|
|
127
|
-
maxCount: field.maxCount || 10 // Default maxCount is 10 if not specified.
|
|
128
|
-
}));
|
|
129
|
-
let allowedFileTypes = [];
|
|
130
|
-
fields.forEach(field => {
|
|
131
|
-
const types = field.fileTypes || [];
|
|
132
|
-
types.forEach(type => {
|
|
133
|
-
if (ALLOWED_MIME_TYPES[type]) {
|
|
134
|
-
allowedFileTypes = allowedFileTypes.concat(ALLOWED_MIME_TYPES[type]);
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
});
|
|
138
|
-
const multerInstance = configureMulter({
|
|
139
|
-
fileTypes: allowedFileTypes,
|
|
140
|
-
customMimeTypes: [],
|
|
141
|
-
fileSizeLimit: fields[0]?.fileSizeLimit // Assuming all fields share the same limit.
|
|
142
|
-
});
|
|
143
|
-
return multerInstance.fields(fieldConfigs);
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
// Export functions to configure multer and available file types
|
|
147
|
-
exports.uploadFields = uploadFields;
|
|
148
|
-
const uploadSingle = (options = {}) => {
|
|
149
|
-
const multerInstance = configureMulter(options);
|
|
150
|
-
return multerInstance.single(options.filename || "file");
|
|
151
|
-
};
|
|
152
|
-
exports.uploadSingle = uploadSingle;
|
|
153
|
-
const uploadMultiple = (options = {}) => {
|
|
154
|
-
const multerInstance = configureMulter(options);
|
|
155
|
-
return multerInstance.fields(options.fields || []);
|
|
156
|
-
};
|
|
157
|
-
exports.uploadMultiple = uploadMultiple;
|
|
158
|
-
const ALLOWED_FILE_TYPES = exports.ALLOWED_FILE_TYPES = Object.keys(ALLOWED_MIME_TYPES);
|
package/dist/index.mjs
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
import multer from "multer";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { v4 as uuidv4 } from "uuid";
|
|
4
|
-
|
|
5
|
-
// Constants for allowed MIME types
|
|
6
|
-
const ALLOWED_MIME_TYPES = {
|
|
7
|
-
images: ["image/jpeg", "image/jpg", "image/png", "image/gif"],
|
|
8
|
-
videos: ["video/mp4", "video/mpeg", "video/ogg", "video/webm", "video/avi"],
|
|
9
|
-
pdfs: ["application/pdf"],
|
|
10
|
-
all: [
|
|
11
|
-
"image/jpeg",
|
|
12
|
-
"image/jpg",
|
|
13
|
-
"image/png",
|
|
14
|
-
"image/gif",
|
|
15
|
-
"video/mp4",
|
|
16
|
-
"video/mpeg",
|
|
17
|
-
"video/ogg",
|
|
18
|
-
"video/webm",
|
|
19
|
-
"video/avi",
|
|
20
|
-
"application/pdf",
|
|
21
|
-
],
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Function to configure storage for Multer.
|
|
26
|
-
*
|
|
27
|
-
* @param {string} destination - The destination folder where files will be stored. Default is "uploads".
|
|
28
|
-
* @returns {object} - Multer storage configuration object.
|
|
29
|
-
*/
|
|
30
|
-
export const configureStorage = (destination) => {
|
|
31
|
-
return multer.diskStorage({
|
|
32
|
-
destination: (req, file, cb) => {
|
|
33
|
-
cb(null, destination || "uploads"); // Default folder is "uploads" if none is provided.
|
|
34
|
-
},
|
|
35
|
-
filename: (req, file, cb) => {
|
|
36
|
-
const sanitizedFilename = file.originalname.replace(/\\/g, "/");
|
|
37
|
-
const extension = path.extname(sanitizedFilename);
|
|
38
|
-
const fieldName = file.fieldname || "file"; // Use the field name as part of the filename.
|
|
39
|
-
const uniqueName = uuidv4(); // Generate a unique name using uuid.
|
|
40
|
-
let fileName = `${uniqueName}-${fieldName}${extension}`;
|
|
41
|
-
|
|
42
|
-
// Replace backslashes with forward slashes in the final filename
|
|
43
|
-
fileName = fileName.replace(/\\/g, "/");
|
|
44
|
-
|
|
45
|
-
cb(null, fileName); // Set the final filename.
|
|
46
|
-
},
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Function to configure file filter for Multer.
|
|
52
|
-
*
|
|
53
|
-
* @param {Array} allowedMimeTypes - Array of allowed MIME types.
|
|
54
|
-
* @returns {function} - File filter function for Multer.
|
|
55
|
-
*/
|
|
56
|
-
export const configureFileFilter = (allowedMimeTypes) => {
|
|
57
|
-
return (req, file, cb) => {
|
|
58
|
-
if (allowedMimeTypes.includes(file.mimetype)) {
|
|
59
|
-
cb(null, true); // Allow the file if its MIME type is allowed.
|
|
60
|
-
} else {
|
|
61
|
-
cb(
|
|
62
|
-
new Error("Invalid file type. Only specified file types are allowed."),
|
|
63
|
-
false
|
|
64
|
-
); // Reject the file if its MIME type is not allowed.
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Function to configure Multer with the provided options.
|
|
71
|
-
*
|
|
72
|
-
* @param {object} options - Configuration options for Multer.
|
|
73
|
-
* @param {string} [options.destination] - Destination folder for files. Default is "uploads".
|
|
74
|
-
* @param {string} [options.filename] - Custom filename template for saved files.
|
|
75
|
-
* @param {Array<string>} [options.fileTypes] - Array of file types to allow (e.g., ['images', 'videos']).
|
|
76
|
-
* @param {Array<string>} [options.customMimeTypes] - Array of custom MIME types to allow.
|
|
77
|
-
* @param {number} [options.fileSizeLimit] - Maximum file size allowed (in bytes). Default is 50MB.
|
|
78
|
-
* @param {boolean} [options.preservePath] - Preserve the full path of files. Default is false.
|
|
79
|
-
* @returns {object} - Multer instance configured with the provided options.
|
|
80
|
-
*/
|
|
81
|
-
export const configureMulter = ({
|
|
82
|
-
destination,
|
|
83
|
-
filename,
|
|
84
|
-
fileTypes = [],
|
|
85
|
-
customMimeTypes = [],
|
|
86
|
-
fileSizeLimit,
|
|
87
|
-
preservePath = false,
|
|
88
|
-
}) => {
|
|
89
|
-
const storage = configureStorage(destination);
|
|
90
|
-
|
|
91
|
-
// Combine allowed MIME types based on fileTypes array
|
|
92
|
-
let allowedMimeTypes = [];
|
|
93
|
-
|
|
94
|
-
if (customMimeTypes.length > 0) {
|
|
95
|
-
// Use custom MIME types if provided
|
|
96
|
-
allowedMimeTypes = customMimeTypes;
|
|
97
|
-
} else {
|
|
98
|
-
// Use default MIME types for specified fileTypes
|
|
99
|
-
fileTypes.forEach((type) => {
|
|
100
|
-
if (ALLOWED_MIME_TYPES[type]) {
|
|
101
|
-
allowedMimeTypes = allowedMimeTypes.concat(ALLOWED_MIME_TYPES[type]);
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
// If no specific file types are provided, use all allowed MIME types
|
|
106
|
-
if (allowedMimeTypes.length === 0) {
|
|
107
|
-
allowedMimeTypes = ALLOWED_MIME_TYPES.all;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const fileFilter = configureFileFilter(allowedMimeTypes);
|
|
112
|
-
|
|
113
|
-
return multer({
|
|
114
|
-
storage,
|
|
115
|
-
fileFilter,
|
|
116
|
-
limits: { fileSize: fileSizeLimit || 1024 * 1024 * 50 }, // Default 50MB file size limit
|
|
117
|
-
preservePath,
|
|
118
|
-
});
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Function to handle multiple fields in a single form submission.
|
|
123
|
-
*
|
|
124
|
-
* @param {Array} fields - Array of field configurations, each containing:
|
|
125
|
-
* @param {string} fields.name - The name of the form field.
|
|
126
|
-
* @param {number} [fields.maxCount=10] - The maximum number of files to accept per field.
|
|
127
|
-
* @param {Array<string>} [fields.fileTypes] - Array of file types to allow for this field (e.g., ['images']).
|
|
128
|
-
* @returns {function} - Multer instance configured to handle multiple fields.
|
|
129
|
-
*/
|
|
130
|
-
export const uploadFields = (fields) => {
|
|
131
|
-
const fieldConfigs = fields.map((field) => ({
|
|
132
|
-
name: field.name,
|
|
133
|
-
maxCount: field.maxCount || 10, // Default maxCount is 10 if not specified.
|
|
134
|
-
}));
|
|
135
|
-
|
|
136
|
-
let allowedFileTypes = [];
|
|
137
|
-
|
|
138
|
-
fields.forEach((field) => {
|
|
139
|
-
const types = field.fileTypes || [];
|
|
140
|
-
types.forEach((type) => {
|
|
141
|
-
if (ALLOWED_MIME_TYPES[type]) {
|
|
142
|
-
allowedFileTypes = allowedFileTypes.concat(ALLOWED_MIME_TYPES[type]);
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
const multerInstance = configureMulter({
|
|
148
|
-
fileTypes: allowedFileTypes,
|
|
149
|
-
customMimeTypes: [],
|
|
150
|
-
fileSizeLimit: fields[0]?.fileSizeLimit, // Assuming all fields share the same limit.
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
return multerInstance.fields(fieldConfigs);
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
// Export functions to configure multer and available file types
|
|
157
|
-
export const uploadSingle = (options = {}) => {
|
|
158
|
-
const multerInstance = configureMulter(options);
|
|
159
|
-
return multerInstance.single(options.filename || "file");
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
export const uploadMultiple = (options = {}) => {
|
|
163
|
-
const multerInstance = configureMulter(options);
|
|
164
|
-
return multerInstance.fields(options.fields || []);
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
export const ALLOWED_FILE_TYPES = Object.keys(ALLOWED_MIME_TYPES);
|
package/src/index.js
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
import multer from "multer";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { v4 as uuidv4 } from "uuid";
|
|
4
|
-
|
|
5
|
-
// Constants for allowed MIME types
|
|
6
|
-
const ALLOWED_MIME_TYPES = {
|
|
7
|
-
images: ["image/jpeg", "image/jpg", "image/png", "image/gif"],
|
|
8
|
-
videos: ["video/mp4", "video/mpeg", "video/ogg", "video/webm", "video/avi"],
|
|
9
|
-
pdfs: ["application/pdf"],
|
|
10
|
-
all: [
|
|
11
|
-
"image/jpeg",
|
|
12
|
-
"image/jpg",
|
|
13
|
-
"image/png",
|
|
14
|
-
"image/gif",
|
|
15
|
-
"video/mp4",
|
|
16
|
-
"video/mpeg",
|
|
17
|
-
"video/ogg",
|
|
18
|
-
"video/webm",
|
|
19
|
-
"video/avi",
|
|
20
|
-
"application/pdf",
|
|
21
|
-
],
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Function to configure storage for Multer.
|
|
26
|
-
*
|
|
27
|
-
* @param {string} destination - The destination folder where files will be stored. Default is "uploads".
|
|
28
|
-
* @returns {object} - Multer storage configuration object.
|
|
29
|
-
*/
|
|
30
|
-
export const configureStorage = (destination) => {
|
|
31
|
-
return multer.diskStorage({
|
|
32
|
-
destination: (req, file, cb) => {
|
|
33
|
-
cb(null, destination || "uploads"); // Default folder is "uploads" if none is provided.
|
|
34
|
-
},
|
|
35
|
-
filename: (req, file, cb) => {
|
|
36
|
-
const sanitizedFilename = file.originalname.replace(/\\/g, "/");
|
|
37
|
-
const extension = path.extname(sanitizedFilename);
|
|
38
|
-
const fieldName = file.fieldname || "file"; // Use the field name as part of the filename.
|
|
39
|
-
const uniqueName = uuidv4(); // Generate a unique name using uuid.
|
|
40
|
-
let fileName = `${uniqueName}-${fieldName}${extension}`;
|
|
41
|
-
|
|
42
|
-
// Replace backslashes with forward slashes in the final filename
|
|
43
|
-
fileName = fileName.replace(/\\/g, "/");
|
|
44
|
-
|
|
45
|
-
cb(null, fileName); // Set the final filename.
|
|
46
|
-
},
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Function to configure file filter for Multer.
|
|
52
|
-
*
|
|
53
|
-
* @param {Array} allowedMimeTypes - Array of allowed MIME types.
|
|
54
|
-
* @returns {function} - File filter function for Multer.
|
|
55
|
-
*/
|
|
56
|
-
export const configureFileFilter = (allowedMimeTypes) => {
|
|
57
|
-
return (req, file, cb) => {
|
|
58
|
-
if (allowedMimeTypes.includes(file.mimetype)) {
|
|
59
|
-
cb(null, true); // Allow the file if its MIME type is allowed.
|
|
60
|
-
} else {
|
|
61
|
-
cb(
|
|
62
|
-
new Error("Invalid file type. Only specified file types are allowed."),
|
|
63
|
-
false
|
|
64
|
-
); // Reject the file if its MIME type is not allowed.
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Function to configure Multer with the provided options.
|
|
71
|
-
*
|
|
72
|
-
* @param {object} options - Configuration options for Multer.
|
|
73
|
-
* @param {string} [options.destination] - Destination folder for files. Default is "uploads".
|
|
74
|
-
* @param {string} [options.filename] - Custom filename template for saved files.
|
|
75
|
-
* @param {Array<string>} [options.fileTypes] - Array of file types to allow (e.g., ['images', 'videos']).
|
|
76
|
-
* @param {Array<string>} [options.customMimeTypes] - Array of custom MIME types to allow.
|
|
77
|
-
* @param {number} [options.fileSizeLimit] - Maximum file size allowed (in bytes). Default is 50MB.
|
|
78
|
-
* @param {boolean} [options.preservePath] - Preserve the full path of files. Default is false.
|
|
79
|
-
* @returns {object} - Multer instance configured with the provided options.
|
|
80
|
-
*/
|
|
81
|
-
export const configureMulter = ({
|
|
82
|
-
destination,
|
|
83
|
-
filename,
|
|
84
|
-
fileTypes = [],
|
|
85
|
-
customMimeTypes = [],
|
|
86
|
-
fileSizeLimit,
|
|
87
|
-
preservePath = false,
|
|
88
|
-
}) => {
|
|
89
|
-
const storage = configureStorage(destination);
|
|
90
|
-
|
|
91
|
-
// Combine allowed MIME types based on fileTypes array
|
|
92
|
-
let allowedMimeTypes = [];
|
|
93
|
-
|
|
94
|
-
if (customMimeTypes.length > 0) {
|
|
95
|
-
// Use custom MIME types if provided
|
|
96
|
-
allowedMimeTypes = customMimeTypes;
|
|
97
|
-
} else {
|
|
98
|
-
// Use default MIME types for specified fileTypes
|
|
99
|
-
fileTypes.forEach((type) => {
|
|
100
|
-
if (ALLOWED_MIME_TYPES[type]) {
|
|
101
|
-
allowedMimeTypes = allowedMimeTypes.concat(ALLOWED_MIME_TYPES[type]);
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
// If no specific file types are provided, use all allowed MIME types
|
|
106
|
-
if (allowedMimeTypes.length === 0) {
|
|
107
|
-
allowedMimeTypes = ALLOWED_MIME_TYPES.all;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const fileFilter = configureFileFilter(allowedMimeTypes);
|
|
112
|
-
|
|
113
|
-
return multer({
|
|
114
|
-
storage,
|
|
115
|
-
fileFilter,
|
|
116
|
-
limits: { fileSize: fileSizeLimit || 1024 * 1024 * 50 }, // Default 50MB file size limit
|
|
117
|
-
preservePath,
|
|
118
|
-
});
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Function to handle multiple fields in a single form submission.
|
|
123
|
-
*
|
|
124
|
-
* @param {Array} fields - Array of field configurations, each containing:
|
|
125
|
-
* @param {string} fields.name - The name of the form field.
|
|
126
|
-
* @param {number} [fields.maxCount=10] - The maximum number of files to accept per field.
|
|
127
|
-
* @param {Array<string>} [fields.fileTypes] - Array of file types to allow for this field (e.g., ['images']).
|
|
128
|
-
* @returns {function} - Multer instance configured to handle multiple fields.
|
|
129
|
-
*/
|
|
130
|
-
export const uploadFields = (fields) => {
|
|
131
|
-
const fieldConfigs = fields.map((field) => ({
|
|
132
|
-
name: field.name,
|
|
133
|
-
maxCount: field.maxCount || 10, // Default maxCount is 10 if not specified.
|
|
134
|
-
}));
|
|
135
|
-
|
|
136
|
-
let allowedFileTypes = [];
|
|
137
|
-
|
|
138
|
-
fields.forEach((field) => {
|
|
139
|
-
const types = field.fileTypes || [];
|
|
140
|
-
types.forEach((type) => {
|
|
141
|
-
if (ALLOWED_MIME_TYPES[type]) {
|
|
142
|
-
allowedFileTypes = allowedFileTypes.concat(ALLOWED_MIME_TYPES[type]);
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
const multerInstance = configureMulter({
|
|
148
|
-
fileTypes: allowedFileTypes,
|
|
149
|
-
customMimeTypes: [],
|
|
150
|
-
fileSizeLimit: fields[0]?.fileSizeLimit, // Assuming all fields share the same limit.
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
return multerInstance.fields(fieldConfigs);
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
// Export functions to configure multer and available file types
|
|
157
|
-
export const uploadSingle = (options = {}) => {
|
|
158
|
-
const multerInstance = configureMulter(options);
|
|
159
|
-
return multerInstance.single(options.filename || "file");
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
export const uploadMultiple = (options = {}) => {
|
|
163
|
-
const multerInstance = configureMulter(options);
|
|
164
|
-
return multerInstance.fields(options.fields || []);
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
export const ALLOWED_FILE_TYPES = Object.keys(ALLOWED_MIME_TYPES);
|