Skip to content

Instantly share code, notes, and snippets.

@igorfonseca05
Last active February 7, 2025 10:24
Show Gist options
  • Save igorfonseca05/1bc3864aa153bdfdd5502441332a0d09 to your computer and use it in GitHub Desktop.
Save igorfonseca05/1bc3864aa153bdfdd5502441332a0d09 to your computer and use it in GitHub Desktop.
Setting up multer
// Usando multer para uploads de arquivos
const upload = multer({
storage: multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'src/uploads')
},
filename: (req, file, cb) => {
const uniqueName = `${Date.now()}-${Math.random() * 1e9}${path.extname(file.originalname)}`
cb(null, uniqueName)
}
}),
limits: { fileSize: 1 * 1024 * 1024 }, // 1MB
fileFilter: (req, file, cb) => {
if (file.originalname.match(/\.(png|jpg|jpeg)$/)) {
return cb(null, file)
}
cb(new Error('Formato de arquivo inválido'))
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment