multermate 1.0.5 → 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 -217
- package/index.mjs +15 -0
- package/package.json +31 -2
- package/readme.md +94 -14
- package/types/index.d.ts +58 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ALLOWED_FILE_TYPES = void 0;
|
|
7
|
+
exports.uploadSingle = uploadSingle;
|
|
8
|
+
exports.uploadMultiple = uploadMultiple;
|
|
9
|
+
exports.deleteFile = deleteFile;
|
|
10
|
+
const promises_1 = __importDefault(require("fs/promises"));
|
|
11
|
+
const multer_1 = __importDefault(require("multer"));
|
|
12
|
+
const path_1 = __importDefault(require("path"));
|
|
13
|
+
const uuid_1 = require("uuid");
|
|
14
|
+
// Define allowed MIME types
|
|
15
|
+
const ALLOWED_MIME_TYPES = {
|
|
16
|
+
images: ["image/jpeg", "image/jpg", "image/png", "image/gif"],
|
|
17
|
+
videos: ["video/mp4", "video/mpeg", "video/ogg", "video/webm", "video/avi"],
|
|
18
|
+
pdfs: ["application/pdf"],
|
|
19
|
+
all: [
|
|
20
|
+
"image/jpeg",
|
|
21
|
+
"image/jpg",
|
|
22
|
+
"image/png",
|
|
23
|
+
"image/gif",
|
|
24
|
+
"video/mp4",
|
|
25
|
+
"video/mpeg",
|
|
26
|
+
"video/ogg",
|
|
27
|
+
"video/webm",
|
|
28
|
+
"video/avi",
|
|
29
|
+
"application/pdf",
|
|
30
|
+
],
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Function to configure storage for Multer.
|
|
34
|
+
*
|
|
35
|
+
* @param destination - The destination folder where files will be stored.
|
|
36
|
+
* @returns Multer storage configuration object.
|
|
37
|
+
*/
|
|
38
|
+
const configureStorage = (destination) => {
|
|
39
|
+
return multer_1.default.diskStorage({
|
|
40
|
+
destination: (_req, _file, cb) => {
|
|
41
|
+
cb(null, destination || "uploads"); // Default folder is "uploads" if none is provided.
|
|
42
|
+
},
|
|
43
|
+
filename: (_req, file, cb) => {
|
|
44
|
+
const sanitizedFilename = file.originalname.replace(/\\/g, "/");
|
|
45
|
+
const extension = path_1.default.extname(sanitizedFilename);
|
|
46
|
+
const fieldName = file.fieldname || "file"; // Use the field name as part of the filename.
|
|
47
|
+
const uniqueName = (0, uuid_1.v4)(); // Generate a unique name using uuid.
|
|
48
|
+
let fileName = `${uniqueName}-${fieldName}${extension}`;
|
|
49
|
+
// Replace backslashes with forward slashes in the final filename
|
|
50
|
+
fileName = fileName.replace(/\\/g, "/");
|
|
51
|
+
cb(null, fileName); // Set the final filename.
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Function to configure file filter for Multer.
|
|
57
|
+
*
|
|
58
|
+
* @param allowedMimeTypes - Array of allowed MIME types.
|
|
59
|
+
* @returns File filter function for Multer.
|
|
60
|
+
*/
|
|
61
|
+
const configureFileFilter = (allowedMimeTypes) => {
|
|
62
|
+
return (_req, file, cb) => {
|
|
63
|
+
if (allowedMimeTypes.includes(file.mimetype)) {
|
|
64
|
+
cb(null, true); // Allow the file if its MIME type is allowed.
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
const error = new Error("Invalid file type. Only specified file types are allowed.");
|
|
68
|
+
error.code = 'INVALID_FILE_TYPE';
|
|
69
|
+
cb(error); // Reject the file if its MIME type is not allowed.
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Function to configure Multer with the provided options.
|
|
75
|
+
*
|
|
76
|
+
* @param options - Configuration options for Multer.
|
|
77
|
+
* @returns Multer instance configured with the provided options.
|
|
78
|
+
*/
|
|
79
|
+
const configureMulter = ({ destination, filename, fileTypes = [], customMimeTypes = [], fileSizeLimit, preservePath = false, }) => {
|
|
80
|
+
const storage = configureStorage(destination);
|
|
81
|
+
// Combine allowed MIME types based on fileTypes array
|
|
82
|
+
let allowedMimeTypes = [];
|
|
83
|
+
if (customMimeTypes.length > 0) {
|
|
84
|
+
// Use custom MIME types if provided
|
|
85
|
+
allowedMimeTypes = customMimeTypes;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
// Use default MIME types for specified fileTypes
|
|
89
|
+
fileTypes.forEach((type) => {
|
|
90
|
+
if (ALLOWED_MIME_TYPES[type]) {
|
|
91
|
+
allowedMimeTypes = allowedMimeTypes.concat(ALLOWED_MIME_TYPES[type]);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
// If no specific file types are provided, use all allowed MIME types
|
|
95
|
+
if (allowedMimeTypes.length === 0) {
|
|
96
|
+
allowedMimeTypes = ALLOWED_MIME_TYPES.all;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const fileFilter = configureFileFilter(allowedMimeTypes);
|
|
100
|
+
return (0, multer_1.default)({
|
|
101
|
+
storage,
|
|
102
|
+
fileFilter,
|
|
103
|
+
limits: { fileSize: fileSizeLimit || 1024 * 1024 * 50 }, // Default 50MB file size limit
|
|
104
|
+
preservePath,
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Function to handle a single file upload.
|
|
109
|
+
*
|
|
110
|
+
* @param options - Configuration options for the single file upload.
|
|
111
|
+
* @returns Multer middleware configured for single file upload.
|
|
112
|
+
*/
|
|
113
|
+
function uploadSingle(options = {}) {
|
|
114
|
+
// Create destination directory if it doesn't exist
|
|
115
|
+
const destination = options.destination || 'uploads';
|
|
116
|
+
const multerInstance = configureMulter(options);
|
|
117
|
+
const middleware = multerInstance.single(options.filename || "file");
|
|
118
|
+
return (req, res, next) => {
|
|
119
|
+
// Make sure the destination directory exists
|
|
120
|
+
require('fs').mkdirSync(destination, { recursive: true });
|
|
121
|
+
middleware(req, res, (err) => {
|
|
122
|
+
if (err) {
|
|
123
|
+
if (err.code === 'LIMIT_FILE_SIZE') {
|
|
124
|
+
req.fileValidationError = 'File size limit exceeded';
|
|
125
|
+
}
|
|
126
|
+
else if (err.code === 'INVALID_FILE_TYPE') {
|
|
127
|
+
req.fileValidationError = 'Invalid file type';
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
req.fileValidationError = err.message;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
next();
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Function to handle multiple file uploads across multiple fields.
|
|
139
|
+
*
|
|
140
|
+
* @param options - Configuration options for multiple file uploads.
|
|
141
|
+
* @returns Multer middleware configured for multiple file uploads.
|
|
142
|
+
*/
|
|
143
|
+
function uploadMultiple(options) {
|
|
144
|
+
const destination = options.destination || 'uploads';
|
|
145
|
+
// Map fields configuration to multer format
|
|
146
|
+
const fieldConfigs = options.fields.map(field => ({
|
|
147
|
+
name: field.name,
|
|
148
|
+
maxCount: field.maxCount || 10, // Default maxCount is 10 if not specified.
|
|
149
|
+
}));
|
|
150
|
+
let allowedFileTypes = [];
|
|
151
|
+
options.fields.forEach((field) => {
|
|
152
|
+
const types = field.fileTypes || [];
|
|
153
|
+
types.forEach((type) => {
|
|
154
|
+
if (ALLOWED_MIME_TYPES[type]) {
|
|
155
|
+
allowedFileTypes = allowedFileTypes.concat(ALLOWED_MIME_TYPES[type]);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
const multerConfig = {
|
|
160
|
+
destination,
|
|
161
|
+
fileTypes: [],
|
|
162
|
+
customMimeTypes: options.customMimeTypes || [],
|
|
163
|
+
fileSizeLimit: options.fileSizeLimit,
|
|
164
|
+
preservePath: options.preservePath
|
|
165
|
+
};
|
|
166
|
+
const multerInstance = configureMulter(multerConfig);
|
|
167
|
+
const middleware = multerInstance.fields(fieldConfigs);
|
|
168
|
+
return (req, res, next) => {
|
|
169
|
+
// Make sure the destination directory exists
|
|
170
|
+
require('fs').mkdirSync(destination, { recursive: true });
|
|
171
|
+
middleware(req, res, (err) => {
|
|
172
|
+
if (err) {
|
|
173
|
+
if (err.code === 'LIMIT_FILE_SIZE') {
|
|
174
|
+
req.fileValidationError = 'File size limit exceeded';
|
|
175
|
+
}
|
|
176
|
+
else if (err.code === 'INVALID_FILE_TYPE') {
|
|
177
|
+
req.fileValidationError = 'Invalid file type';
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
req.fileValidationError = err.message;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
next();
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Utility function to delete a file from the filesystem
|
|
189
|
+
*
|
|
190
|
+
* @param filePath - The path to the file that needs to be deleted
|
|
191
|
+
* @returns Promise that resolves to true if deletion was successful, false otherwise
|
|
192
|
+
*/
|
|
193
|
+
async function deleteFile(filePath) {
|
|
194
|
+
try {
|
|
195
|
+
await promises_1.default.unlink(filePath);
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
console.error(`Error deleting file: ${error.message}`);
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
// Export the allowed file types for reference
|
|
204
|
+
exports.ALLOWED_FILE_TYPES = Object.keys(ALLOWED_MIME_TYPES);
|
|
205
|
+
// Export your functions
|
|
206
|
+
exports.default = {
|
|
207
|
+
uploadSingle,
|
|
208
|
+
uploadMultiple,
|
|
209
|
+
deleteFile,
|
|
210
|
+
ALLOWED_FILE_TYPES: exports.ALLOWED_FILE_TYPES
|
|
211
|
+
};
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import multer from 'multer';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
5
|
+
// Define 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
|
+
* Function to configure storage for Multer.
|
|
25
|
+
*
|
|
26
|
+
* @param destination - The destination folder where files will be stored.
|
|
27
|
+
* @returns Multer storage configuration object.
|
|
28
|
+
*/
|
|
29
|
+
const configureStorage = (destination) => {
|
|
30
|
+
return multer.diskStorage({
|
|
31
|
+
destination: (_req, _file, cb) => {
|
|
32
|
+
cb(null, destination || "uploads"); // Default folder is "uploads" if none is provided.
|
|
33
|
+
},
|
|
34
|
+
filename: (_req, file, cb) => {
|
|
35
|
+
const sanitizedFilename = file.originalname.replace(/\\/g, "/");
|
|
36
|
+
const extension = path.extname(sanitizedFilename);
|
|
37
|
+
const fieldName = file.fieldname || "file"; // Use the field name as part of the filename.
|
|
38
|
+
const uniqueName = uuidv4(); // Generate a unique name using uuid.
|
|
39
|
+
let fileName = `${uniqueName}-${fieldName}${extension}`;
|
|
40
|
+
// Replace backslashes with forward slashes in the final filename
|
|
41
|
+
fileName = fileName.replace(/\\/g, "/");
|
|
42
|
+
cb(null, fileName); // Set the final filename.
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Function to configure file filter for Multer.
|
|
48
|
+
*
|
|
49
|
+
* @param allowedMimeTypes - Array of allowed MIME types.
|
|
50
|
+
* @returns File filter function for Multer.
|
|
51
|
+
*/
|
|
52
|
+
const configureFileFilter = (allowedMimeTypes) => {
|
|
53
|
+
return (_req, file, cb) => {
|
|
54
|
+
if (allowedMimeTypes.includes(file.mimetype)) {
|
|
55
|
+
cb(null, true); // Allow the file if its MIME type is allowed.
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
const error = new Error("Invalid file type. Only specified file types are allowed.");
|
|
59
|
+
error.code = 'INVALID_FILE_TYPE';
|
|
60
|
+
cb(error); // Reject the file if its MIME type is not allowed.
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Function to configure Multer with the provided options.
|
|
66
|
+
*
|
|
67
|
+
* @param options - Configuration options for Multer.
|
|
68
|
+
* @returns Multer instance configured with the provided options.
|
|
69
|
+
*/
|
|
70
|
+
const configureMulter = ({ destination, filename, fileTypes = [], customMimeTypes = [], fileSizeLimit, preservePath = false, }) => {
|
|
71
|
+
const storage = configureStorage(destination);
|
|
72
|
+
// Combine allowed MIME types based on fileTypes array
|
|
73
|
+
let allowedMimeTypes = [];
|
|
74
|
+
if (customMimeTypes.length > 0) {
|
|
75
|
+
// Use custom MIME types if provided
|
|
76
|
+
allowedMimeTypes = customMimeTypes;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
// Use default MIME types for specified fileTypes
|
|
80
|
+
fileTypes.forEach((type) => {
|
|
81
|
+
if (ALLOWED_MIME_TYPES[type]) {
|
|
82
|
+
allowedMimeTypes = allowedMimeTypes.concat(ALLOWED_MIME_TYPES[type]);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
// If no specific file types are provided, use all allowed MIME types
|
|
86
|
+
if (allowedMimeTypes.length === 0) {
|
|
87
|
+
allowedMimeTypes = ALLOWED_MIME_TYPES.all;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const fileFilter = configureFileFilter(allowedMimeTypes);
|
|
91
|
+
return multer({
|
|
92
|
+
storage,
|
|
93
|
+
fileFilter,
|
|
94
|
+
limits: { fileSize: fileSizeLimit || 1024 * 1024 * 50 }, // Default 50MB file size limit
|
|
95
|
+
preservePath,
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Function to handle a single file upload.
|
|
100
|
+
*
|
|
101
|
+
* @param options - Configuration options for the single file upload.
|
|
102
|
+
* @returns Multer middleware configured for single file upload.
|
|
103
|
+
*/
|
|
104
|
+
export function uploadSingle(options = {}) {
|
|
105
|
+
// Create destination directory if it doesn't exist
|
|
106
|
+
const destination = options.destination || 'uploads';
|
|
107
|
+
const multerInstance = configureMulter(options);
|
|
108
|
+
const middleware = multerInstance.single(options.filename || "file");
|
|
109
|
+
return (req, res, next) => {
|
|
110
|
+
// Make sure the destination directory exists
|
|
111
|
+
require('fs').mkdirSync(destination, { recursive: true });
|
|
112
|
+
middleware(req, res, (err) => {
|
|
113
|
+
if (err) {
|
|
114
|
+
if (err.code === 'LIMIT_FILE_SIZE') {
|
|
115
|
+
req.fileValidationError = 'File size limit exceeded';
|
|
116
|
+
}
|
|
117
|
+
else if (err.code === 'INVALID_FILE_TYPE') {
|
|
118
|
+
req.fileValidationError = 'Invalid file type';
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
req.fileValidationError = err.message;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
next();
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Function to handle multiple file uploads across multiple fields.
|
|
130
|
+
*
|
|
131
|
+
* @param options - Configuration options for multiple file uploads.
|
|
132
|
+
* @returns Multer middleware configured for multiple file uploads.
|
|
133
|
+
*/
|
|
134
|
+
export function uploadMultiple(options) {
|
|
135
|
+
const destination = options.destination || 'uploads';
|
|
136
|
+
// Map fields configuration to multer format
|
|
137
|
+
const fieldConfigs = options.fields.map(field => ({
|
|
138
|
+
name: field.name,
|
|
139
|
+
maxCount: field.maxCount || 10, // Default maxCount is 10 if not specified.
|
|
140
|
+
}));
|
|
141
|
+
let allowedFileTypes = [];
|
|
142
|
+
options.fields.forEach((field) => {
|
|
143
|
+
const types = field.fileTypes || [];
|
|
144
|
+
types.forEach((type) => {
|
|
145
|
+
if (ALLOWED_MIME_TYPES[type]) {
|
|
146
|
+
allowedFileTypes = allowedFileTypes.concat(ALLOWED_MIME_TYPES[type]);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
const multerConfig = {
|
|
151
|
+
destination,
|
|
152
|
+
fileTypes: [],
|
|
153
|
+
customMimeTypes: options.customMimeTypes || [],
|
|
154
|
+
fileSizeLimit: options.fileSizeLimit,
|
|
155
|
+
preservePath: options.preservePath
|
|
156
|
+
};
|
|
157
|
+
const multerInstance = configureMulter(multerConfig);
|
|
158
|
+
const middleware = multerInstance.fields(fieldConfigs);
|
|
159
|
+
return (req, res, next) => {
|
|
160
|
+
// Make sure the destination directory exists
|
|
161
|
+
require('fs').mkdirSync(destination, { recursive: true });
|
|
162
|
+
middleware(req, res, (err) => {
|
|
163
|
+
if (err) {
|
|
164
|
+
if (err.code === 'LIMIT_FILE_SIZE') {
|
|
165
|
+
req.fileValidationError = 'File size limit exceeded';
|
|
166
|
+
}
|
|
167
|
+
else if (err.code === 'INVALID_FILE_TYPE') {
|
|
168
|
+
req.fileValidationError = 'Invalid file type';
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
req.fileValidationError = err.message;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
next();
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Utility function to delete a file from the filesystem
|
|
180
|
+
*
|
|
181
|
+
* @param filePath - The path to the file that needs to be deleted
|
|
182
|
+
* @returns Promise that resolves to true if deletion was successful, false otherwise
|
|
183
|
+
*/
|
|
184
|
+
export async function deleteFile(filePath) {
|
|
185
|
+
try {
|
|
186
|
+
await fs.unlink(filePath);
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
console.error(`Error deleting file: ${error.message}`);
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
// Export the allowed file types for reference
|
|
195
|
+
export const ALLOWED_FILE_TYPES = Object.keys(ALLOWED_MIME_TYPES);
|
|
196
|
+
// Export your functions
|
|
197
|
+
export default {
|
|
198
|
+
uploadSingle,
|
|
199
|
+
uploadMultiple,
|
|
200
|
+
deleteFile,
|
|
201
|
+
ALLOWED_FILE_TYPES
|
|
202
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "module"}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './types/index';
|
package/index.js
CHANGED
|
@@ -1,217 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const { v4: uuidv4 } = require("uuid");
|
|
4
|
-
const fs = require("fs/promises");
|
|
5
|
-
|
|
6
|
-
// Constants for allowed MIME types
|
|
7
|
-
const ALLOWED_MIME_TYPES = {
|
|
8
|
-
images: ["image/jpeg", "image/jpg", "image/png", "image/gif"],
|
|
9
|
-
videos: ["video/mp4", "video/mpeg", "video/ogg", "video/webm", "video/avi"],
|
|
10
|
-
pdfs: ["application/pdf"],
|
|
11
|
-
all: [
|
|
12
|
-
"image/jpeg",
|
|
13
|
-
"image/jpg",
|
|
14
|
-
"image/png",
|
|
15
|
-
"image/gif",
|
|
16
|
-
"video/mp4",
|
|
17
|
-
"video/mpeg",
|
|
18
|
-
"video/ogg",
|
|
19
|
-
"video/webm",
|
|
20
|
-
"video/avi",
|
|
21
|
-
"application/pdf",
|
|
22
|
-
],
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Function to configure storage for Multer.
|
|
27
|
-
*
|
|
28
|
-
* @param {string} destination - The destination folder where files will be stored. Default is "uploads".
|
|
29
|
-
* @returns {object} - Multer storage configuration object.
|
|
30
|
-
*/
|
|
31
|
-
const configureStorage = (destination) => {
|
|
32
|
-
return multer.diskStorage({
|
|
33
|
-
destination: (req, file, cb) => {
|
|
34
|
-
cb(null, destination || "uploads"); // Default folder is "uploads" if none is provided.
|
|
35
|
-
},
|
|
36
|
-
filename: (req, file, cb) => {
|
|
37
|
-
const sanitizedFilename = file.originalname.replace(/\\/g, "/");
|
|
38
|
-
const extension = path.extname(sanitizedFilename);
|
|
39
|
-
const fieldName = file.fieldname || "file"; // Use the field name as part of the filename.
|
|
40
|
-
const uniqueName = uuidv4(); // Generate a unique name using uuid.
|
|
41
|
-
let fileName = `${uniqueName}-${fieldName}${extension}`;
|
|
42
|
-
|
|
43
|
-
// Replace backslashes with forward slashes in the final filename
|
|
44
|
-
fileName = fileName.replace(/\\/g, "/");
|
|
45
|
-
|
|
46
|
-
cb(null, fileName); // Set the final filename.
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Function to configure file filter for Multer.
|
|
53
|
-
*
|
|
54
|
-
* @param {Array} allowedMimeTypes - Array of allowed MIME types.
|
|
55
|
-
* @returns {function} - File filter function for Multer.
|
|
56
|
-
*/
|
|
57
|
-
const configureFileFilter = (allowedMimeTypes) => {
|
|
58
|
-
return (req, file, cb) => {
|
|
59
|
-
if (allowedMimeTypes.includes(file.mimetype)) {
|
|
60
|
-
cb(null, true); // Allow the file if its MIME type is allowed.
|
|
61
|
-
} else {
|
|
62
|
-
cb(
|
|
63
|
-
new Error("Invalid file type. Only specified file types are allowed."),
|
|
64
|
-
false
|
|
65
|
-
); // Reject the file if its MIME type is not allowed.
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Function to configure Multer with the provided options.
|
|
72
|
-
*
|
|
73
|
-
* @param {object} options - Configuration options for Multer.
|
|
74
|
-
* @param {string} [options.destination] - Destination folder for files. Default is "uploads".
|
|
75
|
-
* @param {string} [options.filename] - Custom filename template for saved files.
|
|
76
|
-
* @param {Array<string>} [options.fileTypes] - Array of file types to allow (e.g., ['images', 'videos']).
|
|
77
|
-
* @param {Array<string>} [options.customMimeTypes] - Array of custom MIME types to allow.
|
|
78
|
-
* @param {number} [options.fileSizeLimit] - Maximum file size allowed (in bytes). Default is 50MB.
|
|
79
|
-
* @param {boolean} [options.preservePath] - Preserve the full path of files. Default is false.
|
|
80
|
-
* @returns {object} - Multer instance configured with the provided options.
|
|
81
|
-
*/
|
|
82
|
-
const configureMulter = ({
|
|
83
|
-
destination,
|
|
84
|
-
filename,
|
|
85
|
-
fileTypes = [],
|
|
86
|
-
customMimeTypes = [],
|
|
87
|
-
fileSizeLimit,
|
|
88
|
-
preservePath = false,
|
|
89
|
-
}) => {
|
|
90
|
-
const storage = configureStorage(destination);
|
|
91
|
-
|
|
92
|
-
// Combine allowed MIME types based on fileTypes array
|
|
93
|
-
let allowedMimeTypes = [];
|
|
94
|
-
|
|
95
|
-
if (customMimeTypes.length > 0) {
|
|
96
|
-
// Use custom MIME types if provided
|
|
97
|
-
allowedMimeTypes = customMimeTypes;
|
|
98
|
-
} else {
|
|
99
|
-
// Use default MIME types for specified fileTypes
|
|
100
|
-
fileTypes.forEach((type) => {
|
|
101
|
-
if (ALLOWED_MIME_TYPES[type]) {
|
|
102
|
-
allowedMimeTypes = allowedMimeTypes.concat(ALLOWED_MIME_TYPES[type]);
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
// If no specific file types are provided, use all allowed MIME types
|
|
107
|
-
if (allowedMimeTypes.length === 0) {
|
|
108
|
-
allowedMimeTypes = ALLOWED_MIME_TYPES.all;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const fileFilter = configureFileFilter(allowedMimeTypes);
|
|
113
|
-
|
|
114
|
-
return multer({
|
|
115
|
-
storage,
|
|
116
|
-
fileFilter,
|
|
117
|
-
limits: { fileSize: fileSizeLimit || 1024 * 1024 * 50 }, // Default 50MB file size limit
|
|
118
|
-
preservePath,
|
|
119
|
-
});
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Function to handle multiple fields in a single form submission.
|
|
124
|
-
*
|
|
125
|
-
* @param {Array} fields - Array of field configurations, each containing:
|
|
126
|
-
* @param {string} fields.name - The name of the form field.
|
|
127
|
-
* @param {number} [fields.maxCount=10] - The maximum number of files to accept per field.
|
|
128
|
-
* @param {Array<string>} [fields.fileTypes] - Array of file types to allow for this field (e.g., ['images']).
|
|
129
|
-
* @returns {function} - Multer instance configured to handle multiple fields.
|
|
130
|
-
*/
|
|
131
|
-
const uploadFields = (fields) => {
|
|
132
|
-
const fieldConfigs = fields.map((field) => ({
|
|
133
|
-
name: field.name,
|
|
134
|
-
maxCount: field.maxCount || 10, // Default maxCount is 10 if not specified.
|
|
135
|
-
}));
|
|
136
|
-
|
|
137
|
-
let allowedFileTypes = [];
|
|
138
|
-
|
|
139
|
-
fields.forEach((field) => {
|
|
140
|
-
const types = field.fileTypes || [];
|
|
141
|
-
types.forEach((type) => {
|
|
142
|
-
if (ALLOWED_MIME_TYPES[type]) {
|
|
143
|
-
allowedFileTypes = allowedFileTypes.concat(ALLOWED_MIME_TYPES[type]);
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
const multerInstance = configureMulter({
|
|
149
|
-
fileTypes: allowedFileTypes,
|
|
150
|
-
customMimeTypes: [],
|
|
151
|
-
fileSizeLimit: fields[0]?.fileSizeLimit, // Assuming all fields share the same limit.
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
return multerInstance.fields(fieldConfigs);
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Utility function to delete a file from the filesystem
|
|
159
|
-
*
|
|
160
|
-
* @param {string} filePath - The path to the file that needs to be deleted
|
|
161
|
-
* @returns {Promise<boolean>} - Returns true if deletion was successful, false otherwise
|
|
162
|
-
*/
|
|
163
|
-
const deleteFile = async (filePath) => {
|
|
164
|
-
try {
|
|
165
|
-
await fs.unlink(filePath);
|
|
166
|
-
return true;
|
|
167
|
-
} catch (error) {
|
|
168
|
-
console.error(`Error deleting file: ${error.message}`);
|
|
169
|
-
return false;
|
|
170
|
-
}
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
// Export functions to configure multer and available file types
|
|
174
|
-
module.exports = {
|
|
175
|
-
/**
|
|
176
|
-
* Function to handle a single file upload.
|
|
177
|
-
*
|
|
178
|
-
* @param {object} options - Configuration options for the single file upload.
|
|
179
|
-
* @param {string} [options.destination] - Destination folder for the uploaded file.
|
|
180
|
-
* @param {string} [options.filename] - Custom filename template for the uploaded file.
|
|
181
|
-
* @param {Array<string>} [options.fileTypes] - Array of file types to allow (e.g., ['images']).
|
|
182
|
-
* @param {Array<string>} [options.customMimeTypes] - Array of custom MIME types to allow.
|
|
183
|
-
* @param {number} [options.fileSizeLimit] - Maximum file size allowed (in bytes). Default is 50MB.
|
|
184
|
-
* @param {boolean} [options.preservePath] - Preserve the full path of the uploaded file. Default is false.
|
|
185
|
-
* @returns {function} - Multer instance configured for single file upload.
|
|
186
|
-
*/
|
|
187
|
-
uploadSingle: (options = {}) => {
|
|
188
|
-
const multerInstance = configureMulter(options);
|
|
189
|
-
return multerInstance.single(options.filename || "file");
|
|
190
|
-
},
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Function to handle multiple file uploads across multiple fields.
|
|
194
|
-
*
|
|
195
|
-
* @param {object} options - Configuration options for multiple file uploads.
|
|
196
|
-
* @param {Array<object>} options.fields - Array of field configurations for multiple file uploads.
|
|
197
|
-
* @param {string} [options.destination] - Destination folder for the uploaded files.
|
|
198
|
-
* @param {Array<string>} [options.customMimeTypes] - Array of custom MIME types to allow.
|
|
199
|
-
* @param {number} [options.fileSizeLimit] - Maximum file size allowed (in bytes). Default is 50MB.
|
|
200
|
-
* @param {boolean} [options.preservePath] - Preserve the full path of the uploaded files. Default is false.
|
|
201
|
-
* @returns {function} - Multer instance configured for multiple file uploads.
|
|
202
|
-
*/
|
|
203
|
-
uploadMultiple: (options = {}) => {
|
|
204
|
-
const multerInstance = configureMulter(options);
|
|
205
|
-
return multerInstance.fields(options.fields || []);
|
|
206
|
-
},
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Export the allowed file types for reference.
|
|
210
|
-
*
|
|
211
|
-
* @type {Array<string>}
|
|
212
|
-
*/
|
|
213
|
-
ALLOWED_FILE_TYPES: Object.keys(ALLOWED_MIME_TYPES),
|
|
214
|
-
|
|
215
|
-
// Add the delete file utility
|
|
216
|
-
deleteFile,
|
|
217
|
-
};
|
|
1
|
+
// CommonJS entry point
|
|
2
|
+
module.exports = require("./dist/cjs/index");
|
package/index.mjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// ES Module entry point
|
|
2
|
+
export * from "./dist/esm/index.js";
|
|
3
|
+
|
|
4
|
+
// This pattern ensures better compatibility with various Node.js environments
|
|
5
|
+
import { createRequire } from "module";
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
const multermate = require("./dist/cjs/index.js");
|
|
8
|
+
|
|
9
|
+
// Re-export everything from the CJS module
|
|
10
|
+
export const uploadSingle = multermate.uploadSingle;
|
|
11
|
+
export const uploadMultiple = multermate.uploadMultiple;
|
|
12
|
+
export const deleteFile = multermate.deleteFile;
|
|
13
|
+
|
|
14
|
+
// Default export
|
|
15
|
+
export default multermate;
|
package/package.json
CHANGED
|
@@ -1,9 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "multermate",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "A flexible and customizable
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"description": "A flexible and customizable file upload utility built on top of Multer with TypeScript support",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"module": "index.mjs",
|
|
7
|
+
"types": "index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"require": "./dist/cjs/index.js",
|
|
11
|
+
"import": "./dist/esm/index.js",
|
|
12
|
+
"types": "./types/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/",
|
|
17
|
+
"types/",
|
|
18
|
+
"index.js",
|
|
19
|
+
"index.mjs",
|
|
20
|
+
"index.d.ts"
|
|
21
|
+
],
|
|
6
22
|
"scripts": {
|
|
23
|
+
"build": "npm run clean && npm run build:cjs && npm run build:esm && npm run build:types",
|
|
24
|
+
"build:cjs": "tsc --module commonjs --outDir dist/cjs",
|
|
25
|
+
"build:esm": "tsc --module ES2020 --outDir dist/esm && echo '{\"type\": \"module\"}' > dist/esm/package.json",
|
|
26
|
+
"build:types": "tsc --emitDeclarationOnly",
|
|
27
|
+
"clean": "node -e \"const fs=require('fs'); ['dist', 'types'].forEach(dir => { try { fs.rmSync(dir, {recursive: true, force: true}); } catch(e){} })\"",
|
|
28
|
+
"prepublishOnly": "npm run build",
|
|
7
29
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
30
|
},
|
|
9
31
|
"keywords": [
|
|
@@ -19,6 +41,13 @@
|
|
|
19
41
|
"multer": "1.4.5-lts.1",
|
|
20
42
|
"uuid": "^10.0.0"
|
|
21
43
|
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/multer": "^1.4.11",
|
|
46
|
+
"@types/node": "^20.10.5",
|
|
47
|
+
"@types/uuid": "^9.0.7",
|
|
48
|
+
"rimraf": "^5.0.5",
|
|
49
|
+
"typescript": "^5.3.3"
|
|
50
|
+
},
|
|
22
51
|
"repository": {
|
|
23
52
|
"type": "git",
|
|
24
53
|
"url": "git+https://github.com/Wasim-Zaman/multermate.git"
|
package/readme.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Multer Mate
|
|
2
2
|
|
|
3
|
-
A robust and flexible file upload utility built on top of Multer, providing advanced file handling capabilities for Node.js applications.
|
|
3
|
+
A robust and flexible file upload utility built on top of Multer, providing advanced file handling capabilities for Node.js applications. Now with full TypeScript support!
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
@@ -14,6 +14,8 @@ A robust and flexible file upload utility built on top of Multer, providing adva
|
|
|
14
14
|
- 🔄 Unique file naming with UUID
|
|
15
15
|
- 🛡️ Path sanitization
|
|
16
16
|
- 📝 Comprehensive error handling
|
|
17
|
+
- 🔌 Works with CommonJS, ES Modules, and TypeScript
|
|
18
|
+
- 📘 Full TypeScript definitions and type safety
|
|
17
19
|
|
|
18
20
|
## Installation
|
|
19
21
|
|
|
@@ -23,10 +25,37 @@ npm install multermate
|
|
|
23
25
|
|
|
24
26
|
## Basic Usage
|
|
25
27
|
|
|
28
|
+
### CommonJS
|
|
29
|
+
|
|
26
30
|
```javascript
|
|
27
31
|
const { uploadSingle, uploadMultiple, deleteFile } = require("multermate");
|
|
28
32
|
```
|
|
29
33
|
|
|
34
|
+
### ES Modules
|
|
35
|
+
|
|
36
|
+
```javascript
|
|
37
|
+
import { uploadSingle, uploadMultiple, deleteFile } from "multermate";
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### TypeScript
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import {
|
|
44
|
+
uploadSingle,
|
|
45
|
+
uploadMultiple,
|
|
46
|
+
deleteFile,
|
|
47
|
+
UploadSingleOptions,
|
|
48
|
+
UploadMultipleOptions,
|
|
49
|
+
} from "multermate";
|
|
50
|
+
|
|
51
|
+
// With type definitions
|
|
52
|
+
const options: UploadSingleOptions = {
|
|
53
|
+
destination: "uploads/images",
|
|
54
|
+
fileTypes: ["images"],
|
|
55
|
+
fileSizeLimit: 5 * 1024 * 1024,
|
|
56
|
+
};
|
|
57
|
+
```
|
|
58
|
+
|
|
30
59
|
## Upload Configurations
|
|
31
60
|
|
|
32
61
|
### Single File Upload
|
|
@@ -99,7 +128,10 @@ app.post(
|
|
|
99
128
|
"text/csv",
|
|
100
129
|
],
|
|
101
130
|
fileSizeLimit: 1024 * 1024, // 1MB
|
|
102
|
-
})
|
|
131
|
+
}),
|
|
132
|
+
(req, res) => {
|
|
133
|
+
res.json({ file: req.file });
|
|
134
|
+
}
|
|
103
135
|
);
|
|
104
136
|
```
|
|
105
137
|
|
|
@@ -167,11 +199,12 @@ Configures multiple file uploads with the following options:
|
|
|
167
199
|
|
|
168
200
|
#### Field Configuration
|
|
169
201
|
|
|
170
|
-
| Option
|
|
171
|
-
|
|
|
172
|
-
| name
|
|
173
|
-
| maxCount
|
|
174
|
-
| fileTypes
|
|
202
|
+
| Option | Type | Default | Description |
|
|
203
|
+
| ------------- | -------- | ------- | --------------------- |
|
|
204
|
+
| name | string | - | Field name (required) |
|
|
205
|
+
| maxCount | number | 10 | Max files per field |
|
|
206
|
+
| fileTypes | string[] | ['all'] | Allowed types |
|
|
207
|
+
| fileSizeLimit | number | 50MB | Max file size |
|
|
175
208
|
|
|
176
209
|
### deleteFile(filePath)
|
|
177
210
|
|
|
@@ -182,18 +215,32 @@ Deletes a file from the filesystem:
|
|
|
182
215
|
| filePath | string | Path to file |
|
|
183
216
|
| Returns | Promise<boolean> | Deletion success |
|
|
184
217
|
|
|
185
|
-
### Supported
|
|
218
|
+
### Supported MIME Types
|
|
186
219
|
|
|
187
220
|
```javascript
|
|
188
|
-
const
|
|
189
|
-
images: ["jpeg", "jpg", "png", "gif"],
|
|
190
|
-
videos: ["mp4", "mpeg", "ogg", "webm", "avi"],
|
|
191
|
-
pdfs: ["pdf"],
|
|
221
|
+
const ALLOWED_MIME_TYPES = {
|
|
222
|
+
images: ["image/jpeg", "image/jpg", "image/png", "image/gif"],
|
|
223
|
+
videos: ["video/mp4", "video/mpeg", "video/ogg", "video/webm", "video/avi"],
|
|
224
|
+
pdfs: ["application/pdf"],
|
|
225
|
+
all: [
|
|
226
|
+
"image/jpeg",
|
|
227
|
+
"image/jpg",
|
|
228
|
+
"image/png",
|
|
229
|
+
"image/gif",
|
|
230
|
+
"video/mp4",
|
|
231
|
+
"video/mpeg",
|
|
232
|
+
"video/ogg",
|
|
233
|
+
"video/webm",
|
|
234
|
+
"video/avi",
|
|
235
|
+
"application/pdf",
|
|
236
|
+
],
|
|
192
237
|
};
|
|
193
238
|
```
|
|
194
239
|
|
|
195
240
|
## Error Handling
|
|
196
241
|
|
|
242
|
+
MulterMate adds a `fileValidationError` property to the request object when validation fails:
|
|
243
|
+
|
|
197
244
|
```javascript
|
|
198
245
|
app.post("/upload", uploadSingle(), (req, res) => {
|
|
199
246
|
try {
|
|
@@ -229,6 +276,37 @@ app.post("/upload", uploadSingle(), (req, res) => {
|
|
|
229
276
|
});
|
|
230
277
|
```
|
|
231
278
|
|
|
279
|
+
## TypeScript Support
|
|
280
|
+
|
|
281
|
+
MulterMate includes complete TypeScript definitions for all functions and options:
|
|
282
|
+
|
|
283
|
+
```typescript
|
|
284
|
+
// Type definitions for all options
|
|
285
|
+
import {
|
|
286
|
+
UploadSingleOptions,
|
|
287
|
+
UploadMultipleOptions,
|
|
288
|
+
FieldConfig,
|
|
289
|
+
} from "multermate";
|
|
290
|
+
|
|
291
|
+
// Using with Express and TypeScript
|
|
292
|
+
import express from "express";
|
|
293
|
+
import { uploadSingle } from "multermate";
|
|
294
|
+
|
|
295
|
+
const app = express();
|
|
296
|
+
|
|
297
|
+
app.post(
|
|
298
|
+
"/upload",
|
|
299
|
+
uploadSingle({
|
|
300
|
+
destination: "uploads/typescript",
|
|
301
|
+
fileTypes: ["images"],
|
|
302
|
+
fileSizeLimit: 5 * 1024 * 1024,
|
|
303
|
+
}),
|
|
304
|
+
(req, res) => {
|
|
305
|
+
res.json({ success: true, file: req.file });
|
|
306
|
+
}
|
|
307
|
+
);
|
|
308
|
+
```
|
|
309
|
+
|
|
232
310
|
## Best Practices
|
|
233
311
|
|
|
234
312
|
1. Always implement proper error handling
|
|
@@ -237,6 +315,8 @@ app.post("/upload", uploadSingle(), (req, res) => {
|
|
|
237
315
|
4. Use custom storage destinations for different file types
|
|
238
316
|
5. Implement file cleanup mechanisms
|
|
239
317
|
6. Consider implementing file type verification beyond MIME types
|
|
318
|
+
7. Create upload directories if they don't exist (MulterMate does this automatically)
|
|
319
|
+
8. Use TypeScript types for better development experience
|
|
240
320
|
|
|
241
321
|
## License
|
|
242
322
|
|
|
@@ -248,8 +328,8 @@ Contributions are welcome! Please feel free to submit issues and pull requests.
|
|
|
248
328
|
|
|
249
329
|
## Author
|
|
250
330
|
|
|
251
|
-
|
|
331
|
+
Wasim Zaman
|
|
252
332
|
|
|
253
333
|
## Support
|
|
254
334
|
|
|
255
|
-
For support, please open an issue in the GitHub repository.
|
|
335
|
+
For support, please open an issue in the GitHub repository: https://github.com/Wasim-Zaman/multermate
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from 'express';
|
|
2
|
+
export interface UploadSingleOptions {
|
|
3
|
+
destination?: string;
|
|
4
|
+
filename?: string;
|
|
5
|
+
fileTypes?: string[];
|
|
6
|
+
customMimeTypes?: string[];
|
|
7
|
+
fileSizeLimit?: number;
|
|
8
|
+
preservePath?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface FieldConfig {
|
|
11
|
+
name: string;
|
|
12
|
+
maxCount?: number;
|
|
13
|
+
fileTypes?: string[];
|
|
14
|
+
fileSizeLimit?: number;
|
|
15
|
+
}
|
|
16
|
+
export interface UploadMultipleOptions {
|
|
17
|
+
fields: FieldConfig[];
|
|
18
|
+
destination?: string;
|
|
19
|
+
customMimeTypes?: string[];
|
|
20
|
+
fileSizeLimit?: number;
|
|
21
|
+
preservePath?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Function to handle a single file upload.
|
|
25
|
+
*
|
|
26
|
+
* @param options - Configuration options for the single file upload.
|
|
27
|
+
* @returns Multer middleware configured for single file upload.
|
|
28
|
+
*/
|
|
29
|
+
export declare function uploadSingle(options?: UploadSingleOptions): (req: Request, res: Response, next: NextFunction) => void;
|
|
30
|
+
/**
|
|
31
|
+
* Function to handle multiple file uploads across multiple fields.
|
|
32
|
+
*
|
|
33
|
+
* @param options - Configuration options for multiple file uploads.
|
|
34
|
+
* @returns Multer middleware configured for multiple file uploads.
|
|
35
|
+
*/
|
|
36
|
+
export declare function uploadMultiple(options: UploadMultipleOptions): (req: Request, res: Response, next: NextFunction) => void;
|
|
37
|
+
/**
|
|
38
|
+
* Utility function to delete a file from the filesystem
|
|
39
|
+
*
|
|
40
|
+
* @param filePath - The path to the file that needs to be deleted
|
|
41
|
+
* @returns Promise that resolves to true if deletion was successful, false otherwise
|
|
42
|
+
*/
|
|
43
|
+
export declare function deleteFile(filePath: string): Promise<boolean>;
|
|
44
|
+
declare global {
|
|
45
|
+
namespace Express {
|
|
46
|
+
interface Request {
|
|
47
|
+
fileValidationError?: string;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export declare const ALLOWED_FILE_TYPES: string[];
|
|
52
|
+
declare const _default: {
|
|
53
|
+
uploadSingle: typeof uploadSingle;
|
|
54
|
+
uploadMultiple: typeof uploadMultiple;
|
|
55
|
+
deleteFile: typeof deleteFile;
|
|
56
|
+
ALLOWED_FILE_TYPES: string[];
|
|
57
|
+
};
|
|
58
|
+
export default _default;
|