Skip to content

Instantly share code, notes, and snippets.

@leomarcelino
Created October 30, 2019 00:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save leomarcelino/13e2f3d62298022b9f74bc93d1edef6a to your computer and use it in GitHub Desktop.
Multer config example
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