Skip to content

Commit 55c2c7b

Browse files
committedJun 4, 2019
fix: implement fileFilter to allow multer to ignore other files
1 parent 20bf7f1 commit 55c2c7b

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed
 

‎src/file.model.js

+16-20
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,13 @@ export const createModels = () => {
191191
* Image.find((error, files) => { ... });
192192
*
193193
*/
194-
export const modelFor = bucket => {
194+
export const modelFor = (bucket = 'fs') => {
195195
// create models
196196
const models = createModels();
197197

198-
// use default bucket options
199-
let bucketInfo = Buckets.File;
200-
201-
// obtain options for specified bucket
202-
bucketInfo = find(values(Buckets), { bucketName: bucket }) || bucketInfo;
203-
204198
// obtain model name for specified bucket
205-
const { modelName } = bucketInfo;
199+
const { modelName } =
200+
find(values(Buckets), { bucketName: bucket }) || Buckets.File;
206201

207202
// obtain GridFS model instace for specified bucket
208203
const Model = get(models, modelName);
@@ -233,18 +228,13 @@ export const modelFor = bucket => {
233228
* images.unlink(_id, (error, _id) => { ... });
234229
*
235230
*/
236-
export const bucketFor = bucket => {
231+
export const bucketFor = (bucket = 'fs') => {
237232
// create buckets
238233
const buckets = createBuckets();
239234

240-
// use default bucket options
241-
let bucketInfo = Buckets.File;
242-
243235
// obtain options for specified bucket
244-
bucketInfo = find(values(Buckets), { bucketName: bucket }) || bucketInfo;
245-
246-
// obtain real bucket name for specified bucket
247-
const { bucketName } = bucketInfo;
236+
const { bucketName } =
237+
find(values(Buckets), { bucketName: bucket }) || Buckets.File;
248238

249239
// obtain GridFSBucket instance
250240
const Bucket = get(buckets, bucketName) || buckets.files;
@@ -259,16 +249,22 @@ export const uploaderFor = (/* ...bucket */) => (request, response, next) => {
259249

260250
// obtain bucket options
261251
const { field, bucketName } =
262-
find(Buckets, { bucketName: bucket }) || Buckets.File;
252+
find(values(Buckets), { bucketName: bucket }) || Buckets.File;
263253

264254
// obtain GridFSBucket storage
265255
const storage = bucketFor(bucketName);
266256

267257
// obtain model for the bucket
268258
const File = modelFor(bucketName);
269259

260+
// const file filter
261+
const fileFilter = (req, file, cb) => {
262+
const isAllowed = file && file.fieldname === field;
263+
cb(null, isAllowed);
264+
};
265+
270266
// create multer uploader
271-
const upload = multer({ storage }).single(field);
267+
const upload = multer({ storage, fileFilter }).any();
272268

273269
// do upload
274270
upload(request, response, error => {
@@ -278,9 +274,9 @@ export const uploaderFor = (/* ...bucket */) => (request, response, next) => {
278274
}
279275

280276
// extend request with file instance
281-
if (request.file && request.file.fieldname === field) {
277+
if (request.files) {
282278
// create file instance
283-
const file = new File(request.file);
279+
const file = new File(request.files[0]);
284280

285281
// set file to request
286282
request.file = file;

0 commit comments

Comments
 (0)