Created
October 30, 2019 00:06
-
-
Save leomarcelino/13e2f3d62298022b9f74bc93d1edef6a to your computer and use it in GitHub Desktop.
Multer config example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const multer = require('multer') | |
const path = require('path') | |
const crypto = require('crypto') | |
module.exports = { | |
dest: path.resolve(__dirname, '..', '..', 'tmp', 'uploads'), | |
storage: multer.diskStorage({ | |
destination: (req, file, done) => { | |
done(null, path.resolve(__dirname, '..', '..', 'tmp', 'uploads')) | |
}, | |
filename: (req, file, done) => { | |
crypto.randomBytes(16, (err, hash) => { | |
if (err) return done(err) | |
const fileName = `${hash.toString('hex')}-${file.originalname}` | |
return done(null, fileName) | |
}) | |
} | |
}), | |
limits: { | |
fileSize: 5 * 1024 * 1024 | |
}, | |
fileFilter: (req, file, done) => { | |
const allowedMimes = [ | |
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' | |
] | |
if (allowedMimes.includes(file.mimetype)) { | |
return done(null, true) | |
} | |
return done(new Error('File type not allowed')) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment